1 /* arithmetic.c -- Builtins for HSAIL arithmetic instructions for which
2 there is no feasible direct gcc GENERIC expression.
4 Copyright (C) 2015-2017 Free Software Foundation, Inc.
5 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
6 for General Processor Tech.
8 Permission is hereby granted, free of charge, to any person obtaining a
9 copy of this software and associated documentation files
10 (the "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice shall be included
17 in all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 USE OR OTHER DEALINGS IN THE SOFTWARE.
34 /* HSAIL defines INT_MIN % -1 to be 0 while with C it's undefined,
35 and causes an overflow exception at least with gcc and C on IA-32. */
38 __hsail_rem_s32 (int32_t dividend
, int32_t divisor
)
40 if (dividend
== INT_MIN
&& divisor
== -1)
43 return dividend
% divisor
;
47 __hsail_rem_s64 (int64_t dividend
, int64_t divisor
)
49 if (dividend
== INT64_MIN
&& divisor
== -1)
52 return dividend
% divisor
;
55 /* HSAIL has defined behavior for min and max when one of the operands is
56 NaN: in that case the other operand is returned. In C and with gcc's
57 MIN_EXPR/MAX_EXPR, the returned operand is undefined. */
60 __hsail_min_f32 (float a
, float b
)
66 else if (a
== 0.0f
&& b
== 0.0f
)
67 return signbit (a
) ? a
: b
;
75 __hsail_min_f64 (double a
, double b
)
88 __hsail_max_f32 (float a
, float b
)
94 else if (a
== 0.0f
&& b
== 0.0f
&& signbit (a
))
103 __hsail_max_f64 (double a
, double b
)
109 else if (a
== 0.0 && b
== 0.0 && signbit (a
))
118 __hsail_cvt_zeroi_sat_u8_f32 (float a
)
122 if (a
>= (float) UINT8_MAX
)
130 __hsail_cvt_zeroi_sat_s8_f32 (float a
)
134 if (a
>= (float) INT8_MAX
)
136 if (a
<= (float) INT8_MIN
)
142 __hsail_cvt_zeroi_sat_u16_f32 (float a
)
146 if (a
>= (float) UINT16_MAX
)
154 __hsail_cvt_zeroi_sat_s16_f32 (float a
)
158 if (a
>= (float) INT16_MAX
)
160 if (a
<= (float) INT16_MIN
)
166 __hsail_cvt_zeroi_sat_u32_f32 (float a
)
170 if (a
>= (float) UINT32_MAX
)
178 __hsail_cvt_zeroi_sat_s32_f32 (float a
)
182 if (a
>= (float) INT32_MAX
)
184 if (a
<= (float) INT32_MIN
)
190 __hsail_cvt_zeroi_sat_u64_f32 (float a
)
194 if (a
>= (float) UINT64_MAX
)
202 __hsail_cvt_zeroi_sat_s64_f32 (float a
)
206 if (a
>= (float) INT64_MAX
)
208 if (a
<= (float) INT64_MIN
)
214 __hsail_cvt_zeroi_sat_u8_f64 (double a
)
218 if (a
>= (double) UINT8_MAX
)
226 __hsail_cvt_zeroi_sat_s8_f64 (double a
)
230 if (a
>= (double) INT8_MAX
)
232 if (a
<= (double) INT8_MIN
)
238 __hsail_cvt_zeroi_sat_u16_f64 (double a
)
242 if (a
>= (double) UINT16_MAX
)
250 __hsail_cvt_zeroi_sat_s16_f64 (double a
)
254 if (a
>= (double) INT16_MAX
)
256 if (a
<= (double) INT16_MIN
)
262 __hsail_cvt_zeroi_sat_u32_f64 (double a
)
266 if (a
>= (double) UINT32_MAX
)
274 __hsail_cvt_zeroi_sat_s32_f64 (double a
)
278 if (a
>= (double) INT32_MAX
)
280 if (a
<= (double) INT32_MIN
)
286 __hsail_cvt_zeroi_sat_u64_f64 (double a
)
290 if (a
>= (double) UINT64_MAX
)
298 __hsail_cvt_zeroi_sat_s64_f64 (double a
)
302 if (a
>= (double) INT64_MAX
)
304 if (a
<= (double) INT64_MIN
)
310 /* Flush the operand to zero in case it's a denormalized number.
311 Do not cause any exceptions in case of NaNs. */
314 __hsail_ftz_f32 (float a
)
316 if (isnan (a
) || isinf (a
) || a
== 0.0f
)
332 #define F16_MIN (6.10e-5)
334 /* Flush the single precision operand to zero in case it's considered
335 a denormalized number in case it was a f16. Do not cause any exceptions
339 __hsail_ftz_f32_f16 (float a
)
341 if (isnan (a
) || isinf (a
) || a
== 0.0f
)
358 __hsail_ftz_f64 (double a
)
360 if (isnan (a
) || isinf (a
) || a
== 0.0d
)
377 __hsail_borrow_u32 (uint32_t a
, uint32_t b
)
379 return __builtin_sub_overflow_p (a
, b
, a
);
383 __hsail_borrow_u64 (uint64_t a
, uint64_t b
)
385 return __builtin_sub_overflow_p (a
, b
, a
);
389 __hsail_carry_u32 (uint32_t a
, uint32_t b
)
391 return __builtin_add_overflow_p (a
, b
, a
);
395 __hsail_carry_u64 (uint64_t a
, uint64_t b
)
397 return __builtin_add_overflow_p (a
, b
, a
);
401 __hsail_fract_f32 (float a
)
405 return signbit (a
) == 0 ? 0.0f
: -0.0f
;
406 if (isnan (a
) || a
== 0.0f
)
409 return fminf (a
- floorf (a
), 0x1.fffffep
-1f
);
413 __hsail_fract_f64 (double a
)
417 return 0.0f
* isinf (a
);
418 if (isnan (a
) || a
== 0.0f
)
421 return fmin (a
- floor (a
), 0x1.fffffffffffffp
-1d
);
425 __hsail_class_f32 (float a
, uint32_t flags
)
427 return (flags
& 0x0001 && isnan (a
) && !(*(uint32_t *) &a
& (1ul << 22)))
428 || (flags
& 0x0002 && isnan (a
) && (*(uint32_t *) &a
& (1ul << 22)))
429 || (flags
& 0x0004 && isinf (a
) && a
< 0.0f
)
430 || (flags
& 0x0008 && isnormal (a
) && signbit (a
))
431 || (flags
& 0x0010 && a
< 0.0f
&& a
> -FLT_MIN
)
432 || (flags
& 0x0020 && a
== 0.0f
&& signbit (a
))
433 || (flags
& 0x0040 && a
== 0.0f
&& !signbit (a
))
434 || (flags
& 0x0080 && a
> 0.0f
&& a
< FLT_MIN
)
435 || (flags
& 0x0100 && isnormal (a
) && !signbit (a
))
436 || (flags
& 0x0200 && isinf (a
) && a
>= 0.0f
);
440 __hsail_class_f64 (double a
, uint32_t flags
)
442 return (flags
& 0x0001 && isnan (a
) && !(*(uint64_t *) &a
& (1ul << 51)))
443 || (flags
& 0x0002 && isnan (a
) && (*(uint64_t *) &a
& (1ul << 51)))
444 || (flags
& 0x0004 && isinf (a
) && a
< 0.0f
)
445 || (flags
& 0x0008 && isnormal (a
) && signbit (a
))
446 || (flags
& 0x0010 && a
< 0.0f
&& a
> -FLT_MIN
)
447 || (flags
& 0x0020 && a
== 0.0f
&& signbit (a
))
448 || (flags
& 0x0040 && a
== 0.0f
&& !signbit (a
))
449 || (flags
& 0x0080 && a
> 0.0f
&& a
< FLT_MIN
)
450 || (flags
& 0x0100 && isnormal (a
) && !signbit (a
))
451 || (flags
& 0x0200 && isinf (a
) && a
>= 0.0f
);
455 /* 'class' for a f32-converted f16 which should otherwise be treated like f32
456 except for its limits. */
459 __hsail_class_f32_f16 (float a
, uint32_t flags
)
461 return (flags
& 0x0001 && isnan (a
) && !(*(uint32_t *) &a
& 0x40000000))
462 || (flags
& 0x0002 && isnan (a
) && (*(uint32_t *) &a
& 0x40000000))
463 || (flags
& 0x0004 && isinf (a
) && a
< 0.0f
)
464 || (flags
& 0x0008 && a
!= 0.0f
&& !isinf (a
) && !isnan (a
)
466 || (flags
& 0x0010 && a
!= 0.0f
&& !isinf (a
) && !isnan (a
) && a
< 0.0f
468 || (flags
& 0x0020 && a
== 0.0f
&& signbit (a
))
469 || (flags
& 0x0040 && a
== 0.0f
&& !signbit (a
))
470 || (flags
& 0x0080 && a
!= 0.0f
&& !isinf (a
) && !isnan (a
) && a
> 0.0f
472 || (flags
& 0x0100 && a
!= 0.0f
&& !isinf (a
) && !isnan (a
)
474 || (flags
& 0x0200 && isinf (a
) && a
>= 0.0f
);