| 1 |
/* Template file for scalar saturating arithmetic validation. |
| 2 |
|
| 3 |
This file defines scalar saturating addition and subtraction functions for a |
| 4 |
given type. This type, along with the corresponding minimum and maximum |
| 5 |
values for that type, must be defined by any test file which includes this |
| 6 |
template file. */ |
| 7 |
|
| 8 |
#ifndef SAT_ARIT_INC |
| 9 |
#define SAT_ARIT_INC |
| 10 |
|
| 11 |
#include <limits.h> |
| 12 |
|
| 13 |
#ifndef UT |
| 14 |
#define UT unsigned int |
| 15 |
#define UMAX UINT_MAX |
| 16 |
#define UMIN 0 |
| 17 |
#endif |
| 18 |
|
| 19 |
UT uadd (UT a, UT b) |
| 20 |
{ |
| 21 |
UT sum = a + b; |
| 22 |
return sum < a ? UMAX : sum; |
| 23 |
} |
| 24 |
|
| 25 |
UT uadd2 (UT a, UT b) |
| 26 |
{ |
| 27 |
UT c; |
| 28 |
if (!__builtin_add_overflow(a, b, &c)) |
| 29 |
return c; |
| 30 |
return UMAX; |
| 31 |
} |
| 32 |
|
| 33 |
UT usub (UT a, UT b) |
| 34 |
{ |
| 35 |
UT sum = a - b; |
| 36 |
return sum > a ? UMIN : sum; |
| 37 |
} |
| 38 |
|
| 39 |
#endif |