1144091Sdas/* @(#)e_fmod.c 1.3 95/01/18 */ 2144091Sdas/*- 3144091Sdas * ==================================================== 4144091Sdas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5144091Sdas * 6144091Sdas * Developed at SunSoft, a Sun Microsystems, Inc. business. 7144091Sdas * Permission to use, copy, modify, and distribute this 8144091Sdas * software is freely granted, provided that this notice 9144091Sdas * is preserved. 10144091Sdas * ==================================================== 11144091Sdas */ 12144091Sdas 13144091Sdas#include <sys/cdefs.h> 14144091Sdas__FBSDID("$FreeBSD$"); 15144091Sdas 16144091Sdas#include "math.h" 17144091Sdas#include "math_private.h" 18144091Sdas 19144091Sdasstatic const float Zero[] = {0.0, -0.0,}; 20144091Sdas 21144091Sdas/* 22144091Sdas * Return the IEEE remainder and set *quo to the last n bits of the 23144091Sdas * quotient, rounded to the nearest integer. We choose n=31 because 24144091Sdas * we wind up computing all the integer bits of the quotient anyway as 25144091Sdas * a side-effect of computing the remainder by the shift and subtract 26144091Sdas * method. In practice, this is far more bits than are needed to use 27144091Sdas * remquo in reduction algorithms. 28144091Sdas */ 29144091Sdasfloat 30144091Sdasremquof(float x, float y, int *quo) 31144091Sdas{ 32144091Sdas int32_t n,hx,hy,hz,ix,iy,sx,i; 33144091Sdas u_int32_t q,sxy; 34144091Sdas 35144091Sdas GET_FLOAT_WORD(hx,x); 36144091Sdas GET_FLOAT_WORD(hy,y); 37144091Sdas sxy = (hx ^ hy) & 0x80000000; 38144091Sdas sx = hx&0x80000000; /* sign of x */ 39144091Sdas hx ^=sx; /* |x| */ 40144091Sdas hy &= 0x7fffffff; /* |y| */ 41144091Sdas 42144091Sdas /* purge off exception values */ 43144091Sdas if(hy==0||hx>=0x7f800000||hy>0x7f800000) /* y=0,NaN;or x not finite */ 44144091Sdas return (x*y)/(x*y); 45144091Sdas if(hx<hy) { 46144091Sdas q = 0; 47144091Sdas goto fixup; /* |x|<|y| return x or x-y */ 48144091Sdas } else if(hx==hy) { 49233973Sdas *quo = (sxy ? -1 : 1); 50144091Sdas return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/ 51144091Sdas } 52144091Sdas 53144091Sdas /* determine ix = ilogb(x) */ 54144091Sdas if(hx<0x00800000) { /* subnormal x */ 55144091Sdas for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1; 56144091Sdas } else ix = (hx>>23)-127; 57144091Sdas 58144091Sdas /* determine iy = ilogb(y) */ 59144091Sdas if(hy<0x00800000) { /* subnormal y */ 60144091Sdas for (iy = -126,i=(hy<<8); i>0; i<<=1) iy -=1; 61144091Sdas } else iy = (hy>>23)-127; 62144091Sdas 63144091Sdas /* set up {hx,lx}, {hy,ly} and align y to x */ 64144091Sdas if(ix >= -126) 65144091Sdas hx = 0x00800000|(0x007fffff&hx); 66144091Sdas else { /* subnormal x, shift x to normal */ 67144091Sdas n = -126-ix; 68144091Sdas hx <<= n; 69144091Sdas } 70144091Sdas if(iy >= -126) 71144091Sdas hy = 0x00800000|(0x007fffff&hy); 72144091Sdas else { /* subnormal y, shift y to normal */ 73144091Sdas n = -126-iy; 74144091Sdas hy <<= n; 75144091Sdas } 76144091Sdas 77144091Sdas /* fix point fmod */ 78144091Sdas n = ix - iy; 79144091Sdas q = 0; 80144091Sdas while(n--) { 81144091Sdas hz=hx-hy; 82144091Sdas if(hz<0) hx = hx << 1; 83144091Sdas else {hx = hz << 1; q++;} 84144091Sdas q <<= 1; 85144091Sdas } 86144091Sdas hz=hx-hy; 87144091Sdas if(hz>=0) {hx=hz;q++;} 88144091Sdas 89144091Sdas /* convert back to floating value and restore the sign */ 90144091Sdas if(hx==0) { /* return sign(x)*0 */ 91233973Sdas q &= 0x7fffffff; 92144091Sdas *quo = (sxy ? -q : q); 93144091Sdas return Zero[(u_int32_t)sx>>31]; 94144091Sdas } 95144091Sdas while(hx<0x00800000) { /* normalize x */ 96144091Sdas hx <<= 1; 97144091Sdas iy -= 1; 98144091Sdas } 99144091Sdas if(iy>= -126) { /* normalize output */ 100144091Sdas hx = ((hx-0x00800000)|((iy+127)<<23)); 101144091Sdas } else { /* subnormal output */ 102144091Sdas n = -126 - iy; 103144091Sdas hx >>= n; 104144091Sdas } 105144091Sdasfixup: 106144091Sdas SET_FLOAT_WORD(x,hx); 107144091Sdas y = fabsf(y); 108144091Sdas if (y < 0x1p-125f) { 109144091Sdas if (x+x>y || (x+x==y && (q & 1))) { 110144091Sdas q++; 111144091Sdas x-=y; 112144091Sdas } 113144091Sdas } else if (x>0.5f*y || (x==0.5f*y && (q & 1))) { 114144091Sdas q++; 115144091Sdas x-=y; 116144091Sdas } 117144091Sdas GET_FLOAT_WORD(hx,x); 118144091Sdas SET_FLOAT_WORD(x,hx^sx); 119144091Sdas q &= 0x7fffffff; 120144091Sdas *quo = (sxy ? -q : q); 121144091Sdas return x; 122144091Sdas} 123