J
Jason Curl
Greetings,
I have an array of 32 values. This makes it extremely fast to access
elements in this array based on an index provided by a separate enum. This
array is defined of type "unsigned long int". I have a typedef for this:
typedef unsigned long int Uint32;
typedef float Float32;
Uint32 myArray[100];
In C++ there are casting operators, however I'm writing my software in C.
I'm trying to tell the compiler to interpret a value as a float (32-bit
entity on this platform), but store it into the Uint32 array, which I later
take and convert back to float. (I don't want to have type promotion or
demotion, just have the compiler change its interpretation of the type).
This can be easily done by
Uint32 u32;
Float32 f32;
f32=3.1415;
u32 = *(Uint32 *)(&f32);
So now, if you print u32 to the console it will print what looks like
garbage (definitely not the number 3), but the binary representation remains
the same.
The same could be done also with Union structs (but the above is nicer I
think).
But for readability I would like to combine all this into something that
appears on one line without having to first put the number into a variable
or register, so that:
u32 = ConvertToFloat(3.14159);
I don't want to write a function - the overhead is high and I now need to
put this function in a library that will need to be linked (this is being
used by multiple binaries, somewhat like a "plugin" architecture). Also,
it's dependent on the compiler if such a function would be inline'd or not
(C doesn't provide inline as far as I know, and I still want everything in a
header file if possible). It would be convenient to have a macro in a header
file that could do this.
Does anyone know how to write such a macro?
Is there something that can be written in assembly for Microsoft Visual C++?
I don't know anything about Intel Assembly.
The other alternative I can think is an array of 32-bit values that derives
its type from something I explicitly set it to.
I have an array of 32 values. This makes it extremely fast to access
elements in this array based on an index provided by a separate enum. This
array is defined of type "unsigned long int". I have a typedef for this:
typedef unsigned long int Uint32;
typedef float Float32;
Uint32 myArray[100];
In C++ there are casting operators, however I'm writing my software in C.
I'm trying to tell the compiler to interpret a value as a float (32-bit
entity on this platform), but store it into the Uint32 array, which I later
take and convert back to float. (I don't want to have type promotion or
demotion, just have the compiler change its interpretation of the type).
This can be easily done by
Uint32 u32;
Float32 f32;
f32=3.1415;
u32 = *(Uint32 *)(&f32);
So now, if you print u32 to the console it will print what looks like
garbage (definitely not the number 3), but the binary representation remains
the same.
The same could be done also with Union structs (but the above is nicer I
think).
But for readability I would like to combine all this into something that
appears on one line without having to first put the number into a variable
or register, so that:
u32 = ConvertToFloat(3.14159);
I don't want to write a function - the overhead is high and I now need to
put this function in a library that will need to be linked (this is being
used by multiple binaries, somewhat like a "plugin" architecture). Also,
it's dependent on the compiler if such a function would be inline'd or not
(C doesn't provide inline as far as I know, and I still want everything in a
header file if possible). It would be convenient to have a macro in a header
file that could do this.
Does anyone know how to write such a macro?
Is there something that can be written in assembly for Microsoft Visual C++?
I don't know anything about Intel Assembly.
The other alternative I can think is an array of 32-bit values that derives
its type from something I explicitly set it to.