blob: 8004493de77bcd3dbb85be617b8f9802fc272d18 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* 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
Elliott Hughesa0ee0782013-01-30 19:06:37 -080016#include <sys/cdefs.h>
Elliott Hughesbac0ebb2021-01-26 14:17:20 -080017__FBSDID("$FreeBSD$");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080018
19#include "math.h"
20#include "math_private.h"
21
22static const float zero = 0.0;
23
24
25float
26__ieee754_remainderf(float x, float p)
27{
28 int32_t hx,hp;
29 u_int32_t sx;
30 float p_half;
31
32 GET_FLOAT_WORD(hx,x);
33 GET_FLOAT_WORD(hp,p);
34 sx = hx&0x80000000;
35 hp &= 0x7fffffff;
36 hx &= 0x7fffffff;
37
38 /* purge off exception values */
Elliott Hughesab528072018-07-24 00:01:52 +000039 if((hp==0)|| /* p = 0 */
40 (hx>=0x7f800000)|| /* x not finite */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041 ((hp>0x7f800000))) /* p is NaN */
Elliott Hughesab528072018-07-24 00:01:52 +000042 return nan_mix_op(x, p, *)/nan_mix_op(x, p, *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043
44
45 if (hp<=0x7effffff) x = __ieee754_fmodf(x,p+p); /* now x < 2p */
46 if ((hx-hp)==0) return zero*x;
47 x = fabsf(x);
48 p = fabsf(p);
49 if (hp<0x01000000) {
50 if(x+x>p) {
51 x-=p;
52 if(x+x>=p) x -= p;
53 }
54 } else {
55 p_half = (float)0.5*p;
56 if(x>p_half) {
57 x-=p;
58 if(x>=p_half) x -= p;
59 }
60 }
61 GET_FLOAT_WORD(hx,x);
Elliott Hughesa0ee0782013-01-30 19:06:37 -080062 if ((hx&0x7fffffff)==0) hx = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063 SET_FLOAT_WORD(x,hx^sx);
64 return x;
65}