aboutsummaryrefslogtreecommitdiff
path: root/libhsail-rt/rt/sat_arithmetic.c
blob: 883da99d11fc62a8f0b3d75b44015590e1b0264a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* sat_arithmetic.c -- Builtins for HSAIL saturating arithmetic instructions.

   Copyright (C) 2015-2018 Free Software Foundation, Inc.
   Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
   for General Processor Tech.

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files
   (the "Software"), to deal in the Software without restriction, including
   without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense, and/or sell copies of the Software, and to
   permit persons to whom the Software is furnished to do so, subject to
   the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
   OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
   USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <stdint.h>

uint8_t
__hsail_sat_add_u8 (uint8_t a, uint8_t b)
{
  uint16_t c = (uint16_t) a + (uint16_t) b;
  if (c > UINT8_MAX)
    return UINT8_MAX;
  else
    return c;
}

uint16_t
__hsail_sat_add_u16 (uint16_t a, uint16_t b)
{
  uint32_t c = (uint32_t) a + (uint32_t) b;
  if (c > UINT16_MAX)
    return UINT16_MAX;
  else
    return c;
}

uint32_t
__hsail_sat_add_u32 (uint32_t a, uint32_t b)
{
  uint32_t c;
  if (__builtin_add_overflow (a, b, &c))
    return UINT32_MAX;
  return c;
}

uint64_t
__hsail_sat_add_u64 (uint64_t a, uint64_t b)
{
  uint64_t c;
  if (__builtin_add_overflow (a, b, &c))
    return UINT64_MAX;
  return c;
}

int8_t
__hsail_sat_add_s8 (int8_t a, int8_t b)
{
  int16_t c = (int16_t) a + (int16_t) b;
  if (c > INT8_MAX)
    return INT8_MAX;
  else if (c < INT8_MIN)
    return INT8_MIN;
  else
    return c;
}

int16_t
__hsail_sat_add_s16 (int16_t a, int16_t b)
{
  int32_t c = (int32_t) a + (int32_t) b;
  if (c > INT16_MAX)
    return INT16_MAX;
  else if (c < INT16_MIN)
    return INT16_MIN;
  else
    return c;
}

int32_t
__hsail_sat_add_s32 (int32_t a, int32_t b)
{
  int32_t c;
  if (__builtin_add_overflow (a, b, &c))
    return b < 0 ? INT32_MIN : INT32_MAX;
  return c;
}

int64_t
__hsail_sat_add_s64 (int64_t a, int64_t b)
{
  int64_t c;
  if (__builtin_add_overflow (a, b, &c))
    return b < 0 ? INT64_MIN : INT64_MAX;
  return c;
}

uint8_t
__hsail_sat_sub_u8 (uint8_t a, uint8_t b)
{
  int16_t c = (uint16_t) a - (uint16_t) b;
  if (c < 0)
    return 0;
  else
    return c;
}

uint16_t
__hsail_sat_sub_u16 (uint16_t a, uint16_t b)
{
  int32_t c = (uint32_t) a - (uint32_t) b;
  if (c < 0)
    return 0;
  else
    return c;
}

uint32_t
__hsail_sat_sub_u32 (uint32_t a, uint32_t b)
{
  uint32_t c;
  if (__builtin_sub_overflow (a, b, &c))
    return 0;
  return c;
}

uint64_t
__hsail_sat_sub_u64 (uint64_t a, uint64_t b)
{
  uint64_t c;
  if (__builtin_sub_overflow (a, b, &c))
    return 0;
  return c;
}

int8_t
__hsail_sat_sub_s8 (int8_t a, int8_t b)
{
  int16_t c = (int16_t) a - (int16_t) b;
  if (c > INT8_MAX)
    return INT8_MAX;
  else if (c < INT8_MIN)
    return INT8_MIN;
  else
    return c;
}

int16_t
__hsail_sat_sub_s16 (int16_t a, int16_t b)
{
  int32_t c = (int32_t) a - (int32_t) b;
  if (c > INT16_MAX)
    return INT16_MAX;
  else if (c < INT16_MIN)
    return INT16_MIN;
  else
    return c;
}

int32_t
__hsail_sat_sub_s32 (int32_t a, int32_t b)
{
  int32_t c;
  if (__builtin_sub_overflow (a, b, &c))
    return b < 0 ? INT32_MAX : INT32_MIN;
  return c;
}

int64_t
__hsail_sat_sub_s64 (int64_t a, int64_t b)
{
  int64_t c;
  if (__builtin_sub_overflow (a, b, &c))
    return b < 0 ? INT64_MAX : INT64_MIN;
  return c;
}

uint8_t
__hsail_sat_mul_u8 (uint8_t a, uint8_t b)
{
  uint16_t c = (uint16_t) a * (uint16_t) b;
  if (c > UINT8_MAX)
    return UINT8_MAX;
  else
    return c;
}

uint16_t
__hsail_sat_mul_u16 (uint16_t a, uint16_t b)
{
  uint32_t c = (uint32_t) a * (uint32_t) b;
  if (c > UINT16_MAX)
    return UINT16_MAX;
  else
    return c;
}

uint32_t
__hsail_sat_mul_u32 (uint32_t a, uint32_t b)
{
  uint32_t c;
  if (__builtin_mul_overflow (a, b, &c))
    return UINT32_MAX;
  return c;
}

uint64_t
__hsail_sat_mul_u64 (uint64_t a, uint64_t b)
{
  uint64_t c;
  if (__builtin_mul_overflow (a, b, &c))
    return UINT64_MAX;
  return c;
}

int8_t
__hsail_sat_mul_s8 (int8_t a, int8_t b)
{
  int16_t c = (int16_t) a * (int16_t) b;
  if (c > INT8_MAX)
    return INT8_MAX;
  else if (c < INT8_MIN)
    return INT8_MIN;
  else
    return c;
}

int16_t
__hsail_sat_mul_s16 (int16_t a, int16_t b)
{
  int32_t c = (int32_t) a * (int32_t) b;
  if (c > INT16_MAX)
    return INT16_MAX;
  else if (c < INT16_MIN)
    return INT16_MIN;
  else
    return c;
}

int32_t
__hsail_sat_mul_s32 (int32_t a, int32_t b)
{
  int32_t c;
  if (__builtin_mul_overflow (a, b, &c))
    return ((a > 0) ^ (b > 0)) ? INT32_MIN : INT32_MAX;
  return c;
}

int64_t
__hsail_sat_mul_s64 (int64_t a, int64_t b)
{
  int64_t c;
  if (__builtin_mul_overflow (a, b, &c))
    return ((a > 0) ^ (b > 0)) ? INT64_MIN : INT64_MAX;
  return c;
}