The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* e_remainderf.c -- float version of e_remainder.c. |
| 2 | * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * ==================================================== |
| 7 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
| 8 | * |
| 9 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
| 10 | * Permission to use, copy, modify, and distribute this |
| 11 | * software is freely granted, provided that this notice |
| 12 | * is preserved. |
| 13 | * ==================================================== |
| 14 | */ |
| 15 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 16 | #include "math.h" |
| 17 | #include "math_private.h" |
| 18 | |
| 19 | static const float zero = 0.0; |
| 20 | |
| 21 | |
| 22 | float |
Elliott Hughes | 4088e3a | 2023-08-03 13:33:56 -0700 | [diff] [blame] | 23 | remainderf(float x, float p) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 24 | { |
| 25 | int32_t hx,hp; |
| 26 | u_int32_t sx; |
| 27 | float p_half; |
| 28 | |
| 29 | GET_FLOAT_WORD(hx,x); |
| 30 | GET_FLOAT_WORD(hp,p); |
| 31 | sx = hx&0x80000000; |
| 32 | hp &= 0x7fffffff; |
| 33 | hx &= 0x7fffffff; |
| 34 | |
| 35 | /* purge off exception values */ |
Elliott Hughes | ab52807 | 2018-07-24 00:01:52 +0000 | [diff] [blame] | 36 | if((hp==0)|| /* p = 0 */ |
| 37 | (hx>=0x7f800000)|| /* x not finite */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 38 | ((hp>0x7f800000))) /* p is NaN */ |
Elliott Hughes | ab52807 | 2018-07-24 00:01:52 +0000 | [diff] [blame] | 39 | return nan_mix_op(x, p, *)/nan_mix_op(x, p, *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 | |
| 41 | |
Elliott Hughes | 4088e3a | 2023-08-03 13:33:56 -0700 | [diff] [blame] | 42 | if (hp<=0x7effffff) x = fmodf(x,p+p); /* now x < 2p */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 43 | if ((hx-hp)==0) return zero*x; |
| 44 | x = fabsf(x); |
| 45 | p = fabsf(p); |
| 46 | if (hp<0x01000000) { |
| 47 | if(x+x>p) { |
| 48 | x-=p; |
| 49 | if(x+x>=p) x -= p; |
| 50 | } |
| 51 | } else { |
| 52 | p_half = (float)0.5*p; |
| 53 | if(x>p_half) { |
| 54 | x-=p; |
| 55 | if(x>=p_half) x -= p; |
| 56 | } |
| 57 | } |
| 58 | GET_FLOAT_WORD(hx,x); |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 59 | if ((hx&0x7fffffff)==0) hx = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 60 | SET_FLOAT_WORD(x,hx^sx); |
| 61 | return x; |
| 62 | } |