Path: blob/master/src/java.base/share/native/libfdlibm/e_fmod.c
41149 views
/*1* Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* __ieee754_fmod(x,y)27* Return x mod y in exact arithmetic28* Method: shift and subtract29*/3031#include "fdlibm.h"3233#ifdef __STDC__34static const double one = 1.0, Zero[] = {0.0, -0.0,};35#else36static double one = 1.0, Zero[] = {0.0, -0.0,};37#endif3839#ifdef __STDC__40double __ieee754_fmod(double x, double y)41#else42double __ieee754_fmod(x,y)43double x,y ;44#endif45{46int n,hx,hy,hz,ix,iy,sx,i;47unsigned lx,ly,lz;4849hx = __HI(x); /* high word of x */50lx = __LO(x); /* low word of x */51hy = __HI(y); /* high word of y */52ly = __LO(y); /* low word of y */53sx = hx&0x80000000; /* sign of x */54hx ^=sx; /* |x| */55hy &= 0x7fffffff; /* |y| */5657/* purge off exception values */58if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */59((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */60return (x*y)/(x*y);61if(hx<=hy) {62if((hx<hy)||(lx<ly)) return x; /* |x|<|y| return x */63if(lx==ly)64return Zero[(unsigned)sx>>31]; /* |x|=|y| return x*0*/65}6667/* determine ix = ilogb(x) */68if(hx<0x00100000) { /* subnormal x */69if(hx==0) {70for (ix = -1043, i=lx; i>0; i<<=1) ix -=1;71} else {72for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1;73}74} else ix = (hx>>20)-1023;7576/* determine iy = ilogb(y) */77if(hy<0x00100000) { /* subnormal y */78if(hy==0) {79for (iy = -1043, i=ly; i>0; i<<=1) iy -=1;80} else {81for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1;82}83} else iy = (hy>>20)-1023;8485/* set up {hx,lx}, {hy,ly} and align y to x */86if(ix >= -1022)87hx = 0x00100000|(0x000fffff&hx);88else { /* subnormal x, shift x to normal */89n = -1022-ix;90if(n<=31) {91hx = (hx<<n)|(lx>>(32-n));92lx <<= n;93} else {94hx = lx<<(n-32);95lx = 0;96}97}98if(iy >= -1022)99hy = 0x00100000|(0x000fffff&hy);100else { /* subnormal y, shift y to normal */101n = -1022-iy;102if(n<=31) {103hy = (hy<<n)|(ly>>(32-n));104ly <<= n;105} else {106hy = ly<<(n-32);107ly = 0;108}109}110111/* fix point fmod */112n = ix - iy;113while(n--) {114hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;115if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}116else {117if((hz|lz)==0) /* return sign(x)*0 */118return Zero[(unsigned)sx>>31];119hx = hz+hz+(lz>>31); lx = lz+lz;120}121}122hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;123if(hz>=0) {hx=hz;lx=lz;}124125/* convert back to floating value and restore the sign */126if((hx|lx)==0) /* return sign(x)*0 */127return Zero[(unsigned)sx>>31];128while(hx<0x00100000) { /* normalize x */129hx = hx+hx+(lx>>31); lx = lx+lx;130iy -= 1;131}132if(iy>= -1022) { /* normalize output */133hx = ((hx-0x00100000)|((iy+1023)<<20));134__HI(x) = hx|sx;135__LO(x) = lx;136} else { /* subnormal output */137n = -1022 - iy;138if(n<=20) {139lx = (lx>>n)|((unsigned)hx<<(32-n));140hx >>= n;141} else if (n<=31) {142lx = (hx<<(32-n))|(lx>>n); hx = sx;143} else {144lx = hx>>(n-32); hx = sx;145}146__HI(x) = hx|sx;147__LO(x) = lx;148x *= one; /* create necessary signal */149}150return x; /* exact output */151}152153154