1570af302Sopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/src/e_rem_pio2f.c */ 2570af302Sopenharmony_ci/* 3570af302Sopenharmony_ci * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. 4570af302Sopenharmony_ci * Debugged and optimized by Bruce D. Evans. 5570af302Sopenharmony_ci */ 6570af302Sopenharmony_ci/* 7570af302Sopenharmony_ci * ==================================================== 8570af302Sopenharmony_ci * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 9570af302Sopenharmony_ci * 10570af302Sopenharmony_ci * Developed at SunPro, a Sun Microsystems, Inc. business. 11570af302Sopenharmony_ci * Permission to use, copy, modify, and distribute this 12570af302Sopenharmony_ci * software is freely granted, provided that this notice 13570af302Sopenharmony_ci * is preserved. 14570af302Sopenharmony_ci * ==================================================== 15570af302Sopenharmony_ci */ 16570af302Sopenharmony_ci/* __rem_pio2f(x,y) 17570af302Sopenharmony_ci * 18570af302Sopenharmony_ci * return the remainder of x rem pi/2 in *y 19570af302Sopenharmony_ci * use double precision for everything except passing x 20570af302Sopenharmony_ci * use __rem_pio2_large() for large x 21570af302Sopenharmony_ci */ 22570af302Sopenharmony_ci 23570af302Sopenharmony_ci#include "libm.h" 24570af302Sopenharmony_ci 25570af302Sopenharmony_ci#if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 26570af302Sopenharmony_ci#define EPS DBL_EPSILON 27570af302Sopenharmony_ci#elif FLT_EVAL_METHOD==2 28570af302Sopenharmony_ci#define EPS LDBL_EPSILON 29570af302Sopenharmony_ci#endif 30570af302Sopenharmony_ci 31570af302Sopenharmony_ci/* 32570af302Sopenharmony_ci * invpio2: 53 bits of 2/pi 33570af302Sopenharmony_ci * pio2_1: first 25 bits of pi/2 34570af302Sopenharmony_ci * pio2_1t: pi/2 - pio2_1 35570af302Sopenharmony_ci */ 36570af302Sopenharmony_cistatic const double 37570af302Sopenharmony_citoint = 1.5/EPS, 38570af302Sopenharmony_cipio4 = 0x1.921fb6p-1, 39570af302Sopenharmony_ciinvpio2 = 6.36619772367581382433e-01, /* 0x3FE45F30, 0x6DC9C883 */ 40570af302Sopenharmony_cipio2_1 = 1.57079631090164184570e+00, /* 0x3FF921FB, 0x50000000 */ 41570af302Sopenharmony_cipio2_1t = 1.58932547735281966916e-08; /* 0x3E5110b4, 0x611A6263 */ 42570af302Sopenharmony_ci 43570af302Sopenharmony_ciint __rem_pio2f(float x, double *y) 44570af302Sopenharmony_ci{ 45570af302Sopenharmony_ci union {float f; uint32_t i;} u = {x}; 46570af302Sopenharmony_ci double tx[1],ty[1]; 47570af302Sopenharmony_ci double_t fn; 48570af302Sopenharmony_ci uint32_t ix; 49570af302Sopenharmony_ci int n, sign, e0; 50570af302Sopenharmony_ci 51570af302Sopenharmony_ci ix = u.i & 0x7fffffff; 52570af302Sopenharmony_ci /* 25+53 bit pi is good enough for medium size */ 53570af302Sopenharmony_ci if (ix < 0x4dc90fdb) { /* |x| ~< 2^28*(pi/2), medium size */ 54570af302Sopenharmony_ci /* Use a specialized rint() to get fn. */ 55570af302Sopenharmony_ci fn = (double_t)x*invpio2 + toint - toint; 56570af302Sopenharmony_ci n = (int32_t)fn; 57570af302Sopenharmony_ci *y = x - fn*pio2_1 - fn*pio2_1t; 58570af302Sopenharmony_ci /* Matters with directed rounding. */ 59570af302Sopenharmony_ci if (predict_false(*y < -pio4)) { 60570af302Sopenharmony_ci n--; 61570af302Sopenharmony_ci fn--; 62570af302Sopenharmony_ci *y = x - fn*pio2_1 - fn*pio2_1t; 63570af302Sopenharmony_ci } else if (predict_false(*y > pio4)) { 64570af302Sopenharmony_ci n++; 65570af302Sopenharmony_ci fn++; 66570af302Sopenharmony_ci *y = x - fn*pio2_1 - fn*pio2_1t; 67570af302Sopenharmony_ci } 68570af302Sopenharmony_ci return n; 69570af302Sopenharmony_ci } 70570af302Sopenharmony_ci if(ix>=0x7f800000) { /* x is inf or NaN */ 71570af302Sopenharmony_ci *y = x-x; 72570af302Sopenharmony_ci return 0; 73570af302Sopenharmony_ci } 74570af302Sopenharmony_ci /* scale x into [2^23, 2^24-1] */ 75570af302Sopenharmony_ci sign = u.i>>31; 76570af302Sopenharmony_ci e0 = (ix>>23) - (0x7f+23); /* e0 = ilogb(|x|)-23, positive */ 77570af302Sopenharmony_ci u.i = ix - (e0<<23); 78570af302Sopenharmony_ci tx[0] = u.f; 79570af302Sopenharmony_ci n = __rem_pio2_large(tx,ty,e0,1,0); 80570af302Sopenharmony_ci if (sign) { 81570af302Sopenharmony_ci *y = -ty[0]; 82570af302Sopenharmony_ci return -n; 83570af302Sopenharmony_ci } 84570af302Sopenharmony_ci *y = ty[0]; 85570af302Sopenharmony_ci return n; 86570af302Sopenharmony_ci} 87