Skip to content

🧹 [Refactor IEEE 80-bit float conversion using std::ldexp]#283

Open
segin wants to merge 1 commit intomasterfrom
code-health-ieee80-ldexp-3068015473548146780
Open

🧹 [Refactor IEEE 80-bit float conversion using std::ldexp]#283
segin wants to merge 1 commit intomasterfrom
code-health-ieee80-ldexp-3068015473548146780

Conversation

@segin
Copy link
Copy Markdown
Owner

@segin segin commented Apr 8, 2026

🎯 What: Refactored the custom IEEE 80-bit float to double conversion routine in ChunkDemuxer::ieee80ToDouble to use std::ldexp.
💡 Why: Using std::ldexp from the standard <cmath> library is safer, cleaner, and avoids potential intermediate precision loss caused by std::pow and integer division scaling factors when calculating floating-point mantissa and exponents. It avoids needing to pull in a large external dependency for a single 10-byte conversion routine.
Verification: Verified math equivalence mathematically and via isolated ad-hoc tests proving std::ldexp produces identical output. Confirmed ChunkDemuxer.o compiles cleanly in the main build chain.
Result: Improved code readability, safety, and health by utilizing standard library functionality instead of custom floating point arithmetic reconstruction.


PR created automatically by Jules for task 3068015473548146780 started by @segin

Replaced the manual custom exponent arithmetic (`std::pow` and division)
used to construct the final double in `ChunkDemuxer::ieee80ToDouble`
with the standard library function `std::ldexp`. This improves
readability, guarantees correct platform-independent behavior without
intermediate rounding issues, and simplifies the codebase.

Co-authored-by: segin <480709+segin@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings April 8, 2026 23:43
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors AIFF’s IEEE 80-bit extended float → double conversion in ChunkDemuxer::ieee80ToDouble to use the standard-library std::ldexp scaling approach rather than std::pow plus an intermediate normalization.

Changes:

  • Replaced mantissa normalization + std::pow(2.0, ...) with a single std::ldexp(mantissa, exp) call.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

// Convert to double
double result = static_cast<double>(mantissa) / (1ULL << 63);
result *= std::pow(2.0, static_cast<int>(exponent) - 16383);
double result = std::ldexp(static_cast<double>(mantissa), exponent - 16383 - 63);
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

exponent is a uint16_t; the expression exponent - 16383 - 63 relies on integer promotions and may become unsigned arithmetic on platforms where int can’t represent all uint16_t values (e.g., 16-bit int), causing wraparound and incorrect scaling. To preserve the previous semantics and avoid implementation-defined conversions into std::ldexp’s int exponent parameter, cast exponent to int (or assign to an int local) before doing the bias/shift subtraction.

Suggested change
double result = std::ldexp(static_cast<double>(mantissa), exponent - 16383 - 63);
const int adjusted_exponent = static_cast<int>(exponent) - 16383 - 63;
double result = std::ldexp(static_cast<double>(mantissa), adjusted_exponent);

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the IEEE 80-bit float to double conversion in ChunkDemuxer.cpp by replacing manual power calculations with std::ldexp. A review comment suggests re-adding an explicit cast for the exponent to prevent potential unsigned underflow during subtraction.

// Convert to double
double result = static_cast<double>(mantissa) / (1ULL << 63);
result *= std::pow(2.0, static_cast<int>(exponent) - 16383);
double result = std::ldexp(static_cast<double>(mantissa), exponent - 16383 - 63);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The removal of the explicit static_cast<int>(exponent) introduces a potential risk of unsigned underflow if exponent is an unsigned type that does not promote to int (e.g., uint32_t). In the original code, the cast ensured that the subtraction exponent - 16383 was performed using signed arithmetic. Without it, if exponent is uint32_t and its value is less than 16446, the result will wrap around to a very large positive value, causing std::ldexp to return infinity or overflow. Even if exponent is uint16_t, keeping the cast is safer and more explicit about the intention to perform signed arithmetic.

Suggested change
double result = std::ldexp(static_cast<double>(mantissa), exponent - 16383 - 63);
double result = std::ldexp(static_cast<double>(mantissa), static_cast<int>(exponent) - 16383 - 63);

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