]> git.ipfire.org Git - thirdparty/gcc.git/blame - libhsail-rt/rt/sat_arithmetic.c
Brig front-end
[thirdparty/gcc.git] / libhsail-rt / rt / sat_arithmetic.c
CommitLineData
5fd1486c
PJ
1/* sat_arithmetic.c -- Builtins for HSAIL saturating arithmetic instructions.
2
3 Copyright (C) 2015-2016 Free Software Foundation, Inc.
4 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
5 for General Processor Tech.
6
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:
14
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17
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.
25*/
26
27#include <stdint.h>
28
29uint8_t
30__hsail_sat_add_u8 (uint8_t a, uint8_t b)
31{
32 uint16_t c = (uint16_t) a + (uint16_t) b;
33 if (c > UINT8_MAX)
34 return UINT8_MAX;
35 else
36 return c;
37}
38
39uint16_t
40__hsail_sat_add_u16 (uint16_t a, uint16_t b)
41{
42 uint32_t c = (uint32_t) a + (uint32_t) b;
43 if (c > UINT16_MAX)
44 return UINT16_MAX;
45 else
46 return c;
47}
48
49uint32_t
50__hsail_sat_add_u32 (uint32_t a, uint32_t b)
51{
52 uint64_t c = (uint64_t) a + (uint64_t) b;
53 if (c > UINT32_MAX)
54 return UINT32_MAX;
55 else
56 return c;
57}
58
59uint64_t
60__hsail_sat_add_u64 (uint64_t a, uint64_t b)
61{
62 __uint128_t c = (__uint128_t) a + (__uint128_t) b;
63 if (c > UINT64_MAX)
64 return UINT64_MAX;
65 else
66 return c;
67}
68
69int8_t
70__hsail_sat_add_s8 (int8_t a, int8_t b)
71{
72 int16_t c = (int16_t) a + (int16_t) b;
73 if (c > INT8_MAX)
74 return INT8_MAX;
75 else if (c < INT8_MIN)
76 return INT8_MIN;
77 else
78 return c;
79}
80
81int16_t
82__hsail_sat_add_s16 (int16_t a, int16_t b)
83{
84 int32_t c = (int32_t) a + (int32_t) b;
85 if (c > INT16_MAX)
86 return INT16_MAX;
87 else if (c < INT16_MIN)
88 return INT16_MIN;
89 else
90 return c;
91}
92
93int32_t
94__hsail_sat_add_s32 (int32_t a, int32_t b)
95{
96 int64_t c = (int64_t) a + (int64_t) b;
97 if (c > INT32_MAX)
98 return INT32_MAX;
99 else if (c < INT32_MIN)
100 return INT32_MIN;
101 else
102 return c;
103}
104
105int64_t
106__hsail_sat_add_s64 (int64_t a, int64_t b)
107{
108 __int128_t c = (__int128_t) a + (__int128_t) b;
109 if (c > INT64_MAX)
110 return INT64_MAX;
111 else if (c < INT64_MIN)
112 return INT64_MIN;
113 else
114 return c;
115}
116
117uint8_t
118__hsail_sat_sub_u8 (uint8_t a, uint8_t b)
119{
120 int16_t c = (uint16_t) a - (uint16_t) b;
121 if (c < 0)
122 return 0;
123 else if (c > UINT8_MAX)
124 return UINT8_MAX;
125 else
126 return c;
127}
128
129uint16_t
130__hsail_sat_sub_u16 (uint16_t a, uint16_t b)
131{
132 int32_t c = (uint32_t) a - (uint32_t) b;
133 if (c < 0)
134 return 0;
135 else if (c > UINT16_MAX)
136 return UINT16_MAX;
137 else
138 return c;
139}
140
141uint32_t
142__hsail_sat_sub_u32 (uint32_t a, uint32_t b)
143{
144 int64_t c = (uint64_t) a - (uint64_t) b;
145 if (c < 0)
146 return 0;
147 else if (c > UINT32_MAX)
148 return UINT32_MAX;
149 else
150 return c;
151}
152
153uint64_t
154__hsail_sat_sub_u64 (uint64_t a, uint64_t b)
155{
156 __int128_t c = (__uint128_t) a - (__uint128_t) b;
157 if (c < 0)
158 return 0;
159 else if (c > UINT64_MAX)
160 return UINT64_MAX;
161 else
162 return c;
163}
164
165int8_t
166__hsail_sat_sub_s8 (int8_t a, int8_t b)
167{
168 int16_t c = (int16_t) a - (int16_t) b;
169 if (c > INT8_MAX)
170 return INT8_MAX;
171 else if (c < INT8_MIN)
172 return INT8_MIN;
173 else
174 return c;
175}
176
177int16_t
178__hsail_sat_sub_s16 (int16_t a, int16_t b)
179{
180 int32_t c = (int32_t) a - (int32_t) b;
181 if (c > INT16_MAX)
182 return INT16_MAX;
183 else if (c < INT16_MIN)
184 return INT16_MIN;
185 else
186 return c;
187}
188
189int32_t
190__hsail_sat_sub_s32 (int32_t a, int32_t b)
191{
192 int64_t c = (int64_t) a - (int64_t) b;
193 if (c > INT32_MAX)
194 return INT32_MAX;
195 else if (c < INT32_MIN)
196 return INT32_MIN;
197 else
198 return c;
199}
200
201int64_t
202__hsail_sat_sub_s64 (int64_t a, int64_t b)
203{
204 __int128_t c = (__int128_t) a - (__int128_t) b;
205 if (c > INT64_MAX)
206 return INT64_MAX;
207 else if (c < INT64_MIN)
208 return INT64_MIN;
209 else
210 return c;
211}
212
213uint8_t
214__hsail_sat_mul_u8 (uint8_t a, uint8_t b)
215{
216 uint16_t c = (uint16_t) a * (uint16_t) b;
217 if (c > UINT8_MAX)
218 return UINT8_MAX;
219 else
220 return c;
221}
222
223uint16_t
224__hsail_sat_mul_u16 (uint16_t a, uint16_t b)
225{
226 uint32_t c = (uint32_t) a * (uint32_t) b;
227 if (c > UINT16_MAX)
228 return UINT16_MAX;
229 else
230 return c;
231}
232
233uint32_t
234__hsail_sat_mul_u32 (uint32_t a, uint32_t b)
235{
236 uint64_t c = (uint64_t) a * (uint64_t) b;
237 if (c > UINT32_MAX)
238 return UINT32_MAX;
239 else
240 return c;
241}
242
243uint64_t
244__hsail_sat_mul_u64 (uint64_t a, uint64_t b)
245{
246 __uint128_t c = (__uint128_t) a * (__uint128_t) b;
247 if (c > UINT64_MAX)
248 return UINT64_MAX;
249 else
250 return c;
251}
252
253int8_t
254__hsail_sat_mul_s8 (int8_t a, int8_t b)
255{
256 int16_t c = (int16_t) a * (int16_t) b;
257 if (c > INT8_MAX)
258 return INT8_MAX;
259 else if (c < INT8_MIN)
260 return INT8_MIN;
261 else
262 return c;
263}
264
265int16_t
266__hsail_sat_mul_s16 (int16_t a, int16_t b)
267{
268 int32_t c = (int32_t) a * (int32_t) b;
269 if (c > INT16_MAX)
270 return INT16_MAX;
271 else if (c < INT16_MIN)
272 return INT16_MIN;
273 else
274 return c;
275}
276
277int32_t
278__hsail_sat_mul_s32 (int32_t a, int32_t b)
279{
280 int64_t c = (int64_t) a * (int64_t) b;
281 if (c > INT32_MAX)
282 return INT32_MAX;
283 else if (c < INT32_MIN)
284 return INT32_MIN;
285 else
286 return c;
287}
288
289int64_t
290__hsail_sat_mul_s64 (int64_t a, int64_t b)
291{
292 __int128_t c = (__int128_t) a * (__int128_t) b;
293 if (c > INT64_MAX)
294 return INT64_MAX;
295 else if (c < INT64_MIN)
296 return INT64_MIN;
297 else
298 return c;
299}