Repository navigation
Add integer constructors to fixed-point types to avoid soft-float - #2197
sutaburosu wants to merge 2 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdded integer-based constructors to many fixed-point types in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Comment Tip CodeRabbit can approve the review once all CodeRabbit's comments are resolved.Enable the |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/fl/stl/fixed_point/s24x8.h`:
- Around line 26-28: The integer constructor s24x8(int n) currently does the
multiplication in 32-bit signed space which can overflow; change it to perform
the multiply in a wider signed type (e.g., i64) and then narrow to i32 when
assigning to mValue using static_cast<i32>(static_cast<i64>(n) * SCALE) so the
intermediate product cannot overflow (follow the same pattern used elsewhere in
this class for safe widening).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 496349cc-077a-429c-9065-24535459577f
📒 Files selected for processing (12)
src/fl/stl/fixed_point/s12x4.hsrc/fl/stl/fixed_point/s16x16.hsrc/fl/stl/fixed_point/s24x8.hsrc/fl/stl/fixed_point/s4x12.hsrc/fl/stl/fixed_point/s8x24.hsrc/fl/stl/fixed_point/s8x8.hsrc/fl/stl/fixed_point/u12x4.hsrc/fl/stl/fixed_point/u16x16.hsrc/fl/stl/fixed_point/u24x8.hsrc/fl/stl/fixed_point/u4x12.hsrc/fl/stl/fixed_point/u8x24.hsrc/fl/stl/fixed_point/u8x8.h
| // Construct from an integer (exact, avoids float math / soft-float helpers). | ||
| constexpr s24x8(int n) | ||
| : mValue(static_cast<i32>(n) * SCALE) {} |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Verify constructor expression in this file.
rg -nP 'constexpr\s+s24x8\(int n\)|mValue\(static_cast<i32>\(n\)\s*\*\s*SCALE\)' src/fl/stl/fixed_point/s24x8.h
# Compute 32-bit signed safe range for n * SCALE (SCALE=256).
python - <<'PY'
INT32_MIN = -(1 << 31)
INT32_MAX = (1 << 31) - 1
SCALE = 1 << 8
print("safe_n_min:", INT32_MIN // SCALE)
print("safe_n_max:", INT32_MAX // SCALE)
print("overflow_example:", 8388608 * SCALE)
PYRepository: FastLED/FastLED
Length of output: 210
🏁 Script executed:
# Check the header file for type definitions and context
cat -n src/fl/stl/fixed_point/s24x8.h | head -50Repository: FastLED/FastLED
Length of output: 2057
🏁 Script executed:
# Look for SCALE definition and mValue type
rg -B5 -A10 'constexpr\s+s24x8\(int n\)' src/fl/stl/fixed_point/s24x8.hRepository: FastLED/FastLED
Length of output: 650
🏁 Script executed:
# Search for the class definition and member variables
rg -B2 'mValue' src/fl/stl/fixed_point/s24x8.h | head -30Repository: FastLED/FastLED
Length of output: 1277
Avoid signed-overflow UB in the integer constructor.
static_cast<i32>(n) * SCALE is evaluated as signed 32-bit multiplication. Since int can range from −2,147,483,648 to 2,147,483,647 but only values in [−8,388,608, 8,388,607] fit safely into the product before overflow, any n outside this range invokes undefined behavior. Use a wider intermediate before narrowing, consistent with the pattern already used in this class (e.g., line 41-42).
Proposed fix
constexpr s24x8(int n)
- : mValue(static_cast<i32>(n) * SCALE) {}
+ : mValue(static_cast<i32>(static_cast<i64>(n) * SCALE)) {}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/fl/stl/fixed_point/s24x8.h` around lines 26 - 28, The integer constructor
s24x8(int n) currently does the multiplication in 32-bit signed space which can
overflow; change it to perform the multiply in a wider signed type (e.g., i64)
and then narrow to i32 when assigning to mValue using
static_cast<i32>(static_cast<i64>(n) * SCALE) so the intermediate product cannot
overflow (follow the same pattern used elsewhere in this class for safe
widening).
There was a problem hiding this comment.
Pull request overview
Adds non-explicit integer constructors to all 12 fixed-point types (signed and unsigned) so that integer values can be converted to fixed-point without invoking soft-float helpers or using the verbose from_raw() pattern.
Changes:
- Added implicit
constexprconstructors takingunsigned(unsigned types) orint(signed types) across all fixed-point classes. - Removed
explicitfrom the float constructor ofs16x16(likely unintentional).
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/fl/stl/fixed_point/u8x8.h | Add unsigned constructor |
| src/fl/stl/fixed_point/u8x24.h | Add unsigned constructor |
| src/fl/stl/fixed_point/u4x12.h | Add unsigned constructor |
| src/fl/stl/fixed_point/u24x8.h | Add unsigned constructor |
| src/fl/stl/fixed_point/u16x16.h | Add unsigned constructor |
| src/fl/stl/fixed_point/u12x4.h | Add unsigned constructor |
| src/fl/stl/fixed_point/s8x8.h | Add int constructor |
| src/fl/stl/fixed_point/s8x24.h | Add int constructor |
| src/fl/stl/fixed_point/s4x12.h | Add int constructor |
| src/fl/stl/fixed_point/s24x8.h | Add int constructor |
| src/fl/stl/fixed_point/s16x16.h | Add int constructor; also drops explicit from float ctor |
| src/fl/stl/fixed_point/s12x4.h | Add int constructor |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
|
So int it 16 bits on avr and 32 bytes on 32 machines. I'm going to have to ask the ai to investigate and see what the best way forward on this is. |
|
we have complex template types will that will bind to any integer type so we can do the right thing for all integer types. |
|
Hey, this is Clud, Zach's AI assistant. I investigated the int width issue and here is what we have decided on and why. The approach: Template integer constructors using SFINAE instead of hardcoded Key differences from this PR:
The semantic: The helper trait lives in |
|
This has been implemented on master in commit 40b3b52 with the template-based approach described above. The implementation includes constexpr range checking, boundary tests for all 12 types, and signed-to-unsigned integer handling. Closing this PR since the feature has landed. |
|
Thanks, man. I knew you'd do it better. |
…art of #3130, meta #3127) First extraction in the fixed_point.cpp split (#3130). Moves the misc section (lines 4818-5226) into its own file: - Fixed-point scalar type alignment tests - Constexpr scalar multiply/divide tests (s0x32, u0x32) - Integer constructor tests (PR #2197 int width portability) - pow boundary regression tests (#2969) Also extends ci/hooks/check_test_file_pairing.py to allow sibling-split test files. The original 1:1 pairing rule rejected fixed_point_misc.cpp because there is no src/fl/math/fixed_point_misc.h. The new sibling-split allowance walks back the base name via underscore separators and allows the write when the longest base prefix has both an existing paired test file and a matching src header. This unblocks the rest of #3127's splits without weakening the convention for genuinely unpaired test files. Tests pass: bash test fl_math_fixed_point --cpp (1/1, 17/17 build steps). fixed_point.cpp: 5,230 -> 4,816 LOC fixed_point_misc.cpp (new): 440 LOC Part of #3130. Meta tracking issue: #3127. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…art of #3130, meta #3127) (#3135) First extraction in the fixed_point.cpp split (#3130). Moves the misc section (lines 4818-5226) into its own file: - Fixed-point scalar type alignment tests - Constexpr scalar multiply/divide tests (s0x32, u0x32) - Integer constructor tests (PR #2197 int width portability) - pow boundary regression tests (#2969) Also extends ci/hooks/check_test_file_pairing.py to allow sibling-split test files. The original 1:1 pairing rule rejected fixed_point_misc.cpp because there is no src/fl/math/fixed_point_misc.h. The new sibling-split allowance walks back the base name via underscore separators and allows the write when the longest base prefix has both an existing paired test file and a matching src header. This unblocks the rest of #3127's splits without weakening the convention for genuinely unpaired test files. Tests pass: bash test fl_math_fixed_point --cpp (1/1, 17/17 build steps). fixed_point.cpp: 5,230 -> 4,816 LOC fixed_point_misc.cpp (new): 440 LOC Part of #3130. Meta tracking issue: #3127. Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Now you can construct fixed_point from an int without pulling in soft float, without doing the .from_raw() dance.
Summary by CodeRabbit