Coverage Report

Created: 2026-01-29 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/tools/fdlibm/s_cbrt.c
Line
Count
Source
1
/* @(#)s_cbrt.c 5.1 93/09/24 */
2
/*
3
 * ====================================================
4
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * Developed at SunPro, a Sun Microsystems, Inc. business.
7
 * Permission to use, copy, modify, and distribute this
8
 * software is freely granted, provided that this notice
9
 * is preserved.
10
 * ====================================================
11
 *
12
 * Optimized by Bruce D. Evans.
13
 */
14
15
16
#include "math_private.h"
17
18
/* cbrt(x)
19
 * Return cube root of x
20
 */
21
static const u_int32_t
22
  B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */
23
  B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */
24
25
/* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */
26
static const double
27
P0 =  1.87595182427177009643,   /* 0x3ffe03e6, 0x0f61e692 */
28
P1 = -1.88497979543377169875,   /* 0xbffe28e0, 0x92f02420 */
29
P2 =  1.621429720105354466140,    /* 0x3ff9f160, 0x4a49d6c2 */
30
P3 = -0.758397934778766047437,    /* 0xbfe844cb, 0xbee751d9 */
31
P4 =  0.145996192886612446982;    /* 0x3fc2b000, 0xd4e4edd7 */
32
33
double
34
s_cbrt(double x)
35
11
{
36
11
  int32_t hx;
37
11
  union {
38
11
      double value;
39
11
      u_int64_t bits;
40
11
  } u;
41
11
  double r,s,t=0.0,w;
42
11
  u_int32_t sign;
43
11
  u_int32_t high,low;
44
45
11
  EXTRACT_WORDS(hx,low,x);
46
11
  sign=hx&0x80000000;     /* sign= sign(x) */
47
11
  hx  ^=sign;
48
11
  if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
49
50
    /*
51
     * Rough cbrt to 5 bits:
52
     *    cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3)
53
     * where e is integral and >= 0, m is real and in [0, 1), and "/" and
54
     * "%" are integer division and modulus with rounding towards minus
55
     * infinity.  The RHS is always >= the LHS and has a maximum relative
56
     * error of about 1 in 16.  Adding a bias of -0.03306235651 to the
57
     * (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE
58
     * floating point representation, for finite positive normal values,
59
     * ordinary integer divison of the value in bits magically gives
60
     * almost exactly the RHS of the above provided we first subtract the
61
     * exponent bias (1023 for doubles) and later add it back.  We do the
62
     * subtraction virtually to keep e >= 0 so that ordinary integer
63
     * division rounds towards minus infinity; this is also efficient.
64
     */
65
2
  if(hx<0x00100000) {     /* zero or subnormal? */
66
2
      if((hx|low)==0)
67
2
    return(x);    /* cbrt(0) is itself */
68
0
      SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */
69
0
      t*=x;
70
0
      GET_HIGH_WORD(high,t);
71
0
      INSERT_WORDS(t,sign|((high&0x7fffffff)/3+B2),0);
72
0
  } else
73
0
      INSERT_WORDS(t,sign|(hx/3+B1),0);
74
75
    /*
76
     * New cbrt to 23 bits:
77
     *    cbrt(x) = t*cbrt(x/t**3) ~= t*P(t**3/x)
78
     * where P(r) is a polynomial of degree 4 that approximates 1/cbrt(r)
79
     * to within 2**-23.5 when |r - 1| < 1/10.  The rough approximation
80
     * has produced t such than |t/cbrt(x) - 1| ~< 1/32, and cubing this
81
     * gives us bounds for r = t**3/x.
82
     *
83
     * Try to optimize for parallel evaluation as in k_tanf.c.
84
     */
85
0
  r=(t*t)*(t/x);
86
0
  t=t*((P0+r*(P1+r*P2))+((r*r)*r)*(P3+r*P4));
87
88
    /*
89
     * Round t away from zero to 23 bits (sloppily except for ensuring that
90
     * the result is larger in magnitude than cbrt(x) but not much more than
91
     * 2 23-bit ulps larger).  With rounding towards zero, the error bound
92
     * would be ~5/6 instead of ~4/6.  With a maximum error of 2 23-bit ulps
93
     * in the rounded t, the infinite-precision error in the Newton
94
     * approximation barely affects third digit in the final error
95
     * 0.667; the error in the rounded t can be up to about 3 23-bit ulps
96
     * before the final error is larger than 0.667 ulps.
97
     */
98
0
  u.value=t;
99
0
  u.bits=(u.bits+0x80000000)&0xffffffffc0000000ULL;
100
0
  t=u.value;
101
102
    /* one step Newton iteration to 53 bits with error < 0.667 ulps */
103
0
  s=t*t;        /* t*t is exact */
104
0
  r=x/s;        /* error <= 0.5 ulps; |r| < |t| */
105
0
  w=t+t;        /* t+t is exact */
106
0
  r=(r-t)/(w+r);      /* r-t is exact; w+r ~= 3*t */
107
0
  t=t+t*r;      /* error <= 0.5 + 0.5/3 + epsilon */
108
109
0
  return(t);
110
2
}