github.com/afumu/libc@v0.0.6/musl/src/math/log2f.c (about)

     1  /*
     2   * Single-precision log2 function.
     3   *
     4   * Copyright (c) 2017-2018, Arm Limited.
     5   * SPDX-License-Identifier: MIT
     6   */
     7  
     8  #include <math.h>
     9  #include <stdint.h>
    10  #include "libm.h"
    11  #include "log2f_data.h"
    12  
    13  /*
    14  LOG2F_TABLE_BITS = 4
    15  LOG2F_POLY_ORDER = 4
    16  
    17  ULP error: 0.752 (nearest rounding.)
    18  Relative error: 1.9 * 2^-26 (before rounding.)
    19  */
    20  
    21  #define N (1 << LOG2F_TABLE_BITS)
    22  #define T __log2f_data.tab
    23  #define A __log2f_data.poly
    24  #define OFF 0x3f330000
    25  
    26  float log2f(float x)
    27  {
    28  	double_t z, r, r2, p, y, y0, invc, logc;
    29  	uint32_t ix, iz, top, tmp;
    30  	int k, i;
    31  
    32  	ix = asuint(x);
    33  	/* Fix sign of zero with downward rounding when x==1.  */
    34  	if (WANT_ROUNDING && predict_false(ix == 0x3f800000))
    35  		return 0;
    36  	if (predict_false(ix - 0x00800000 >= 0x7f800000 - 0x00800000)) {
    37  		/* x < 0x1p-126 or inf or nan.  */
    38  		if (ix * 2 == 0)
    39  			return __math_divzerof(1);
    40  		if (ix == 0x7f800000) /* log2(inf) == inf.  */
    41  			return x;
    42  		if ((ix & 0x80000000) || ix * 2 >= 0xff000000)
    43  			return __math_invalidf(x);
    44  		/* x is subnormal, normalize it.  */
    45  		ix = asuint(x * 0x1p23f);
    46  		ix -= 23 << 23;
    47  	}
    48  
    49  	/* x = 2^k z; where z is in range [OFF,2*OFF] and exact.
    50  	   The range is split into N subintervals.
    51  	   The ith subinterval contains z and c is near its center.  */
    52  	tmp = ix - OFF;
    53  	i = (tmp >> (23 - LOG2F_TABLE_BITS)) % N;
    54  	top = tmp & 0xff800000;
    55  	iz = ix - top;
    56  	k = (int32_t)tmp >> 23; /* arithmetic shift */
    57  	invc = T[i].invc;
    58  	logc = T[i].logc;
    59  	z = (double_t)asfloat(iz);
    60  
    61  	/* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */
    62  	r = z * invc - 1;
    63  	y0 = logc + (double_t)k;
    64  
    65  	/* Pipelined polynomial evaluation to approximate log1p(r)/ln2.  */
    66  	r2 = r * r;
    67  	y = A[1] * r + A[2];
    68  	y = A[0] * r2 + y;
    69  	p = A[3] * r + y0;
    70  	y = y * r2 + p;
    71  	return eval_as_float(y);
    72  }