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
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
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
)
35 GET_FLOAT_WORD (hx
, x
, NO_COND
);
36 GET_FLOAT_WORD (hy
, y
, NO_COND
);
37 v64si sx
= hx
& 0x80000000; /* sign of x */
39 hy
&= 0x7fffffff; /* |y| */
41 v64sf zeroes
= VECTOR_MERGE (VECTOR_INIT (-0.0f
),
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
);
50 VECTOR_IF (hx
< hy
, cond
) // |x|<|y| return x
51 VECTOR_RETURN (x
, cond
);
53 VECTOR_IF (hx
== hy
, cond
)
54 VECTOR_RETURN (zeroes
, hx
== hy
); // |x|=|y| return x*0
57 /* determine ix = ilogb(x) */
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));
64 VECTOR_COND_MOVE (ix
, ix
- 1, cond
& (i
> 0));
66 VECTOR_COND_MOVE (ix
, (hx
>> 23) - 127, cond
);
69 /* determine iy = ilogb(y) */
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));
76 VECTOR_COND_MOVE (iy
, (hy
>> 23) - 127, cond
);
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
85 VECTOR_COND_MOVE (hx
, hx
<< n
, cond
);
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
93 VECTOR_COND_MOVE (hy
, hy
<< n
, cond
);
101 while (!ALL_ZEROES_P (cond
))
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
);
114 n
+= cond
; // Active lanes should be -1
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 */
138 SET_FLOAT_WORD (x
, hx
| sx
, cond
);
139 x
*= VECTOR_INIT (1.0f
); /* create necessary signal */
142 VECTOR_RETURN (x
, NO_COND
); /* exact output */
147 DEF_VARIANTS2 (fmodf
, sf
, sf
)