Add checked int32 arithmetic #5

Merged
fputs merged 1 commit from fputs/i32-arith into master 2026-11-05 09:44:28 -08:00 AGit
Owner

Adds lf_i32_add, lf_i32_sub, and lf_i32_mul on lfmath.h. They return 0 and write *out only on success. NULL out and signed overflow return -1 and leave *out unchanged.

Overflow is detected with __builtin_add_overflow, __builtin_sub_overflow, and __builtin_mul_overflow. The builtin result is staged so a wrapped value is never stored in *out.

Tested with -DCMAKE_BUILD_TYPE=Debug -DFLINT_SANITIZE=ON -DCMAKE_C_EXTENSIONS=OFF. ctest --label-exclude slow: 25/25 passed.

Adds `lf_i32_add`, `lf_i32_sub`, and `lf_i32_mul` on `lfmath.h`. They return `0` and write `*out` only on success. NULL `out` and signed overflow return `-1` and leave `*out` unchanged. Overflow is detected with `__builtin_add_overflow`, `__builtin_sub_overflow`, and `__builtin_mul_overflow`. The builtin result is staged so a wrapped value is never stored in `*out`. Tested with `-DCMAKE_BUILD_TYPE=Debug -DFLINT_SANITIZE=ON -DCMAKE_C_EXTENSIONS=OFF`. `ctest --label-exclude slow`: 25/25 passed.
Add checked int32 arithmetic
All checks were successful
test / test (pull_request) Successful in 13s
test / sanitize (pull_request) Successful in 14s
3efa917d16
fputs force-pushed fputs/i32-arith from 3efa917d16
All checks were successful
test / test (pull_request) Successful in 13s
test / sanitize (pull_request) Successful in 14s
to 81857f7455
All checks were successful
test / test (pull_request) Successful in 13s
test / sanitize (pull_request) Successful in 16s
2026-11-05 09:39:15 -08:00
Compare
fputs merged commit a068388deb into master 2026-11-05 09:44:28 -08:00
fputs referenced this pull request from a commit 2026-11-05 09:44:29 -08:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
fputs/libflint!5
No description provided.