1 /* Single-precision log2 function.
2    Copyright (c) 2017 Arm Ltd.  All rights reserved.
3 
4    SPDX-License-Identifier: BSD-3-Clause
5 
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9    1. Redistributions of source code must retain the above copyright
10       notice, this list of conditions and the following disclaimer.
11    2. Redistributions in binary form must reproduce the above copyright
12       notice, this list of conditions and the following disclaimer in the
13       documentation and/or other materials provided with the distribution.
14    3. The name of the company may not be used to endorse or promote
15       products derived from this software without specific prior written
16       permission.
17 
18    THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
19    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21    IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23    TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
28 
29 #include "fdlibm.h"
30 #if !__OBSOLETE_MATH_FLOAT
31 
32 #include <math.h>
33 #include <stdint.h>
34 #include "math_config.h"
35 
36 /*
37 LOG2F_TABLE_BITS = 4
38 LOG2F_POLY_ORDER = 4
39 
40 ULP error: 0.752 (nearest rounding.)
41 Relative error: 1.9 * 2^-26 (before rounding.)
42 */
43 
44 #define N (1 << LOG2F_TABLE_BITS)
45 #define T __log2f_data.tab
46 #define A __log2f_data.poly
47 #define OFF 0x3f330000
48 
49 float
log2f(float x)50 log2f (float x)
51 {
52   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
53   double_t z, r, r2, p, y, y0, invc, logc;
54   uint32_t ix, iz, top, tmp;
55   int k, i;
56 
57   ix = asuint (x);
58 #if WANT_ROUNDING
59   /* Fix sign of zero with downward rounding when x==1.  */
60   if (__builtin_expect (ix == 0x3f800000, 0))
61     return 0;
62 #endif
63   if (__builtin_expect (ix - 0x00800000 >= 0x7f800000 - 0x00800000, 0))
64     {
65       /* x < 0x1p-126 or inf or nan.  */
66       if (ix * 2 == 0)
67 	return __math_divzerof (1);
68       if (ix == 0x7f800000) /* log2(inf) == inf.  */
69 	return x;
70       if ((ix & 0x80000000) || ix * 2 >= 0xff000000)
71 	return __math_invalidf (x);
72       /* x is subnormal, normalize it.  */
73       ix = asuint (x * 0x1p23f);
74       ix -= (uint32_t) 23 << 23;
75     }
76 
77   /* x = 2^k z; where z is in range [OFF,2*OFF] and exact.
78      The range is split into N subintervals.
79      The ith subinterval contains z and c is near its center.  */
80   tmp = ix - OFF;
81   i = (tmp >> (23 - LOG2F_TABLE_BITS)) % N;
82   top = tmp & 0xff800000;
83   iz = ix - top;
84   k = (int32_t) tmp >> 23; /* arithmetic shift */
85   invc = T[i].invc;
86   logc = T[i].logc;
87   z = (double_t) asfloat (iz);
88 
89   /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */
90   r = z * invc - 1;
91   y0 = logc + (double_t) k;
92 
93   /* Pipelined polynomial evaluation to approximate log1p(r)/ln2.  */
94   r2 = r * r;
95   y = A[1] * r + A[2];
96   y = A[0] * r2 + y;
97   p = A[3] * r + y0;
98   y = y * r2 + p;
99   return (float) y;
100 }
101 #endif /* !__OBSOLETE_MATH_FLOAT */
102