source: trunk/libs/newlib/src/newlib/libm/common/sf_log2.c @ 620

Last change on this file since 620 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

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