Path: blob/master/waterbox/libc/functions/math/__rem_pio2l.c
2 views
/* origin: FreeBSD /usr/src/lib/msun/ld80/e_rem_pio2.c */1/*2* ====================================================3* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.4* Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.5*6* Developed at SunSoft, a Sun Microsystems, Inc. business.7* Permission to use, copy, modify, and distribute this8* software is freely granted, provided that this notice9* is preserved.10* ====================================================11*12* Optimized by Bruce D. Evans.13*/14#include "libm.h"15#if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 1638416/* ld80 and ld128 version of __rem_pio2(x,y)17*18* return the remainder of x rem pi/2 in y[0]+y[1]19* use __rem_pio2_large() for large x20*/2122static const long double toint = 1.5/LDBL_EPSILON;2324#if LDBL_MANT_DIG == 6425/* u ~< 0x1p25*pi/2 */26#define SMALL(u) (((u.i.se & 0x7fffU)<<16 | u.i.m>>48) < ((0x3fff + 25)<<16 | 0x921f>>1 | 0x8000))27#define QUOBITS(x) ((uint32_t)(int32_t)x & 0x7fffffff)28#define ROUND1 2229#define ROUND2 6130#define NX 331#define NY 232/*33* invpio2: 64 bits of 2/pi34* pio2_1: first 39 bits of pi/235* pio2_1t: pi/2 - pio2_136* pio2_2: second 39 bits of pi/237* pio2_2t: pi/2 - (pio2_1+pio2_2)38* pio2_3: third 39 bits of pi/239* pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3)40*/41static const double42pio2_1 = 1.57079632679597125389e+00, /* 0x3FF921FB, 0x54444000 */43pio2_2 = -1.07463465549783099519e-12, /* -0x12e7b967674000.0p-92 */44pio2_3 = 6.36831716351370313614e-25; /* 0x18a2e037074000.0p-133 */45static const long double46invpio2 = 6.36619772367581343076e-01L, /* 0xa2f9836e4e44152a.0p-64 */47pio2_1t = -1.07463465549719416346e-12L, /* -0x973dcb3b399d747f.0p-103 */48pio2_2t = 6.36831716351095013979e-25L, /* 0xc51701b839a25205.0p-144 */49pio2_3t = -2.75299651904407171810e-37L; /* -0xbb5bf6c7ddd660ce.0p-185 */50#elif LDBL_MANT_DIG == 11351/* u ~< 0x1p45*pi/2 */52#define SMALL(u) (((u.i.se & 0x7fffU)<<16 | u.i.top) < ((0x3fff + 45)<<16 | 0x921f))53#define QUOBITS(x) ((uint32_t)(int64_t)x & 0x7fffffff)54#define ROUND1 5155#define ROUND2 11956#define NX 557#define NY 358static const long double59invpio2 = 6.3661977236758134307553505349005747e-01L, /* 0x145f306dc9c882a53f84eafa3ea6a.0p-113 */60pio2_1 = 1.5707963267948966192292994253909555e+00L, /* 0x1921fb54442d18469800000000000.0p-112 */61pio2_1t = 2.0222662487959507323996846200947577e-21L, /* 0x13198a2e03707344a4093822299f3.0p-181 */62pio2_2 = 2.0222662487959507323994779168837751e-21L, /* 0x13198a2e03707344a400000000000.0p-181 */63pio2_2t = 2.0670321098263988236496903051604844e-43L, /* 0x127044533e63a0105df531d89cd91.0p-254 */64pio2_3 = 2.0670321098263988236499468110329591e-43L, /* 0x127044533e63a0105e00000000000.0p-254 */65pio2_3t = -2.5650587247459238361625433492959285e-65L; /* -0x159c4ec64ddaeb5f78671cbfb2210.0p-327 */66#endif6768int __rem_pio2l(long double x, long double *y)69{70union ldshape u,uz;71long double z,w,t,r,fn;72double tx[NX],ty[NY];73int ex,ey,n,i;7475u.f = x;76ex = u.i.se & 0x7fff;77if (SMALL(u)) {78/* rint(x/(pi/2)), Assume round-to-nearest. */79fn = x*invpio2 + toint - toint;80n = QUOBITS(fn);81r = x-fn*pio2_1;82w = fn*pio2_1t; /* 1st round good to 102/180 bits (ld80/ld128) */83y[0] = r-w;84u.f = y[0];85ey = u.i.se & 0x7fff;86if (ex - ey > ROUND1) { /* 2nd iteration needed, good to 141/248 (ld80/ld128) */87t = r;88w = fn*pio2_2;89r = t-w;90w = fn*pio2_2t-((t-r)-w);91y[0] = r-w;92u.f = y[0];93ey = u.i.se & 0x7fff;94if (ex - ey > ROUND2) { /* 3rd iteration, good to 180/316 bits */95t = r; /* will cover all possible cases (not verified for ld128) */96w = fn*pio2_3;97r = t-w;98w = fn*pio2_3t-((t-r)-w);99y[0] = r-w;100}101}102y[1] = (r - y[0]) - w;103return n;104}105/*106* all other (large) arguments107*/108if (ex == 0x7fff) { /* x is inf or NaN */109y[0] = y[1] = x - x;110return 0;111}112/* set z = scalbn(|x|,-ilogb(x)+23) */113uz.f = x;114uz.i.se = 0x3fff + 23;115z = uz.f;116for (i=0; i < NX - 1; i++) {117tx[i] = (double)(int32_t)z;118z = (z-tx[i])*0x1p24;119}120tx[i] = z;121while (tx[i] == 0)122i--;123n = __rem_pio2_large(tx, ty, ex-0x3fff-23, i+1, NY);124w = ty[1];125if (NY == 3)126w += ty[2];127r = ty[0] + w;128/* TODO: for ld128 this does not follow the recommendation of the129comments of __rem_pio2_large which seem wrong if |ty[0]| > |ty[1]+ty[2]| */130w -= r - ty[0];131if (u.i.se >> 15) {132y[0] = -r;133y[1] = -w;134return -n;135}136y[0] = r;137y[1] = w;138return n;139}140#endif141142143