0

I was wondering if there's any point in modifying code to use a lookup table for math.h exp, or if it was done automatically by clang++ when given the -O3 flag.

If it's not, is there a #pragma that does this?

clarification

I was talking about memoisation of function calls. That is, if the function is passed the same exact arguments (yeah floating point numbers can actually be the same, if they're derived from constants and integers).

Example

Say I was going to run exp(-a*x) where a is not a compile-time constant, but once it's set once, it won't be modified. Similarly x can be one of 10 possible values, all of which are set once, and not modified again.

Alex Petrosyan
  • 473
  • 2
  • 20

1 Answers1

2

I don't know much about clang specifics, so I cannot tell details about what it does. But I can reason what it might be able to do.

If the argument is a compile time constant, then the optimizer might be able to pre-calculate at compile/link time. But it's by no means required to, and I wouldn't expect all compilers to do that.

If the argument is not compile time constant, and multiple calls within same function use the same argument (and there are no volatile objects involved), then the optimizer might re-use the result if it knows that the functions don't have side-effects. This optimization I would recommend you to do manually, just because it's better to not repeat yourself anyway.

If the argument is not compile time constant, then you could store the result in a table somewhere to be used later, but the memory access to load the value is probably much slower than doing the calculation itself, so this might end up being a pessimisation.

Obviously, whether there is any point in doing any of these optimisation will depend on many aspects such as the CPU architechture, caches etc. and most importantly on whether the operation had any significant effect on the performance in the first place.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • So how would I specify that objects are non-volatile? – Alex Petrosyan Apr 10 '18 at 21:28
  • @AlexPetrosyan by refraining from using `volatile`. – eerorika Apr 10 '18 at 21:29
  • I did some testing, with my implementation of a lookup table... When compiled with `-O3`, it automatically `caches' expensive operations. My attempt at memoisation is definitely making the code slower. – Alex Petrosyan Apr 10 '18 at 22:06
  • 2
    @AlexPetrosyan: You probably spotted an optimization known as Common SubExpression Elimination (CSE). If a compiler spots two calls with the exact same arguments, to a known pure function, it usually strips out the second call. (Pure functions don't have side effects. Obviously you can't strip two calls to `std::printf`) – MSalters Apr 11 '18 at 00:37