devctl.h: update for POSIX-1.2024
[newlib-cygwin.git] / newlib / libm / machine / amdgcn / v64sf_fmod.c
blob7302420ad382a62fb3f4a3f3f024bf938de5336e
1 /*
2 * Copyright 2023 Siemens
4 * The authors hereby grant permission to use, copy, modify, distribute,
5 * and license this software and its documentation for any purpose, provided
6 * that existing copyright notices are retained in all copies and that this
7 * notice is included verbatim in any distributions. No written agreement,
8 * license, or royalty fee is required for any of the authorized uses.
9 * Modifications to this software may be copyrighted by their authors
10 * and need not follow the licensing terms described here, provided that
11 * the new terms are clearly indicated on the first page of each file where
12 * they apply.
16 * ====================================================
17 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
19 * Developed at SunPro, a Sun Microsystems, Inc. business.
20 * Permission to use, copy, modify, and distribute this
21 * software is freely granted, provided that this notice
22 * is preserved.
23 * ====================================================
26 /* Based on newlib/libm/mathfp/sf_fmod.c in Newlib. */
28 #include "amdgcnmach.h"
30 DEF_VS_MATH_FUNC (v64sf, fmodf, v64sf x, v64sf y)
32 FUNCTION_INIT(v64sf);
34 v64si hx, hy, hz;
35 GET_FLOAT_WORD (hx, x, NO_COND);
36 GET_FLOAT_WORD (hy, y, NO_COND);
37 v64si sx = hx & 0x80000000; /* sign of x */
38 hx ^=sx; /* |x| */
39 hy &= 0x7fffffff; /* |y| */
41 v64sf zeroes = VECTOR_MERGE (VECTOR_INIT (-0.0f),
42 VECTOR_INIT (0.0f),
43 sx != 0);
45 /* purge off exception values */
46 VECTOR_IF ((hy == 0) | (hx >= 0x7f800000)
47 | (hy > 0x7f800000), cond) // y=0, or x not finite or y is NaN
48 VECTOR_RETURN ((x * y) / (x * y), cond);
49 VECTOR_ENDIF
50 VECTOR_IF (hx < hy, cond) // |x|<|y| return x
51 VECTOR_RETURN (x, cond);
52 VECTOR_ENDIF
53 VECTOR_IF (hx == hy, cond)
54 VECTOR_RETURN (zeroes, hx == hy); // |x|=|y| return x*0
55 VECTOR_ENDIF
57 /* determine ix = ilogb(x) */
58 v64si ix;
59 VECTOR_IF (hx < 0x00800000, cond) // subnormal x
60 ix = VECTOR_INIT (-126);
61 for (v64si i = (hx << 8);
62 !ALL_ZEROES_P (cond & (i > 0));
63 i <<= 1)
64 VECTOR_COND_MOVE (ix, ix - 1, cond & (i > 0));
65 VECTOR_ELSE (cond)
66 VECTOR_COND_MOVE (ix, (hx >> 23) - 127, cond);
67 VECTOR_ENDIF
69 /* determine iy = ilogb(y) */
70 v64si iy;
71 VECTOR_IF (hy < 0x00800000, cond) // subnormal y
72 iy = VECTOR_INIT (-126);
73 for (v64si i = (hy << 8); !ALL_ZEROES_P (cond & (i >= 0)); i <<= 1)
74 VECTOR_COND_MOVE (iy, iy - 1, cond & (i >= 0));
75 VECTOR_ELSE (cond)
76 VECTOR_COND_MOVE (iy, (hy >> 23) - 127, cond);
77 VECTOR_ENDIF
79 /* set up {hx,lx}, {hy,ly} and align y to x */
80 VECTOR_IF (ix >= -126, cond)
81 VECTOR_COND_MOVE (hx, 0x00800000 | (0x007fffff & hx), cond);
82 VECTOR_ELSE (cond) // subnormal x, shift x to normal
84 v64si n = -126 - ix;
85 VECTOR_COND_MOVE (hx, hx << n, cond);
87 VECTOR_ENDIF
88 VECTOR_IF (iy >= -126, cond)
89 VECTOR_COND_MOVE (hy, 0x00800000 | (0x007fffff & hy), cond);
90 VECTOR_ELSE (cond) // subnormal y, shift y to normal
92 v64si n = -126 - iy;
93 VECTOR_COND_MOVE (hy, hy << n, cond);
95 VECTOR_ENDIF
97 /* fix point fmod */
98 v64si n = ix - iy;
99 v64si cond = n != 0;
101 while (!ALL_ZEROES_P (cond))
103 hz = hx - hy;
104 VECTOR_IF2 (hz < 0, cond2, cond)
105 VECTOR_COND_MOVE (hx, hx + hx, cond2);
106 VECTOR_ELSE2 (cond2, cond)
107 VECTOR_IF2 (hz == 0, cond3, cond2) // return sign(x)*0
108 VECTOR_RETURN (zeroes, cond3);
109 VECTOR_ELSE2 (cond3, cond2)
110 VECTOR_COND_MOVE (hx, hz + hz, cond2);
111 VECTOR_ENDIF
112 VECTOR_ENDIF
114 n += cond; // Active lanes should be -1
115 cond &= (n != 0);
118 hz = hx - hy;
119 VECTOR_COND_MOVE (hx, hz, hz >= 0);
121 /* convert back to floating value and restore the sign */
122 VECTOR_RETURN (zeroes, hx == 0); // return sign(x)*0
124 cond = hx < 0x00800000;
125 while (!ALL_ZEROES_P (cond)) // normalize x
127 VECTOR_COND_MOVE (hx, hx + hx, cond);
128 iy += cond; // Active lanes should be -1
130 cond &= (hx < 0x00800000);
132 VECTOR_IF (iy >= -126, cond) // normalize output
133 VECTOR_COND_MOVE (hx, (hx - 0x00800000) | ((iy + 127) << 23), cond);
134 SET_FLOAT_WORD (x, hx | sx, cond);
135 VECTOR_ELSE (cond) // subnormal output */
136 n = -126 - iy;
137 hx >>= n;
138 SET_FLOAT_WORD (x, hx | sx, cond);
139 x *= VECTOR_INIT (1.0f); /* create necessary signal */
140 VECTOR_ENDIF
142 VECTOR_RETURN (x, NO_COND); /* exact output */
144 FUNCTION_RETURN;
147 DEF_VARIANTS2 (fmodf, sf, sf)