/* 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>21#include "libm.h"2223static const unsigned24B1 = 709958130, /* B1 = (127-127.0/3-0.03306235651)*2**23 */25B2 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */2627float __cdecl cbrtf(float x)28{29double_t r,T;30union {float f; uint32_t i;} u = {x};31uint32_t hx = u.i & 0x7fffffff;3233if (hx >= 0x7f800000) /* cbrt(NaN,INF) is itself */34return x + x;3536/* rough cbrt to 5 bits */37if (hx < 0x00800000) { /* zero or subnormal? */38if (hx == 0)39return x; /* cbrt(+-0) is itself */40u.f = x*0x1p24f;41hx = u.i & 0x7fffffff;42hx = hx/3 + B2;43} else44hx = hx/3 + B1;45u.i &= 0x80000000;46u.i |= hx;4748/*49* First step Newton iteration (solving t*t-x/t == 0) to 16 bits. In50* double precision so that its terms can be arranged for efficiency51* without causing overflow or underflow.52*/53T = u.f;54r = T*T*T;55T = T*((double_t)x+x+r)/(x+r+r);5657/*58* Second step Newton iteration to 47 bits. In double precision for59* efficiency and accuracy.60*/61r = T*T*T;62T = T*((double_t)x+x+r)/(x+r+r);6364/* rounding to 24 bits is perfect in round-to-nearest mode */65return T;66}676869