0

GCC beautifully pre-computes the results of standard math functions at compile time when the input is a constant.

Anyone know how to do the same with Microsoft C? I've tried everything and so far no joy.

Code example:

#include <stdio.h>
#include <math.h>

#define FFT_SIZE_LOG2       10
#define CALC_FFT_SIZE(x)    (int)pow(2., (double)x)

int main (void)
{
    printf ("FFT size = %d\n", CALC_FFT_SIZE(FFT_SIZE_LOG2));
    return(0);
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
Johned
  • 143
  • 1
  • 9
  • How did you verify that MSVC does not do this? (Use [edit] to add that in your question.) – Jongware Oct 13 '18 at 11:19
  • It works fine with `1 << x` of course but I'm not sure what to do about your specific example – harold Oct 13 '18 at 11:25
  • Do not use `pow` to compute integer powers of two. Poor implementations of `pow` return incorrect values, and, for many years, Microsoft’s implementation was one such. Powers of two are easily prepared using the bit-shift operator, `<<`. – Eric Postpischil Oct 13 '18 at 12:05
  • Thanks for the replies. @usr2564301 To verify I used : cl -c -Fa -Ox log_size.c – Johned Oct 13 '18 at 15:38
  • @harold, << doesn't use the std math functions. – Johned Oct 13 '18 at 15:41
  • 1
    @Eric. I used pow() as a simple example. MSVC does not appear to be able to pre-compute any functions at compile time – Johned Oct 13 '18 at 15:42
  • Although `pow(2,10)` is an example, it is a weak one as `#define CALC_FFT_SIZE(x) ((int)(1u << (x)))` is a reasonable alternative. It would be better to post an example of a true need for compile time `` pre-computation that does not have a trivial alternative.. – chux - Reinstate Monica Oct 13 '18 at 18:21
  • How about this code : #include #include int main (void) { printf ("log = %lf\n", log(7.1729383)); return(0); } – Johned Oct 13 '18 at 20:45
  • I've had some great suggestions about how to optimize specific functions but none to suggest how to force Visual C/C++ to pre-compute any math.h function to a constant in the same way that GCC does. I'm beginning to wonder if it is even possible or is this another area where GCC beats Microsoft ;-) – Johned Oct 16 '18 at 14:39

0 Answers0