Skip to content

Commit

Permalink
easier to just ignore the warning, vs trying to figure out the define…
Browse files Browse the repository at this point in the history
…s for every platform where size_t is unsigned long
  • Loading branch information
graebm committed Aug 13, 2024
1 parent b2ca5fb commit d80a18d
Showing 1 changed file with 19 additions and 32 deletions.
51 changes: 19 additions & 32 deletions include/aws/common/math.inl
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,11 @@ AWS_EXTERN_C_BEGIN
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4127) /*Disable "conditional expression is constant" */
#endif /* _MSC_VER */

/* Macros to cast (if necessary) between size_t and uint64_t.
* On x86_64 platforms, size_t is always 64 bits.
* But on __MACH__, size_t's type is unsigned long, while on others it's unsigned long long.
* Some compilers will warn if you don't you cast between them,
* while other compilers (GCC with -Wuseless-cast) warn if you needlessly cast between them.
* So declare macros that do the right thing. */
#ifdef __MACH__ /* do cast */
# define AWS_CAST_SIZE_TO_U64 (uint64_t)
# define AWS_CAST_SIZE_PTR_TO_U64 (uint64_t *)
# define AWS_CAST_U64_TO_SIZE (size_t)
#else /* no cast required*/
# define AWS_CAST_SIZE_TO_U64
# define AWS_CAST_SIZE_PTR_TO_U64
# define AWS_CAST_U64_TO_SIZE
#elif defined(__GNUC__)
# pragma GCC diagnostic push
# if defined(__cplusplus) && !defined(__clang__)
# pragma GCC diagnostic ignored "-Wuseless-cast" /* Warning is C++ only (not C), and GCC only (not clang) */
# endif
#endif

AWS_STATIC_IMPL uint64_t aws_sub_u64_saturating(uint64_t a, uint64_t b) {
Expand Down Expand Up @@ -100,9 +89,9 @@ AWS_STATIC_IMPL int aws_sub_u32_checked(uint32_t a, uint32_t b, uint32_t *r) {
*/
AWS_STATIC_IMPL size_t aws_mul_size_saturating(size_t a, size_t b) {
#if SIZE_BITS == 32
return aws_mul_u32_saturating(a, b);
return (size_t)aws_mul_u32_saturating(a, b);
#elif SIZE_BITS == 64
return AWS_CAST_SIZE_TO_U64 aws_mul_u64_saturating(a, b);
return (size_t)aws_mul_u64_saturating(a, b);
#else
# error "Target not supported"
#endif
Expand All @@ -114,9 +103,9 @@ AWS_STATIC_IMPL size_t aws_mul_size_saturating(size_t a, size_t b) {
*/
AWS_STATIC_IMPL int aws_mul_size_checked(size_t a, size_t b, size_t *r) {
#if SIZE_BITS == 32
return aws_mul_u32_checked(a, b, r);
return aws_mul_u32_checked(a, b, (uint32_t *)r);
#elif SIZE_BITS == 64
return aws_mul_u64_checked(a, b, AWS_CAST_SIZE_PTR_TO_U64 r);
return aws_mul_u64_checked(a, b, (uint64_t *)r);
#else
# error "Target not supported"
#endif
Expand All @@ -127,9 +116,9 @@ AWS_STATIC_IMPL int aws_mul_size_checked(size_t a, size_t b, size_t *r) {
*/
AWS_STATIC_IMPL size_t aws_add_size_saturating(size_t a, size_t b) {
#if SIZE_BITS == 32
return aws_add_u32_saturating(a, b);
return (size_t)aws_add_u32_saturating(a, b);
#elif SIZE_BITS == 64
return AWS_CAST_U64_TO_SIZE aws_add_u64_saturating(a, b);
return (size_t)aws_add_u64_saturating(a, b);
#else
# error "Target not supported"
#endif
Expand All @@ -141,29 +130,29 @@ AWS_STATIC_IMPL size_t aws_add_size_saturating(size_t a, size_t b) {
*/
AWS_STATIC_IMPL int aws_add_size_checked(size_t a, size_t b, size_t *r) {
#if SIZE_BITS == 32
return aws_add_u32_checked(a, b, r);
return aws_add_u32_checked(a, b, (uint32_t *)r);
#elif SIZE_BITS == 64
return aws_add_u64_checked(a, b, AWS_CAST_SIZE_PTR_TO_U64 r);
return aws_add_u64_checked(a, b, (uint64_t *)r);
#else
# error "Target not supported"
#endif
}

AWS_STATIC_IMPL size_t aws_sub_size_saturating(size_t a, size_t b) {
#if SIZE_BITS == 32
return aws_sub_u32_saturating(a, b);
return (size_t)aws_sub_u32_saturating(a, b);
#elif SIZE_BITS == 64
return AWS_CAST_U64_TO_SIZE aws_sub_u64_saturating(a, b);
return (size_t)aws_sub_u64_saturating(a, b);
#else
# error "Target not supported"
#endif
}

AWS_STATIC_IMPL int aws_sub_size_checked(size_t a, size_t b, size_t *r) {
#if SIZE_BITS == 32
return aws_sub_u32_checked(a, b, r);
return aws_sub_u32_checked(a, b, (uint32_t *)r);
#elif SIZE_BITS == 64
return aws_sub_u64_checked(a, b, AWS_CAST_SIZE_PTR_TO_U64 r);
return aws_sub_u64_checked(a, b, (uint64_t *)r);
#else
# error "Target not supported"
#endif
Expand Down Expand Up @@ -206,6 +195,8 @@ AWS_STATIC_IMPL int aws_round_up_to_power_of_two(size_t n, size_t *result) {

#ifdef _MSC_VER
# pragma warning(pop)
#elif defined(__GNUC__)
# pragma GCC diagnostic pop
#endif /* _MSC_VER */

AWS_STATIC_IMPL uint8_t aws_min_u8(uint8_t a, uint8_t b) {
Expand Down Expand Up @@ -304,10 +295,6 @@ AWS_STATIC_IMPL double aws_max_double(double a, double b) {
return a > b ? a : b;
}

#undef AWS_CAST_SIZE_TO_U64
#undef AWS_CAST_SIZE_PTR_TO_U64
#undef AWS_CAST_U64_TO_SIZE

AWS_EXTERN_C_END

#endif /* AWS_COMMON_MATH_INL */

0 comments on commit d80a18d

Please sign in to comment.