11.1Schristos/* From: @(#)e_rem_pio2.c 1.4 95/01/18 */ 21.1Schristos/* 31.1Schristos * ==================================================== 41.1Schristos * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 51.1Schristos * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans. 61.1Schristos * 71.1Schristos * Developed at SunSoft, a Sun Microsystems, Inc. business. 81.1Schristos * Permission to use, copy, modify, and distribute this 91.2Sriastrad * software is freely granted, provided that this notice 101.1Schristos * is preserved. 111.1Schristos * ==================================================== 121.1Schristos * 131.1Schristos * Optimized by Bruce D. Evans. 141.1Schristos */ 151.1Schristos 161.1Schristos#include <sys/cdefs.h> 171.1Schristos#if 0 181.1Schristos__FBSDID("$FreeBSD: head/lib/msun/ld80/e_rem_pio2l.h 336545 2018-07-20 12:42:24Z bde $"); 191.1Schristos#endif 201.1Schristos 211.1Schristos/* ld80 version of __ieee754_rem_pio2l(x,y) 221.2Sriastrad * 231.2Sriastrad * return the remainder of x rem pi/2 in y[0]+y[1] 241.1Schristos * use __kernel_rem_pio2() 251.1Schristos */ 261.1Schristos 271.1Schristos#include <float.h> 281.1Schristos#include <machine/ieee.h> 291.1Schristos 301.1Schristos#include "math.h" 311.1Schristos#include "math_private.h" 321.1Schristos 331.1Schristos#define BIAS (LDBL_MAX_EXP - 1) 341.1Schristos 351.1Schristos/* 361.1Schristos * invpio2: 64 bits of 2/pi 371.1Schristos * pio2_1: first 39 bits of pi/2 381.1Schristos * pio2_1t: pi/2 - pio2_1 391.1Schristos * pio2_2: second 39 bits of pi/2 401.1Schristos * pio2_2t: pi/2 - (pio2_1+pio2_2) 411.1Schristos * pio2_3: third 39 bits of pi/2 421.1Schristos * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3) 431.1Schristos */ 441.1Schristos 451.1Schristosstatic const double 461.1Schristoszero = 0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */ 471.1Schristostwo24 = 1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */ 481.1Schristospio2_1 = 1.57079632679597125389e+00, /* 0x3FF921FB, 0x54444000 */ 491.1Schristospio2_2 = -1.07463465549783099519e-12, /* -0x12e7b967674000.0p-92 */ 501.1Schristospio2_3 = 6.36831716351370313614e-25; /* 0x18a2e037074000.0p-133 */ 511.1Schristos 521.1Schristos#if defined(__amd64__) || defined(__i386__) 531.1Schristos/* Long double constants are slow on these arches, and broken on i386. */ 541.1Schristosstatic const volatile double 551.1Schristosinvpio2hi = 6.3661977236758138e-01, /* 0x145f306dc9c883.0p-53 */ 561.1Schristosinvpio2lo = -3.9356538861223811e-17, /* -0x16b00000000000.0p-107 */ 571.1Schristospio2_1thi = -1.0746346554971943e-12, /* -0x12e7b9676733af.0p-92 */ 581.1Schristospio2_1tlo = 8.8451028997905949e-29, /* 0x1c080000000000.0p-146 */ 591.1Schristospio2_2thi = 6.3683171635109499e-25, /* 0x18a2e03707344a.0p-133 */ 601.1Schristospio2_2tlo = 2.3183081793789774e-41, /* 0x10280000000000.0p-187 */ 611.1Schristospio2_3thi = -2.7529965190440717e-37, /* -0x176b7ed8fbbacc.0p-174 */ 621.1Schristospio2_3tlo = -4.2006647512740502e-54; /* -0x19c00000000000.0p-230 */ 631.1Schristos#define invpio2 ((long double)invpio2hi + invpio2lo) 641.1Schristos#define pio2_1t ((long double)pio2_1thi + pio2_1tlo) 651.1Schristos#define pio2_2t ((long double)pio2_2thi + pio2_2tlo) 661.1Schristos#define pio2_3t ((long double)pio2_3thi + pio2_3tlo) 671.1Schristos#else 681.1Schristosstatic const long double 691.1Schristosinvpio2 = 6.36619772367581343076e-01L, /* 0xa2f9836e4e44152a.0p-64 */ 701.1Schristospio2_1t = -1.07463465549719416346e-12L, /* -0x973dcb3b399d747f.0p-103 */ 711.1Schristospio2_2t = 6.36831716351095013979e-25L, /* 0xc51701b839a25205.0p-144 */ 721.1Schristospio2_3t = -2.75299651904407171810e-37L; /* -0xbb5bf6c7ddd660ce.0p-185 */ 731.1Schristos#endif 741.1Schristos 751.1Schristosstatic inline __always_inline int 761.1Schristos__ieee754_rem_pio2l(long double x, long double *y) 771.1Schristos{ 781.1Schristos union ieee_ext_u u, u1; 791.1Schristos long double z,w,t,r,fn; 801.1Schristos double tx[3],ty[2]; 811.1Schristos int e0,ex,i,j,nx,n; 821.1Schristos int16_t expsign, expsign1; 831.1Schristos 841.1Schristos u.extu_ld = x; 851.1Schristos ex = u.extu_exp; 861.3Schristos expsign = GET_EXPSIGN(&u); 871.1Schristos if (ex < BIAS + 25 || (ex == BIAS + 25 && u.extu_frach < 0xc90fdaa2U)) { 881.1Schristos /* |x| ~< 2^25*(pi/2), medium size */ 891.1Schristos fn = rnintl(x*invpio2); 901.1Schristos n = irint(fn); 911.1Schristos r = x-fn*pio2_1; 921.1Schristos w = fn*pio2_1t; /* 1st round good to 102 bit */ 931.1Schristos { 941.1Schristos union ieee_ext_u u2; 951.1Schristos int ex1; 961.1Schristos j = ex; 971.2Sriastrad y[0] = r-w; 981.1Schristos u2.extu_ld = y[0]; 991.1Schristos ex1 = u2.extu_exp; 1001.1Schristos i = j-ex1; 1011.1Schristos if(i>22) { /* 2nd iteration needed, good to 141 */ 1021.1Schristos t = r; 1031.2Sriastrad w = fn*pio2_2; 1041.1Schristos r = t-w; 1051.2Sriastrad w = fn*pio2_2t-((t-r)-w); 1061.1Schristos y[0] = r-w; 1071.1Schristos u2.extu_ld = y[0]; 1081.1Schristos ex1 = u2.extu_exp; 1091.1Schristos i = j-ex1; 1101.1Schristos if(i>61) { /* 3rd iteration need, 180 bits acc */ 1111.2Sriastrad t = r; /* will cover all possible cases */ 1121.2Sriastrad w = fn*pio2_3; 1131.2Sriastrad r = t-w; 1141.2Sriastrad w = fn*pio2_3t-((t-r)-w); 1151.2Sriastrad y[0] = r-w; 1161.1Schristos } 1171.1Schristos } 1181.1Schristos } 1191.1Schristos y[1] = (r-y[0])-w; 1201.1Schristos return n; 1211.1Schristos } 1221.2Sriastrad /* 1231.1Schristos * all other (large) arguments 1241.1Schristos */ 1251.1Schristos if(ex==0x7fff) { /* x is inf or NaN */ 1261.1Schristos y[0]=y[1]=x-x; return 0; 1271.1Schristos } 1281.1Schristos /* set z = scalbn(|x|,ilogb(x)-23) */ 1291.1Schristos u1.extu_ld = x; 1301.1Schristos e0 = ex - BIAS - 23; /* e0 = ilogb(|x|)-23; */ 1311.1Schristos expsign1 = ex - e0; 1321.3Schristos SET_EXPSIGN(&u1, expsign1); 1331.1Schristos z = u1.extu_ld; 1341.1Schristos for(i=0;i<2;i++) { 1351.1Schristos tx[i] = (double)((int32_t)(z)); 1361.1Schristos z = (z-tx[i])*two24; 1371.1Schristos } 1381.1Schristos tx[2] = z; 1391.1Schristos nx = 3; 1401.1Schristos while(tx[nx-1]==zero) nx--; /* skip zero term */ 1411.1Schristos n = __kernel_rem_pio2(tx,ty,e0,nx,2); 1421.1Schristos r = (long double)ty[0] + ty[1]; 1431.1Schristos w = ty[1] - (r - ty[0]); 1441.1Schristos if(expsign<0) {y[0] = -r; y[1] = -w; return -n;} 1451.1Schristos y[0] = r; y[1] = w; return n; 1461.1Schristos} 147