Skip to content

Commit 25ce25b

Browse files
maleyva1narimiran
authored andcommitted
fix #23817; Use __builtin_saddl_overflow variants for arm-none-eabi-gcc. (#23835)
Provides a fix for #23817. With target `arm-none-eabi`, GCC defines `int32_t` to `long int`. Nim uses `__builtin_sadd_overflow` for 32-bit targets, but this emits warnings on GCC releases 13 and under, while generating an error on GCC 14. More info regarding this [here](https://gcc.gnu.org/gcc-14/porting_to.html#c) and [here](https://gcc.gnu.org/pipermail/gcc-cvs/2023-December/394351.html). The proposed PR attempts to address this issue for these targets by defining the `nimAddInt`, `nimSubInt`, and `nimMulInt` macros to use the appropriate compiler intrinsics for this platform. As for as we know, the LLVM toolchain for bare metal Arm does not define `int32_t` as `long int` and has no need for this patch. Thus, we only define the above macros for GCC targeting `arm-non-eabi`. (cherry picked from commit c5b206d)
1 parent 7503bed commit 25ce25b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/nimbase.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ __GNUC__
1414
__TINYC__
1515
__clang__
1616
__AVR__
17+
__arm__
1718
*/
1819

1920

@@ -573,9 +574,16 @@ NIM_STATIC_ASSERT(sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8, "P
573574
#define nimMulInt64(a, b, res) __builtin_smulll_overflow(a, b, (long long int*)res)
574575

575576
#if NIM_INTBITS == 32
576-
#define nimAddInt(a, b, res) __builtin_sadd_overflow(a, b, res)
577-
#define nimSubInt(a, b, res) __builtin_ssub_overflow(a, b, res)
578-
#define nimMulInt(a, b, res) __builtin_smul_overflow(a, b, res)
577+
#if defined(__arm__) && defined(__GNUC__)
578+
/* arm-none-eabi-gcc targets defines int32_t as long int */
579+
#define nimAddInt(a, b, res) __builtin_saddl_overflow(a, b, res)
580+
#define nimSubInt(a, b, res) __builtin_ssubl_overflow(a, b, res)
581+
#define nimMulInt(a, b, res) __builtin_smull_overflow(a, b, res)
582+
#else
583+
#define nimAddInt(a, b, res) __builtin_sadd_overflow(a, b, res)
584+
#define nimSubInt(a, b, res) __builtin_ssub_overflow(a, b, res)
585+
#define nimMulInt(a, b, res) __builtin_smul_overflow(a, b, res)
586+
#endif
579587
#else
580588
/* map it to the 'long long' variant */
581589
#define nimAddInt(a, b, res) __builtin_saddll_overflow(a, b, (long long int*)res)

0 commit comments

Comments
 (0)