Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine `sizeof float` without compilation

Tags:

c

gcc

sizeof

I'd like to know the size of a float in GCC, without having to run the compiler. I know one option is to write a small function and have the compiler print out an assembly listing.

There is limits.h, which contains the minimums and maximums, but is there something similar that tells the size of the different implicit types?

I'm using GCC on Windows 7 x64; the target platform is ARM7 32 bit mode. Language is C.

like image 371
Thomas Matthews Avatar asked Aug 23 '11 22:08

Thomas Matthews


1 Answers

You can have GCC print out all of the default macros:

gcc -dM -E - </dev/null | grep FLT

Then you get lines like:

#define __FLT_MANT_DIG__ 24
#define __FLT_MAX_EXP__ 128

Now you can parse this as follows:

24 + lg(128) + 1 = 32

To find documentation:

1) man gcc:

   -E  Stop after the preprocessing stage; do not run the compiler proper.
       The output is in the form of preprocessed source code, which is
       sent to the standard output.

...

   -dCHARS
       CHARS is a sequence of one or more of the following characters, and
       must not be preceded by a space.  Other characters are interpreted
       by the compiler proper, or reserved for future versions of GCC, and
       so are silently ignored.  If you specify characters whose behavior
       conflicts, the result is undefined.

       M   Instead of the normal output, generate a list of #define
           directives for all the macros defined during the execution of
           the preprocessor, including predefined macros.  This gives you
           a way of finding out what is predefined in your version of the
           preprocessor.  Assuming you have no file foo.h, the command

                   touch foo.h; cpp -dM foo.h

           will show all the predefined macros.

2) the actual macros:

http://www.gnu.org/s/hello/manual/libc/Floating-Point-Parameters.html

like image 127
Foo Bah Avatar answered Oct 15 '22 11:10

Foo Bah