Skip to content

[libc][math][c23] Fix undefined behavior in expxf16.h #112734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2024

Conversation

overmighty
Copy link
Member

Fixes the left-shifting of potentially negative signed integers.

Fixes the left-shifting of potentially negative signed integers.
@overmighty overmighty requested review from jhuber6 and lntue October 17, 2024 15:51
@overmighty overmighty changed the title [libc] Fix undefined behavior in expxf16.h [libc][math][c23] Fix undefined behavior in expxf16.h Oct 17, 2024
@llvmbot llvmbot added the libc label Oct 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 17, 2024

@llvm/pr-subscribers-libc

Author: OverMighty (overmighty)

Changes

Fixes the left-shifting of potentially negative signed integers.


Full diff: https://github.com/llvm/llvm-project/pull/112734.diff

1 Files Affected:

  • (modified) libc/src/math/generic/expxf16.h (+4-4)
diff --git a/libc/src/math/generic/expxf16.h b/libc/src/math/generic/expxf16.h
index 8de329bd2ab07f..aba99a2914b41d 100644
--- a/libc/src/math/generic/expxf16.h
+++ b/libc/src/math/generic/expxf16.h
@@ -108,8 +108,8 @@ LIBC_INLINE ExpRangeReduction exp2_range_reduction(float16 x) {
   float xf = x;
   float kf = fputil::nearest_integer(xf * 0x1.0p+3f);
   int x_hi_mid = static_cast<int>(kf);
-  int x_hi = x_hi_mid >> 3;
-  int x_mid = x_hi_mid & 0x7;
+  unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3;
+  unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7;
   // lo = x - (hi + mid) = round(x * 2^3) * (-2^(-3)) + x
   float lo = fputil::multiply_add(kf, -0x1.0p-3f, xf);
 
@@ -155,8 +155,8 @@ LIBC_INLINE ExpRangeReduction exp10_range_reduction(float16 x) {
   float xf = x;
   float kf = fputil::nearest_integer(xf * (LOG2F_10 * 0x1.0p+3f));
   int x_hi_mid = static_cast<int>(kf);
-  int x_hi = x_hi_mid >> 3;
-  int x_mid = x_hi_mid & 0x7;
+  unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3;
+  unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7;
   // lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x
   float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf);
 

Comment on lines 109 to +112
float kf = fputil::nearest_integer(xf * 0x1.0p+3f);
int x_hi_mid = static_cast<int>(kf);
int x_hi = x_hi_mid >> 3;
int x_mid = x_hi_mid & 0x7;
unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3;
unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7;
Copy link
Member Author

@overmighty overmighty Oct 17, 2024

Choose a reason for hiding this comment

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

Casting kf (potentially negative float) to unsigned directly would also be UB: https://eel.is/c++draft/conv.fpint#1. Hence why I left x_hi_mid as int.

We really only need x_hi to be unsigned. I made x_mid unsigned too for consistency.

@overmighty overmighty merged commit 87645e9 into llvm:main Oct 17, 2024
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants