Skip to content

Add IEEE 754 special value (+/-Inf, +/-NaN) support and tests to Highway math functions - #3363

Open
copybara-service[bot] wants to merge 1 commit into
masterfrom
test_977949605
Open

copybara-service[bot] wants to merge 1 commit into
masterfrom
test_977949605

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

Add IEEE 754 special value (+/-Inf, +/-NaN) support and tests to Highway math functions

This change ensures that Highway math functions conform to IEEE 754 / C11 Annex F specifications for special inputs (+/-Inf, +/-NaN) and adds comprehensive unit tests covering these cases:

  • Erf: Added explicit NaN handling to prevent x86 minps/minpd semantics from clamping NaN to +1.0 (+/-Inf handled implicitly via range clamping).
  • Exp / Exp2: Added explicit handling for +Inf / overflow (preventing range reduction arithmetic cancellation into NaN) and NaN (preventing underflow flush to +0.0).
  • Log / Log2 / Log10: Added explicit handling for negative inputs (NaN), +/-0 (-Inf), +Inf (+Inf), and NaN (NaN).
  • Tanh / Expm1 / Log1p: Verified that special inputs (+/-Inf, +/-NaN) are handled implicitly by the underlying arithmetic and range reduction without requiring extra checks.
  • Tests: Added TestSpecialValues in math_test.cc and TestTanhSpecialValues in math_hyper_test.cc covering float32 and float64 across all vector widths.

Performance Impact:

  • Tanh, Expm1, Log1p: 0% overhead (0 instructions added).
  • Erf: Negligible overhead (1 compare + 1 blend).
  • Exp / Exp2: ~2-4% throughput overhead (2 compares + 2 blends).
  • Log / Log2 / Log10: ~3-5% throughput overhead (4 compares + 4 blends).
    Critical-path latency impact is negligible (0-1 cycle) on superscalar architectures because comparison masks depend solely on the input register and are evaluated by out-of-order execution in parallel with the arithmetic pipeline. Workloads requiring maximum throughput on finite inputs can continue using the Fast* variants (e.g., FastExp, FastLog).

…way math functions

This change ensures that Highway math functions conform to IEEE 754 / C11 Annex F specifications for special inputs (+/-Inf, +/-NaN) and adds comprehensive unit tests covering these cases:

- Erf: Added explicit NaN handling to prevent x86 minps/minpd semantics from clamping NaN to +1.0 (+/-Inf handled implicitly via range clamping).
- Exp / Exp2: Added explicit handling for +Inf / overflow (preventing range reduction arithmetic cancellation into NaN) and NaN (preventing underflow flush to +0.0).
- Log / Log2 / Log10: Added explicit handling for negative inputs (NaN), +/-0 (-Inf), +Inf (+Inf), and NaN (NaN).
- Tanh / Expm1 / Log1p: Verified that special inputs (+/-Inf, +/-NaN) are handled implicitly by the underlying arithmetic and range reduction without requiring extra checks.
- Tests: Added TestSpecialValues in math_test.cc and TestTanhSpecialValues in math_hyper_test.cc covering float32 and float64 across all vector widths.

Performance Impact:
- Tanh, Expm1, Log1p: 0% overhead (0 instructions added).
- Erf: Negligible overhead (1 compare + 1 blend).
- Exp / Exp2: ~2-4% throughput overhead (2 compares + 2 blends).
- Log / Log2 / Log10: ~3-5% throughput overhead (4 compares + 4 blends).
Critical-path latency impact is negligible (0-1 cycle) on superscalar architectures because comparison masks depend solely on the input register and are evaluated by out-of-order execution in parallel with the arithmetic pipeline. Workloads requiring maximum throughput on finite inputs can continue using the Fast* variants (e.g., FastExp, FastLog).

PiperOrigin-RevId: 977949605
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.

1 participant