Can I use INLINE this way?

Previous topic - Next topic

Hatonastick

Or should I make DLL's and call the functions in there?  I'd prefer to be able to use INLINE in this way if it was possible and use a combination of C++ and GLBasic to make libraries of functions.  I have my reasons why.

For example, the ANSI C code for the TEA encryption algorithm is:

Code (glbasic) Select
TEA takes 64 bits of data in v[0] and v[1], and 128 bits of key in k[0] - k[3].
The result is returned in w[0] and w[1].

Note this algorithm is optimised for 32-bit CPUs with fast shift capabilities.
It can very easily be ported to assembly language on most CPUs.

void encipher(unsigned long *const v,unsigned long *const w,
   const unsigned long *const k)
{
   register unsigned long       y=v[0],z=v[1],sum=0,delta=0x9E3779B9,
a=k[0],b=k[1],c=k[2],d=k[3],n=32;

   while(n-->0)
      {
      sum += delta;
      y += (z << 4)+a ^ z+sum ^ (z >> 5)+b;
      z += (y << 4)+c ^ y+sum ^ (y >> 5)+d;
      }

   w[0]=y; w[1]=z;
}

void decipher(unsigned long *const v,unsigned long *const w,
   const unsigned long *const k)
{
   register unsigned long       y=v[0],z=v[1],sum=0xC6EF3720,
delta=0x9E3779B9,a=k[0],b=k[1],
c=k[2],d=k[3],n=32;

   /* sum = delta<<5, in general sum = delta * n */

   while(n-->0)
      {
      z -= (y << 4)+c ^ y+sum ^ (y >> 5)+d;
      y -= (z << 4)+a ^ z+sum ^ (z >> 5)+b;
      sum -= delta;
      }
   
   w[0]=y; w[1]=z;
}


Now the main difficulty is that encryption routines of this sort often tend to be touchy about the size and type of variable used because they often involve bit shifting and other similar tricks.  I started work on turning the above into GLBasic functions using INLINE, but hit a snag regarding data conversion as GLBasic doesn't appear to have the ability to make banks or blocks of memory which you can then access with peeks and pokes or similar.

So I guess what I'm asking is: Is it possible to turn the above into two GLBasic functions using INLINE?  If so, can I see an example of this please for future reference?  I have other similar C and C++ code that I'd like to turn into GLBasic functions (and therefore GLBasic libraries) and need to know if its possible.

Note:  I haven't coded anything in months and am just starting to look into it again.  I'm probably using the wrong terms and gotten everything muddled, so I apologise in advance for any possible confusion caused. :)

Edit: My brain must have been switched off.  I just realised the "easy" way around it is to (in C++) convert between the relevant data types (GLBasic types and the required C++ types) using C++ in the INLINE section instead of trying to do it in the GLBasic part of the GLBasic function.  So the answer is: yes it is possible, just do things the right way around by doing the type conversions in C++ inside INLINE/ENDINLINE instead of trying to do them using GLBasic commands.  Hope that makes sense.  Sorry...

Hemlos

Thank you, for answering your own questions, youre your own best friend :)

Can i see an example of the GBAS code you converted for these functions?
Im curious what you were talking about.
Bing ChatGpt is pretty smart :O

Hatonastick

#2
It happens a lot.  I often can't think of the answer until I've posted the question on a forum.  I suspect I secretly hate me and want to embarrass me in public (just call me Rimmer).    :S

Sure - if/when it's finished and it works (I wouldn't hold your breath).  Otherwise the answer would be no. =D  I have a bunch of things I'd like to "convert", this was just a test case.  I'd rather use INLINE when and if I can get away with it instead of throwing these things into a DLL.

Edit: After playing with it a bit more: Is it possible? Yes, but I'm starting to think it might be simpler to create a DLL in C and use it that way.  Maybe.