| 1 |
#ifndef AWS_COMMON_MATH_GCC_OVERFLOW_INL |
| 2 |
#define AWS_COMMON_MATH_GCC_OVERFLOW_INL |
| 3 |
|
| 4 |
/** |
| 5 |
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 |
* SPDX-License-Identifier: Apache-2.0. |
| 7 |
*/ |
| 8 |
|
| 9 |
/* |
| 10 |
* This header is already included, but include it again to make editor |
| 11 |
* highlighting happier. |
| 12 |
*/ |
| 13 |
#include <aws/common/common.h> |
| 14 |
#include <aws/common/math.h> |
| 15 |
|
| 16 |
AWS_EXTERN_C_BEGIN |
| 17 |
/** |
| 18 |
* Multiplies a * b. If the result overflows, returns 2^64 - 1. |
| 19 |
*/ |
| 20 |
AWS_STATIC_IMPL uint64_t aws_mul_u64_saturating(uint64_t a, uint64_t b) { |
| 21 |
uint64_t res; |
| 22 |
|
| 23 |
if (__builtin_mul_overflow(a, b, &res)) { |
| 24 |
res = UINT64_MAX; |
| 25 |
} |
| 26 |
|
| 27 |
return res; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* If a * b overflows, returns AWS_OP_ERR; otherwise multiplies |
| 32 |
* a * b, returns the result in *r, and returns AWS_OP_SUCCESS. |
| 33 |
*/ |
| 34 |
AWS_STATIC_IMPL int aws_mul_u64_checked(uint64_t a, uint64_t b, uint64_t *r) { |
| 35 |
if (__builtin_mul_overflow(a, b, r)) { |
| 36 |
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); |
| 37 |
} |
| 38 |
return AWS_OP_SUCCESS; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Multiplies a * b. If the result overflows, returns 2^32 - 1. |
| 43 |
*/ |
| 44 |
AWS_STATIC_IMPL uint32_t aws_mul_u32_saturating(uint32_t a, uint32_t b) { |
| 45 |
uint32_t res; |
| 46 |
|
| 47 |
if (__builtin_mul_overflow(a, b, &res)) { |
| 48 |
res = UINT32_MAX; |
| 49 |
} |
| 50 |
|
| 51 |
return res; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* If a * b overflows, returns AWS_OP_ERR; otherwise multiplies |
| 56 |
* a * b, returns the result in *r, and returns AWS_OP_SUCCESS. |
| 57 |
*/ |
| 58 |
AWS_STATIC_IMPL int aws_mul_u32_checked(uint32_t a, uint32_t b, uint32_t *r) { |
| 59 |
if (__builtin_mul_overflow(a, b, r)) { |
| 60 |
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); |
| 61 |
} |
| 62 |
return AWS_OP_SUCCESS; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* If a + b overflows, returns AWS_OP_ERR; otherwise adds |
| 67 |
* a + b, returns the result in *r, and returns AWS_OP_SUCCESS. |
| 68 |
*/ |
| 69 |
AWS_STATIC_IMPL int aws_add_u64_checked(uint64_t a, uint64_t b, uint64_t *r) { |
| 70 |
if (__builtin_add_overflow(a, b, r)) { |
| 71 |
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); |
| 72 |
} |
| 73 |
return AWS_OP_SUCCESS; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Adds a + b. If the result overflows, returns 2^64 - 1. |
| 78 |
*/ |
| 79 |
AWS_STATIC_IMPL uint64_t aws_add_u64_saturating(uint64_t a, uint64_t b) { |
| 80 |
uint64_t res; |
| 81 |
|
| 82 |
if (__builtin_add_overflow(a, b, &res)) { |
| 83 |
res = UINT64_MAX; |
| 84 |
} |
| 85 |
|
| 86 |
return res; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* If a + b overflows, returns AWS_OP_ERR; otherwise adds |
| 91 |
* a + b, returns the result in *r, and returns AWS_OP_SUCCESS. |
| 92 |
*/ |
| 93 |
AWS_STATIC_IMPL int aws_add_u32_checked(uint32_t a, uint32_t b, uint32_t *r) { |
| 94 |
if (__builtin_add_overflow(a, b, r)) { |
| 95 |
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); |
| 96 |
} |
| 97 |
return AWS_OP_SUCCESS; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Adds a + b. If the result overflows, returns 2^32 - 1. |
| 102 |
*/ |
| 103 |
AWS_STATIC_IMPL uint32_t aws_add_u32_saturating(uint32_t a, uint32_t b) { |
| 104 |
uint32_t res; |
| 105 |
|
| 106 |
if (__builtin_add_overflow(a, b, &res)) { |
| 107 |
res = UINT32_MAX; |
| 108 |
} |
| 109 |
|
| 110 |
return res; |
| 111 |
} |
| 112 |
|
| 113 |
AWS_EXTERN_C_END |
| 114 |
|
| 115 |
#endif /* AWS_COMMON_MATH_GCC_OVERFLOW_INL */ |