OpenWareLaboratory
fastlog.c
Go to the documentation of this file.
1 /* Lookup table generator for the ICSI logarithm function
2  Copyright (C) 2007 International Computer Science Institute
3  1947 Center Street. Suite 600
4  Berkeley, CA 94704
5  Contact information:
6  Oriol Vinyals vinyals@icsi.berkeley.edu
7  Gerald Friedland fractor@icsi.berkeley.edu
8  Acknowledgements:
9  Thanks to Harrison Ainsworth (hxa7241@gmail.com) for his idea that
10  doubled the accuracy.
11  This program is free software; you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation; either version 2 of the License, or
14  (at your option) any later version.
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19  You should have received a copy of the GNU General Public License along
20  with this program; if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23 #include "fastlog.h"
24 #include <math.h>
25 
26 /* Creates the ICSILog lookup table. Must be called
27  once before any call to icsi_log().
28  n is the number of bits to be taken from the mantissa
29  (0<=n<=23)
30  lookup_table is a pointer to a floating point array of 2^n positions.
31 */
32 void fill_icsi_log_table(float* lookup_table, const uint32_t precision){
33  /* step along table elements and x-axis positions
34  (start with extra half increment, so the steps intersect at their midpoints.) */
35  float oneToTwo = 1.0f + (1.0f / (float)( 1 <<(precision + 1) ));
36  int32_t i;
37  for(i = 0; i < (1 << precision); ++i){
38  // make y-axis value for table element
39  lookup_table[i] = logf(oneToTwo) / 0.69314718055995f;
40  oneToTwo += 1.0f / (float)( 1 << precision );
41  }
42 }
43 
44 float icsi_log(float arg, const float* lookup_table, const uint32_t precision){
45  /* get access to float bits */
46  register const int32_t* const pVal = (const int32_t*)(&arg);
47  /* extract exponent and mantissa (quantized) */
48  register const int32_t exp = ((*pVal >> 23) & 255) - 127;
49  register const int32_t man = (*pVal & 0x7FFFFF) >> (23 - precision);
50  /* exponent plus lookup refinement */
51  return ((float)(exp) + lookup_table[man]) * 0.69314718055995f;
52 }
float icsi_log(float arg, const float *lookup_table, const uint32_t precision)
Definition: fastlog.c:44
void fill_icsi_log_table(float *lookup_table, const uint32_t precision)
Definition: fastlog.c:32