1 /* sat_arithmetic.c -- Builtins for HSAIL saturating arithmetic instructions.
3 Copyright (C) 2015-2018 Free Software Foundation, Inc.
4 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
5 for General Processor Tech.
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files
9 (the "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
30 __hsail_sat_add_u8 (uint8_t a
, uint8_t b
)
32 uint16_t c
= (uint16_t) a
+ (uint16_t) b
;
40 __hsail_sat_add_u16 (uint16_t a
, uint16_t b
)
42 uint32_t c
= (uint32_t) a
+ (uint32_t) b
;
50 __hsail_sat_add_u32 (uint32_t a
, uint32_t b
)
53 if (__builtin_add_overflow (a
, b
, &c
))
59 __hsail_sat_add_u64 (uint64_t a
, uint64_t b
)
62 if (__builtin_add_overflow (a
, b
, &c
))
68 __hsail_sat_add_s8 (int8_t a
, int8_t b
)
70 int16_t c
= (int16_t) a
+ (int16_t) b
;
73 else if (c
< INT8_MIN
)
80 __hsail_sat_add_s16 (int16_t a
, int16_t b
)
82 int32_t c
= (int32_t) a
+ (int32_t) b
;
85 else if (c
< INT16_MIN
)
92 __hsail_sat_add_s32 (int32_t a
, int32_t b
)
95 if (__builtin_add_overflow (a
, b
, &c
))
96 return b
< 0 ? INT32_MIN
: INT32_MAX
;
101 __hsail_sat_add_s64 (int64_t a
, int64_t b
)
104 if (__builtin_add_overflow (a
, b
, &c
))
105 return b
< 0 ? INT64_MIN
: INT64_MAX
;
110 __hsail_sat_sub_u8 (uint8_t a
, uint8_t b
)
112 int16_t c
= (uint16_t) a
- (uint16_t) b
;
120 __hsail_sat_sub_u16 (uint16_t a
, uint16_t b
)
122 int32_t c
= (uint32_t) a
- (uint32_t) b
;
130 __hsail_sat_sub_u32 (uint32_t a
, uint32_t b
)
133 if (__builtin_sub_overflow (a
, b
, &c
))
139 __hsail_sat_sub_u64 (uint64_t a
, uint64_t b
)
142 if (__builtin_sub_overflow (a
, b
, &c
))
148 __hsail_sat_sub_s8 (int8_t a
, int8_t b
)
150 int16_t c
= (int16_t) a
- (int16_t) b
;
153 else if (c
< INT8_MIN
)
160 __hsail_sat_sub_s16 (int16_t a
, int16_t b
)
162 int32_t c
= (int32_t) a
- (int32_t) b
;
165 else if (c
< INT16_MIN
)
172 __hsail_sat_sub_s32 (int32_t a
, int32_t b
)
175 if (__builtin_sub_overflow (a
, b
, &c
))
176 return b
< 0 ? INT32_MAX
: INT32_MIN
;
181 __hsail_sat_sub_s64 (int64_t a
, int64_t b
)
184 if (__builtin_sub_overflow (a
, b
, &c
))
185 return b
< 0 ? INT64_MAX
: INT64_MIN
;
190 __hsail_sat_mul_u8 (uint8_t a
, uint8_t b
)
192 uint16_t c
= (uint16_t) a
* (uint16_t) b
;
200 __hsail_sat_mul_u16 (uint16_t a
, uint16_t b
)
202 uint32_t c
= (uint32_t) a
* (uint32_t) b
;
210 __hsail_sat_mul_u32 (uint32_t a
, uint32_t b
)
213 if (__builtin_mul_overflow (a
, b
, &c
))
219 __hsail_sat_mul_u64 (uint64_t a
, uint64_t b
)
222 if (__builtin_mul_overflow (a
, b
, &c
))
228 __hsail_sat_mul_s8 (int8_t a
, int8_t b
)
230 int16_t c
= (int16_t) a
* (int16_t) b
;
233 else if (c
< INT8_MIN
)
240 __hsail_sat_mul_s16 (int16_t a
, int16_t b
)
242 int32_t c
= (int32_t) a
* (int32_t) b
;
245 else if (c
< INT16_MIN
)
252 __hsail_sat_mul_s32 (int32_t a
, int32_t b
)
255 if (__builtin_mul_overflow (a
, b
, &c
))
256 return ((a
> 0) ^ (b
> 0)) ? INT32_MIN
: INT32_MAX
;
261 __hsail_sat_mul_s64 (int64_t a
, int64_t b
)
264 if (__builtin_mul_overflow (a
, b
, &c
))
265 return ((a
> 0) ^ (b
> 0)) ? INT64_MIN
: INT64_MAX
;