Skip to content

Add fltflt rounding and fmod functions#1129

Open
tbensonatl wants to merge 4 commits intomainfrom
feature/add-fltflt-round-fmod
Open

Add fltflt rounding and fmod functions#1129
tbensonatl wants to merge 4 commits intomainfrom
feature/add-fltflt-round-fmod

Conversation

@tbensonatl
Copy link
Collaborator

Add support for the following float-float (fltflt) functions:

  • Round toward nearest, with ties toward even
  • Truncate toward zero
  • Truncate toward negative infinity
  • fmod (floating point remainder)

Also includes are new unit tests and benchmarks for the newly introduced functions.

Also add fltflt_add_same_sign(), with is more efficient than fltflt_add()
for the case where we know both inputs have the same sign.

Signed-off-by: Thomas Benson <tbenson@nvidia.com>
Signed-off-by: Thomas Benson <tbenson@nvidia.com>
Signed-off-by: Thomas Benson <tbenson@nvidia.com>
Signed-off-by: Thomas Benson <tbenson@nvidia.com>
@tbensonatl tbensonatl self-assigned this Feb 27, 2026
@copy-pr-bot
Copy link

copy-pr-bot bot commented Feb 27, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 27, 2026

Greptile Summary

Adds comprehensive float-float arithmetic rounding and modulo functions with proper IEEE-754 semantics. The implementation includes fltflt_round_to_nearest() with banker's rounding (round half to even), fltflt_round_toward_zero() for truncation, fltflt_floor() for rounding toward negative infinity, and fltflt_fmod() for floating-point remainder.

Key implementation details:

  • Proper handling of the fast path (|hi| < 2^23) where hi may have fractional parts
  • Correct treatment of the slow path (|hi| >= 2^23) where hi is already an integer
  • Edge case handling for when hi is exactly an integer but lo has opposite sign
  • Round-to-nearest uses tie-breaking to even when exactly at 0.5
  • Constructors modernized with constexpr and = default
  • Added fltflt_add_same_sign() optimization (11 FLOPs vs 20 FLOPs)

Testing and benchmarking:

  • Comprehensive unit tests covering edge cases, tie-breaking, boundary conditions, and large values
  • Performance benchmarks added for all new functions comparing fltflt against float/double baselines

Confidence Score: 5/5

  • Safe to merge - well-implemented mathematical functions with comprehensive tests
  • Implementation is mathematically sound with proper IEEE-754 semantics, comprehensive unit tests covering edge cases, and follows existing code patterns
  • No files require special attention

Important Files Changed

Filename Overview
include/matx/kernels/fltflt.h Adds fltflt rounding functions (round_to_nearest, round_toward_zero, floor) and fmod with proper tie-breaking and edge case handling
test/00_misc/FloatFloatTests.cu Comprehensive unit tests for new rounding functions covering edge cases, tie-breaking, and boundary conditions
bench/00_misc/fltflt_arithmetic.cu Adds benchmark kernels for round, fmod, trunc, floor, and cast operations with proper ILP and timing instrumentation
bench/scripts/run_fltflt_benchmarks.py Updates benchmark list to include new functions in test execution and reporting

Last reviewed commit: 0651eb6

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

if (idx < size) {
T val[ILP_FACTOR];
//const T init_val = static_cast<T>(std::numbers::e);
const T init_val = static_cast<T>(33554432.5);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constexpr?

// given in Algorithm 14.1 of "Handbook of Floating-Point Arithmetic" by Muller et al. Rather
// than include a conditional on the magnitude of a and b to use fltflt_fast_two_sum(), we
// use fltflt_two_sum() at the cost of more FLOPs but without a branch.
static __MATX_HOST__ __MATX_DEVICE__ __MATX_INLINE__ fltflt fltflt_add_same_sign(fltflt a, fltflt b) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would someone use this function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently you would likely call it directly in a kernel. I plan on switching the sar_bp operator to use this function when computing a distance because in that case we are adding squared values and thus we know they are non-negative. I will also benchmark if it is worth adding the conditionals required to call this function when appropriate in operator+(), but I suspect the answer is no.

@cliffburdick
Copy link
Collaborator

/build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants