0

I have some problems with the log10 function and the importing of math.h. The usage of this function is likely critical for now and I need to reduce their consumption.

Is there a way to use a quicker log10 function that will not need to include math.h library ?

Some precisions : I need a log algorithm on real values (from -100. to 100.) and returning real values. An approximation table with interpolations between steps can be accepted if it is faster than log10.

alpereira7
  • 1,444
  • 3
  • 18
  • 28
  • 4
    What's important is how much precision you need. Is an approximation enough? – phuclv Nov 23 '16 at 08:47
  • @Garf365 those are [integer log10](https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10) which might not be what the OP wants – phuclv Nov 23 '16 at 08:50
  • 1
    FWIW x86 (assuming that's your target) has some log instructions (log2 mostly, not log10, unfortunately). If you really need bleeding performance then you could put together some assembly code using those instructions (and do log base conversion yourself). – Dai Nov 23 '16 at 08:51
  • @LưuVĩnhPhúc Yes, approximation can be OK, i'm not sure how much precision is needed for now, I will have to make some tests. – alpereira7 Nov 23 '16 at 08:53
  • 2
    [Fast Approximate Logarithm, Exponential, Power, and Inverse Root](http://www.machinedlearnings.com/2011/06/fast-approximate-logarithm-exponential.html), [Fast Approximate Logarithms](http://www.ebaytechblog.com/2015/05/01/fast-approximate-logarithms-part-i-the-basics/), [fast log and exp functions for x86/x64 SSE](https://github.com/herumi/fmath), [Simple SSE and SSE2 (and now NEON) optimized sin, cos, log and exp](http://gruntthepeon.free.fr/ssemath/) – phuclv Nov 23 '16 at 09:14
  • @Dai only x87 has instructions for `log`, but those are very slow compared to optimized SIMD version. There's no `log` in SSE/AVX. But if one has `log2`, it's very easy to obtain `log10(x)` which is `log2(x)/log2(10)` – phuclv Nov 23 '16 at 09:19
  • 1
    what platform is this for? what does it mean faster? do you have some code for comparison with measured times? In most cases in a rookie code the bottleneck is elsewhere like heap/stack trashing , reallocations , CACHE problems etc... What kind of numbers are you dealing with IEEE floats (32/64/80 bits) or something else (as you have no math.h then it could be anything like fixed point)? Not sure LUT is the way for log due to precision problems. – Spektre Nov 24 '16 at 07:43
  • @LưuVĩnhPhúc, do the implementations described on your links need special libraries ? – alpereira7 Nov 25 '16 at 16:15
  • 2
    I don't see any reason they should use an external library. Those are their own custom libraries which implement their specific calculations without any special types needed. But to be sure why don't just look at their implementations? math.h's `log10` calculates the full precision `log`, therefore it's always slower than approximations. So as I said what's important is how many bits of precision you need. Because a 10-bit approximation will be faster than a 20-bit approximation (example: [`rsqrt` vs `sqrt`](http://stackoverflow.com/q/1528727/995714)) – phuclv Nov 30 '16 at 03:52

1 Answers1

2

What about a lookup table. There is no faster "calculation".

You should provide more details about your problem. Do you neet a generic log10 function? Is there a fixed value range? Do you neet it for floating point numbers or integer arithmetic?

If you need a generic aproach function for floating points it will be quite hard to be faster than the log10 function in the standard library. Maybe fixed point arithmetic is an alternative? There are plenty of free fixed point implementations out there.

This side provides a nice overview of ways to calculate a logarithmic function numerically: http://yacas.sourceforge.net/Algochapter5.html#c5s3

Jonny Schubert
  • 1,357
  • 2
  • 17
  • 35