/* origin: FreeBSD /usr/src/lib/msun/src/s_cbrtf.c */1/*2* Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].3* Debugged and optimized by Bruce D. Evans.4*/5/*6* ====================================================7* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.8*9* Developed at SunPro, a Sun Microsystems, Inc. business.10* Permission to use, copy, modify, and distribute this11* software is freely granted, provided that this notice12* is preserved.13* ====================================================14*/15/* cbrtf(x)16* Return cube root of x17*/1819#include <math.h>20#include <stdint.h>2122static const unsigned23B1 = 709958130, /* B1 = (127-127.0/3-0.03306235651)*2**23 */24B2 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */2526float cbrtf(float x)27{28double_t r,T;29union {float f; uint32_t i;} u = {x};30uint32_t hx = u.i & 0x7fffffff;3132if (hx >= 0x7f800000) /* cbrt(NaN,INF) is itself */33return x + x;3435/* rough cbrt to 5 bits */36if (hx < 0x00800000) { /* zero or subnormal? */37if (hx == 0)38return x; /* cbrt(+-0) is itself */39u.f = x*0x1p24f;40hx = u.i & 0x7fffffff;41hx = hx/3 + B2;42} else43hx = hx/3 + B1;44u.i &= 0x80000000;45u.i |= hx;4647/*48* First step Newton iteration (solving t*t-x/t == 0) to 16 bits. In49* double precision so that its terms can be arranged for efficiency50* without causing overflow or underflow.51*/52T = u.f;53r = T*T*T;54T = T*((double_t)x+x+r)/(x+r+r);5556/*57* Second step Newton iteration to 47 bits. In double precision for58* efficiency and accuracy.59*/60r = T*T*T;61T = T*((double_t)x+x+r)/(x+r+r);6263/* rounding to 24 bits is perfect in round-to-nearest mode */64return T;65}666768