1570af302Sopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/ld80/e_rem_pio2.c */ 2570af302Sopenharmony_ci/* 3570af302Sopenharmony_ci * ==================================================== 4570af302Sopenharmony_ci * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5570af302Sopenharmony_ci * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans. 6570af302Sopenharmony_ci * 7570af302Sopenharmony_ci * Developed at SunSoft, a Sun Microsystems, Inc. business. 8570af302Sopenharmony_ci * Permission to use, copy, modify, and distribute this 9570af302Sopenharmony_ci * software is freely granted, provided that this notice 10570af302Sopenharmony_ci * is preserved. 11570af302Sopenharmony_ci * ==================================================== 12570af302Sopenharmony_ci * 13570af302Sopenharmony_ci * Optimized by Bruce D. Evans. 14570af302Sopenharmony_ci */ 15570af302Sopenharmony_ci#include "libm.h" 16570af302Sopenharmony_ci#if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 17570af302Sopenharmony_ci/* ld80 and ld128 version of __rem_pio2(x,y) 18570af302Sopenharmony_ci * 19570af302Sopenharmony_ci * return the remainder of x rem pi/2 in y[0]+y[1] 20570af302Sopenharmony_ci * use __rem_pio2_large() for large x 21570af302Sopenharmony_ci */ 22570af302Sopenharmony_ci 23570af302Sopenharmony_cistatic const long double toint = 1.5/LDBL_EPSILON; 24570af302Sopenharmony_ci 25570af302Sopenharmony_ci#if LDBL_MANT_DIG == 64 26570af302Sopenharmony_ci/* u ~< 0x1p25*pi/2 */ 27570af302Sopenharmony_ci#define SMALL(u) (((u.i.se & 0x7fffU)<<16 | u.i.m>>48) < ((0x3fff + 25)<<16 | 0x921f>>1 | 0x8000)) 28570af302Sopenharmony_ci#define QUOBITS(x) ((uint32_t)(int32_t)x & 0x7fffffff) 29570af302Sopenharmony_ci#define ROUND1 22 30570af302Sopenharmony_ci#define ROUND2 61 31570af302Sopenharmony_ci#define NX 3 32570af302Sopenharmony_ci#define NY 2 33570af302Sopenharmony_ci/* 34570af302Sopenharmony_ci * invpio2: 64 bits of 2/pi 35570af302Sopenharmony_ci * pio2_1: first 39 bits of pi/2 36570af302Sopenharmony_ci * pio2_1t: pi/2 - pio2_1 37570af302Sopenharmony_ci * pio2_2: second 39 bits of pi/2 38570af302Sopenharmony_ci * pio2_2t: pi/2 - (pio2_1+pio2_2) 39570af302Sopenharmony_ci * pio2_3: third 39 bits of pi/2 40570af302Sopenharmony_ci * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3) 41570af302Sopenharmony_ci */ 42570af302Sopenharmony_cistatic const double 43570af302Sopenharmony_cipio2_1 = 1.57079632679597125389e+00, /* 0x3FF921FB, 0x54444000 */ 44570af302Sopenharmony_cipio2_2 = -1.07463465549783099519e-12, /* -0x12e7b967674000.0p-92 */ 45570af302Sopenharmony_cipio2_3 = 6.36831716351370313614e-25; /* 0x18a2e037074000.0p-133 */ 46570af302Sopenharmony_cistatic const long double 47570af302Sopenharmony_cipio4 = 0x1.921fb54442d1846ap-1L, 48570af302Sopenharmony_ciinvpio2 = 6.36619772367581343076e-01L, /* 0xa2f9836e4e44152a.0p-64 */ 49570af302Sopenharmony_cipio2_1t = -1.07463465549719416346e-12L, /* -0x973dcb3b399d747f.0p-103 */ 50570af302Sopenharmony_cipio2_2t = 6.36831716351095013979e-25L, /* 0xc51701b839a25205.0p-144 */ 51570af302Sopenharmony_cipio2_3t = -2.75299651904407171810e-37L; /* -0xbb5bf6c7ddd660ce.0p-185 */ 52570af302Sopenharmony_ci#elif LDBL_MANT_DIG == 113 53570af302Sopenharmony_ci/* u ~< 0x1p45*pi/2 */ 54570af302Sopenharmony_ci#define SMALL(u) (((u.i.se & 0x7fffU)<<16 | u.i.top) < ((0x3fff + 45)<<16 | 0x921f)) 55570af302Sopenharmony_ci#define QUOBITS(x) ((uint32_t)(int64_t)x & 0x7fffffff) 56570af302Sopenharmony_ci#define ROUND1 51 57570af302Sopenharmony_ci#define ROUND2 119 58570af302Sopenharmony_ci#define NX 5 59570af302Sopenharmony_ci#define NY 3 60570af302Sopenharmony_cistatic const long double 61570af302Sopenharmony_cipio4 = 0x1.921fb54442d18469898cc51701b8p-1L, 62570af302Sopenharmony_ciinvpio2 = 6.3661977236758134307553505349005747e-01L, /* 0x145f306dc9c882a53f84eafa3ea6a.0p-113 */ 63570af302Sopenharmony_cipio2_1 = 1.5707963267948966192292994253909555e+00L, /* 0x1921fb54442d18469800000000000.0p-112 */ 64570af302Sopenharmony_cipio2_1t = 2.0222662487959507323996846200947577e-21L, /* 0x13198a2e03707344a4093822299f3.0p-181 */ 65570af302Sopenharmony_cipio2_2 = 2.0222662487959507323994779168837751e-21L, /* 0x13198a2e03707344a400000000000.0p-181 */ 66570af302Sopenharmony_cipio2_2t = 2.0670321098263988236496903051604844e-43L, /* 0x127044533e63a0105df531d89cd91.0p-254 */ 67570af302Sopenharmony_cipio2_3 = 2.0670321098263988236499468110329591e-43L, /* 0x127044533e63a0105e00000000000.0p-254 */ 68570af302Sopenharmony_cipio2_3t = -2.5650587247459238361625433492959285e-65L; /* -0x159c4ec64ddaeb5f78671cbfb2210.0p-327 */ 69570af302Sopenharmony_ci#endif 70570af302Sopenharmony_ci 71570af302Sopenharmony_ciint __rem_pio2l(long double x, long double *y) 72570af302Sopenharmony_ci{ 73570af302Sopenharmony_ci union ldshape u,uz; 74570af302Sopenharmony_ci long double z,w,t,r,fn; 75570af302Sopenharmony_ci double tx[NX],ty[NY]; 76570af302Sopenharmony_ci int ex,ey,n,i; 77570af302Sopenharmony_ci 78570af302Sopenharmony_ci u.f = x; 79570af302Sopenharmony_ci ex = u.i.se & 0x7fff; 80570af302Sopenharmony_ci if (SMALL(u)) { 81570af302Sopenharmony_ci /* rint(x/(pi/2)) */ 82570af302Sopenharmony_ci fn = x*invpio2 + toint - toint; 83570af302Sopenharmony_ci n = QUOBITS(fn); 84570af302Sopenharmony_ci r = x-fn*pio2_1; 85570af302Sopenharmony_ci w = fn*pio2_1t; /* 1st round good to 102/180 bits (ld80/ld128) */ 86570af302Sopenharmony_ci /* Matters with directed rounding. */ 87570af302Sopenharmony_ci if (predict_false(r - w < -pio4)) { 88570af302Sopenharmony_ci n--; 89570af302Sopenharmony_ci fn--; 90570af302Sopenharmony_ci r = x - fn*pio2_1; 91570af302Sopenharmony_ci w = fn*pio2_1t; 92570af302Sopenharmony_ci } else if (predict_false(r - w > pio4)) { 93570af302Sopenharmony_ci n++; 94570af302Sopenharmony_ci fn++; 95570af302Sopenharmony_ci r = x - fn*pio2_1; 96570af302Sopenharmony_ci w = fn*pio2_1t; 97570af302Sopenharmony_ci } 98570af302Sopenharmony_ci y[0] = r-w; 99570af302Sopenharmony_ci u.f = y[0]; 100570af302Sopenharmony_ci ey = u.i.se & 0x7fff; 101570af302Sopenharmony_ci if (ex - ey > ROUND1) { /* 2nd iteration needed, good to 141/248 (ld80/ld128) */ 102570af302Sopenharmony_ci t = r; 103570af302Sopenharmony_ci w = fn*pio2_2; 104570af302Sopenharmony_ci r = t-w; 105570af302Sopenharmony_ci w = fn*pio2_2t-((t-r)-w); 106570af302Sopenharmony_ci y[0] = r-w; 107570af302Sopenharmony_ci u.f = y[0]; 108570af302Sopenharmony_ci ey = u.i.se & 0x7fff; 109570af302Sopenharmony_ci if (ex - ey > ROUND2) { /* 3rd iteration, good to 180/316 bits */ 110570af302Sopenharmony_ci t = r; /* will cover all possible cases (not verified for ld128) */ 111570af302Sopenharmony_ci w = fn*pio2_3; 112570af302Sopenharmony_ci r = t-w; 113570af302Sopenharmony_ci w = fn*pio2_3t-((t-r)-w); 114570af302Sopenharmony_ci y[0] = r-w; 115570af302Sopenharmony_ci } 116570af302Sopenharmony_ci } 117570af302Sopenharmony_ci y[1] = (r - y[0]) - w; 118570af302Sopenharmony_ci return n; 119570af302Sopenharmony_ci } 120570af302Sopenharmony_ci /* 121570af302Sopenharmony_ci * all other (large) arguments 122570af302Sopenharmony_ci */ 123570af302Sopenharmony_ci if (ex == 0x7fff) { /* x is inf or NaN */ 124570af302Sopenharmony_ci y[0] = y[1] = x - x; 125570af302Sopenharmony_ci return 0; 126570af302Sopenharmony_ci } 127570af302Sopenharmony_ci /* set z = scalbn(|x|,-ilogb(x)+23) */ 128570af302Sopenharmony_ci uz.f = x; 129570af302Sopenharmony_ci uz.i.se = 0x3fff + 23; 130570af302Sopenharmony_ci z = uz.f; 131570af302Sopenharmony_ci for (i=0; i < NX - 1; i++) { 132570af302Sopenharmony_ci tx[i] = (double)(int32_t)z; 133570af302Sopenharmony_ci z = (z-tx[i])*0x1p24; 134570af302Sopenharmony_ci } 135570af302Sopenharmony_ci tx[i] = z; 136570af302Sopenharmony_ci while (tx[i] == 0) 137570af302Sopenharmony_ci i--; 138570af302Sopenharmony_ci n = __rem_pio2_large(tx, ty, ex-0x3fff-23, i+1, NY); 139570af302Sopenharmony_ci w = ty[1]; 140570af302Sopenharmony_ci if (NY == 3) 141570af302Sopenharmony_ci w += ty[2]; 142570af302Sopenharmony_ci r = ty[0] + w; 143570af302Sopenharmony_ci /* TODO: for ld128 this does not follow the recommendation of the 144570af302Sopenharmony_ci comments of __rem_pio2_large which seem wrong if |ty[0]| > |ty[1]+ty[2]| */ 145570af302Sopenharmony_ci w -= r - ty[0]; 146570af302Sopenharmony_ci if (u.i.se >> 15) { 147570af302Sopenharmony_ci y[0] = -r; 148570af302Sopenharmony_ci y[1] = -w; 149570af302Sopenharmony_ci return -n; 150570af302Sopenharmony_ci } 151570af302Sopenharmony_ci y[0] = r; 152570af302Sopenharmony_ci y[1] = w; 153570af302Sopenharmony_ci return n; 154570af302Sopenharmony_ci} 155570af302Sopenharmony_ci#endif 156