Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/musl/src/math/cbrtf.c
4397 views
1
/* origin: FreeBSD /usr/src/lib/msun/src/s_cbrtf.c */
2
/*
3
* Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
4
* Debugged and optimized by Bruce D. Evans.
5
*/
6
/*
7
* ====================================================
8
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9
*
10
* Developed at SunPro, a Sun Microsystems, Inc. business.
11
* Permission to use, copy, modify, and distribute this
12
* software is freely granted, provided that this notice
13
* is preserved.
14
* ====================================================
15
*/
16
/* cbrtf(x)
17
* Return cube root of x
18
*/
19
20
#include <math.h>
21
#include <stdint.h>
22
#include "libm.h"
23
24
static const unsigned
25
B1 = 709958130, /* B1 = (127-127.0/3-0.03306235651)*2**23 */
26
B2 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */
27
28
float __cdecl cbrtf(float x)
29
{
30
double_t r,T;
31
union {float f; uint32_t i;} u = {x};
32
uint32_t hx = u.i & 0x7fffffff;
33
34
if (hx >= 0x7f800000) /* cbrt(NaN,INF) is itself */
35
return x + x;
36
37
/* rough cbrt to 5 bits */
38
if (hx < 0x00800000) { /* zero or subnormal? */
39
if (hx == 0)
40
return x; /* cbrt(+-0) is itself */
41
u.f = x*0x1p24f;
42
hx = u.i & 0x7fffffff;
43
hx = hx/3 + B2;
44
} else
45
hx = hx/3 + B1;
46
u.i &= 0x80000000;
47
u.i |= hx;
48
49
/*
50
* First step Newton iteration (solving t*t-x/t == 0) to 16 bits. In
51
* double precision so that its terms can be arranged for efficiency
52
* without causing overflow or underflow.
53
*/
54
T = u.f;
55
r = T*T*T;
56
T = T*((double_t)x+x+r)/(x+r+r);
57
58
/*
59
* Second step Newton iteration to 47 bits. In double precision for
60
* efficiency and accuracy.
61
*/
62
r = T*T*T;
63
T = T*((double_t)x+x+r)/(x+r+r);
64
65
/* rounding to 24 bits is perfect in round-to-nearest mode */
66
return T;
67
}
68
69