[clang] [HLSL] Implement frexp() (PR #235154)

Dan Brown via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 19:55:46 PST 2026


https://github.com/danbrown-amd updated https://github.com/llvm/llvm-project/pull/235154

>From f8f81e6da8006f5a17f6d77c9045e5de31e346b0 Mon Sep 17 00:00:00 2001
From: danbrown-amd <danbrown at amd.com>
Date: Fri, 16 Oct 2026 18:15:51 -0600
Subject: [PATCH 1/2] [HLSL] Implement frexp()

Resolves #99119, #220654

Assisted-by: Claude Opus 5
---
 .../lib/Headers/hlsl/hlsl_intrinsic_helpers.h |  33 +++
 clang/lib/Headers/hlsl/hlsl_intrinsics.h      |  38 ++++
 clang/test/CodeGenHLSL/builtins/frexp.hlsl    |  96 ++++++++
 .../test/CodeGenHLSL/builtins/frexp_mat.hlsl  | 208 ++++++++++++++++++
 .../test/SemaHLSL/BuiltIns/frexp-errors.hlsl  |  50 +++++
 .../SemaHLSL/BuiltIns/frexp_mat-errors.hlsl   |  12 +
 6 files changed, 437 insertions(+)
 create mode 100644 clang/test/CodeGenHLSL/builtins/frexp.hlsl
 create mode 100644 clang/test/CodeGenHLSL/builtins/frexp_mat.hlsl
 create mode 100644 clang/test/SemaHLSL/BuiltIns/frexp-errors.hlsl
 create mode 100644 clang/test/SemaHLSL/BuiltIns/frexp_mat-errors.hlsl

diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
index 5bcce0f68c3865..1c63fd9aff02e9 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
@@ -104,6 +104,39 @@ template <typename T> constexpr T fmod_impl(T X, T Y) {
 #endif
 }
 
+// Returns the mantissa of X, which has the sign of X and an absolute value in
+// [0.5, 1), and writes the exponent to Exp such that X = Mant * 2^Exp. X can be
+// a scalar, vector or matrix of float.
+template <typename T> constexpr T frexp_impl(T X, out T Exp) {
+  auto Bits = bit_cast<uint>(X);
+  auto ExpBits = Bits & 0x7f800000u;
+  // Zeros, denormals, infinities and NaNs are returned as they are, with an
+  // exponent of 0. These are exactly the cases where the exponent field is 0 or
+  // all ones, so ExpBits - 1 is at least 0x7f7fffff, whether it wraps around or
+  // not. This avoids ||, which is not supported on vectors.
+  auto IsSpecial = (ExpBits - 1u) >= 0x7f7fffffu;
+  // Keep the sign and the fraction, and replace the exponent with the one for
+  // 0.5, which scales X into [0.5, 1).
+  auto MantBits = (Bits & 0x807fffffu) | 0x3f000000u;
+  T Mant = select(IsSpecial, X, bit_cast<float>(MantBits));
+  auto RawExp = (ExpBits >> 23u) - 126u;
+  Exp = select(IsSpecial, static_cast<T>(0),
+               static_cast<T>(bit_cast<int>(RawExp)));
+  return Mant;
+}
+
+// Matrices do not support the bitwise operators, so frexp_impl works on the
+// elements of the matrix as a vector.
+template <int R, int C>
+constexpr matrix<float, R, C> frexp_impl(matrix<float, R, C> X,
+                                         out matrix<float, R, C> Exp) {
+  vector<float, R * C> VecExp;
+  vector<float, R * C> VecMant =
+      frexp_impl(__builtin_bit_cast(vector<float, R * C>, X), VecExp);
+  Exp = __builtin_bit_cast(matrix<float, R, C>, VecExp);
+  return __builtin_bit_cast(matrix<float, R, C>, VecMant);
+}
+
 template <typename T> constexpr T smoothstep_impl(T Min, T Max, T X) {
 #if (__has_builtin(__builtin_spirv_smoothstep))
   return __builtin_spirv_smoothstep(Min, Max, X);
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 9ad6ba2f087d86..f331be96ac8c92 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -220,6 +220,44 @@ firstbithigh(vector<T, N> X) {
   return __detail::firstbithigh_impl<vector<uint, N>, vector<T, N>, 64>(X);
 }
 
+//===----------------------------------------------------------------------===//
+// frexp builtins
+//===----------------------------------------------------------------------===//
+
+/// \fn T frexp(T X, out T Exp)
+/// \brief Returns the mantissa and exponent of the specified floating-point
+/// value.
+/// \param X The specified floating-point value.
+/// \param Exp The returned exponent of X.
+///
+/// The mantissa has the same sign as X and an absolute value in [0.5, 1), such
+/// that X = mantissa * 2^Exp. If X is 0, both the mantissa and the exponent
+/// are 0.
+inline float frexp(float X, out float Exp) {
+  return __detail::frexp_impl(X, Exp);
+}
+inline float2 frexp(float2 X, out float2 Exp) {
+  return __detail::frexp_impl(X, Exp);
+}
+inline float3 frexp(float3 X, out float3 Exp) {
+  return __detail::frexp_impl(X, Exp);
+}
+inline float4 frexp(float4 X, out float4 Exp) {
+  return __detail::frexp_impl(X, Exp);
+}
+template <int N>
+inline vector<__detail::enable_if_t<(N > 4), float>, N>
+frexp(vector<__detail::enable_if_t<(N > 4), float>, N> X,
+      out __detail::type_identity_t<vector<float, N>> Exp) {
+  return __detail::frexp_impl(X, Exp);
+}
+template <int R, int C>
+inline matrix<float, R, C>
+frexp(matrix<float, R, C> X,
+      out __detail::type_identity_t<matrix<float, R, C>> Exp) {
+  return __detail::frexp_impl(X, Exp);
+}
+
 //===----------------------------------------------------------------------===//
 // mul builtins
 //===----------------------------------------------------------------------===//
diff --git a/clang/test/CodeGenHLSL/builtins/frexp.hlsl b/clang/test/CodeGenHLSL/builtins/frexp.hlsl
new file mode 100644
index 00000000000000..d627b53abb6071
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/frexp.hlsl
@@ -0,0 +1,96 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-library %s -O1 -emit-llvm -o - | FileCheck %s \
+// RUN:   --check-prefixes=CHECK,DXCHECK
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   spirv-unknown-vulkan-library %s -O1 -emit-llvm -o - | FileCheck %s \
+// RUN:   --check-prefixes=CHECK,SPVCHECK
+
+// The only difference between the targets is the calling convention on the
+// definitions. It is captured once, in the first function, as [[FN_TYPE]].
+
+// DXCHECK: define hidden [[FN_TYPE:]]noundef nofpclass(nan inf) float @{{.*}}test_frexp_f{{.*}}(
+// SPVCHECK: define hidden [[FN_TYPE:spir_func ]]noundef nofpclass(nan inf) float @{{.*}}test_frexp_f{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast float %X to i32
+// CHECK:      [[EXPBITS:%.*]] = and i32 [[BITS]], 2139095040
+// CHECK:      and i32 [[BITS]], -2139095041
+// CHECK:      or {{.*}}i32 {{.*}}, 1056964608
+// CHECK:      [[MANT:%.*]] = select {{.*}}, float %X, float {{.*}}
+// CHECK:      lshr {{.*}}i32 [[EXPBITS]], 23
+// CHECK:      sitofp {{.*}}i32 {{.*}} to float
+// CHECK:      [[EXP:%.*]] = select {{.*}}, float 0.000000e+00, float {{.*}}
+// CHECK-NEXT: store float [[EXP]], ptr %E
+// CHECK-NEXT: ret float [[MANT]]
+float test_frexp_f(float X, out float E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <2 x float> @{{.*}}test_frexp_f2{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <2 x float> %X to <2 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <2 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <2 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<2 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <2 x float> %X, <2 x float> {{.*}}
+// CHECK:      lshr {{.*}}<2 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<2 x i32> {{.*}} to <2 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <2 x float> zeroinitializer, <2 x float> {{.*}}
+// CHECK-NEXT: store <2 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <2 x float> [[MANT]]
+float2 test_frexp_f2(float2 X, out float2 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <3 x float> @{{.*}}test_frexp_f3{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <3 x float> %X to <3 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <3 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <3 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<3 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <3 x float> %X, <3 x float> {{.*}}
+// CHECK:      lshr {{.*}}<3 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<3 x i32> {{.*}} to <3 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <3 x float> zeroinitializer, <3 x float> {{.*}}
+// CHECK-NEXT: store <3 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <3 x float> [[MANT]]
+float3 test_frexp_f3(float3 X, out float3 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <4 x float> @{{.*}}test_frexp_f4{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <4 x float> %X to <4 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <4 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<4 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <4 x float> %X, <4 x float> {{.*}}
+// CHECK:      lshr {{.*}}<4 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<4 x i32> {{.*}} to <4 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <4 x float> zeroinitializer, <4 x float> {{.*}}
+// CHECK-NEXT: store <4 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <4 x float> [[MANT]]
+float4 test_frexp_f4(float4 X, out float4 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <5 x float> @{{.*}}test_frexp_f5{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <5 x float> %X to <5 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <5 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <5 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<5 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <5 x float> %X, <5 x float> {{.*}}
+// CHECK:      lshr {{.*}}<5 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<5 x i32> {{.*}} to <5 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <5 x float> zeroinitializer, <5 x float> {{.*}}
+// CHECK-NEXT: store <5 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <5 x float> [[MANT]]
+vector<float, 5> test_frexp_f5(vector<float, 5> X, out vector<float, 5> E) {
+  return frexp(X, E);
+}
+
+// The exponent can also be an integer, as with DXC. It is computed as a float
+// and converted when it is written back, which folds away here because the
+// exponent is an exact integer.
+
+// CHECK: define {{.*}}test_frexp_int_exp_scalar
+// CHECK:      [[EXPI:%.*]] = select {{.*}}, i32 0, i32 {{.*}}
+// CHECK-NEXT: store i32 [[EXPI]], ptr %E
+// CHECK-NEXT: ret float
+float test_frexp_int_exp_scalar(float X, out int E) { return frexp(X, E); }
+
+// CHECK: define {{.*}}test_frexp_int_exp_long
+// CHECK:      [[EXPI:%.*]] = select {{.*}}, <8 x i32> zeroinitializer, <8 x i32> {{.*}}
+// CHECK-NEXT: store <8 x i32> [[EXPI]], ptr %E
+// CHECK-NEXT: ret <8 x float>
+vector<float, 8> test_frexp_int_exp_long(vector<float, 8> X,
+                                         out vector<int, 8> E) {
+  return frexp(X, E);
+}
diff --git a/clang/test/CodeGenHLSL/builtins/frexp_mat.hlsl b/clang/test/CodeGenHLSL/builtins/frexp_mat.hlsl
new file mode 100644
index 00000000000000..8e71aa3d811940
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/frexp_mat.hlsl
@@ -0,0 +1,208 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-library %s -O1 -emit-llvm -o - | FileCheck %s \
+// RUN:   --check-prefixes=CHECK,DXCHECK
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   spirv-unknown-vulkan-library %s -O1 -emit-llvm -o - | FileCheck %s \
+// RUN:   --check-prefixes=CHECK,SPVCHECK
+
+// Matrices are handled as vectors with one element per matrix element, so the
+// code is the same as for a vector of that size.
+
+// The only difference between the targets is the calling convention on the
+// definitions. It is captured once, in the first function, as [[FN_TYPE]].
+
+// DXCHECK: define hidden [[FN_TYPE:]]noundef nofpclass(nan inf) <2 x float> @{{.*}}test_frexp_f1x2{{.*}}(
+// SPVCHECK: define hidden [[FN_TYPE:spir_func ]]noundef nofpclass(nan inf) <2 x float> @{{.*}}test_frexp_f1x2{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <2 x float> %X to <2 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <2 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <2 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<2 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <2 x float> %X, <2 x float> {{.*}}
+// CHECK:      lshr {{.*}}<2 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<2 x i32> {{.*}} to <2 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <2 x float> zeroinitializer, <2 x float> {{.*}}
+// CHECK-NEXT: store <2 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <2 x float> [[MANT]]
+float1x2 test_frexp_f1x2(float1x2 X, out float1x2 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <3 x float> @{{.*}}test_frexp_f1x3{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <3 x float> %X to <3 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <3 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <3 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<3 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <3 x float> %X, <3 x float> {{.*}}
+// CHECK:      lshr {{.*}}<3 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<3 x i32> {{.*}} to <3 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <3 x float> zeroinitializer, <3 x float> {{.*}}
+// CHECK-NEXT: store <3 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <3 x float> [[MANT]]
+float1x3 test_frexp_f1x3(float1x3 X, out float1x3 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <4 x float> @{{.*}}test_frexp_f1x4{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <4 x float> %X to <4 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <4 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<4 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <4 x float> %X, <4 x float> {{.*}}
+// CHECK:      lshr {{.*}}<4 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<4 x i32> {{.*}} to <4 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <4 x float> zeroinitializer, <4 x float> {{.*}}
+// CHECK-NEXT: store <4 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <4 x float> [[MANT]]
+float1x4 test_frexp_f1x4(float1x4 X, out float1x4 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <2 x float> @{{.*}}test_frexp_f2x1{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <2 x float> %X to <2 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <2 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <2 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<2 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <2 x float> %X, <2 x float> {{.*}}
+// CHECK:      lshr {{.*}}<2 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<2 x i32> {{.*}} to <2 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <2 x float> zeroinitializer, <2 x float> {{.*}}
+// CHECK-NEXT: store <2 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <2 x float> [[MANT]]
+float2x1 test_frexp_f2x1(float2x1 X, out float2x1 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <4 x float> @{{.*}}test_frexp_f2x2{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <4 x float> %X to <4 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <4 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<4 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <4 x float> %X, <4 x float> {{.*}}
+// CHECK:      lshr {{.*}}<4 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<4 x i32> {{.*}} to <4 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <4 x float> zeroinitializer, <4 x float> {{.*}}
+// CHECK-NEXT: store <4 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <4 x float> [[MANT]]
+float2x2 test_frexp_f2x2(float2x2 X, out float2x2 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <6 x float> @{{.*}}test_frexp_f2x3{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <6 x float> %X to <6 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <6 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <6 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<6 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <6 x float> %X, <6 x float> {{.*}}
+// CHECK:      lshr {{.*}}<6 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<6 x i32> {{.*}} to <6 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <6 x float> zeroinitializer, <6 x float> {{.*}}
+// CHECK-NEXT: store <6 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <6 x float> [[MANT]]
+float2x3 test_frexp_f2x3(float2x3 X, out float2x3 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <8 x float> @{{.*}}test_frexp_f2x4{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <8 x float> %X to <8 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <8 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <8 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<8 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <8 x float> %X, <8 x float> {{.*}}
+// CHECK:      lshr {{.*}}<8 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<8 x i32> {{.*}} to <8 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <8 x float> zeroinitializer, <8 x float> {{.*}}
+// CHECK-NEXT: store <8 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <8 x float> [[MANT]]
+float2x4 test_frexp_f2x4(float2x4 X, out float2x4 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <3 x float> @{{.*}}test_frexp_f3x1{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <3 x float> %X to <3 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <3 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <3 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<3 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <3 x float> %X, <3 x float> {{.*}}
+// CHECK:      lshr {{.*}}<3 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<3 x i32> {{.*}} to <3 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <3 x float> zeroinitializer, <3 x float> {{.*}}
+// CHECK-NEXT: store <3 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <3 x float> [[MANT]]
+float3x1 test_frexp_f3x1(float3x1 X, out float3x1 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <6 x float> @{{.*}}test_frexp_f3x2{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <6 x float> %X to <6 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <6 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <6 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<6 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <6 x float> %X, <6 x float> {{.*}}
+// CHECK:      lshr {{.*}}<6 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<6 x i32> {{.*}} to <6 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <6 x float> zeroinitializer, <6 x float> {{.*}}
+// CHECK-NEXT: store <6 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <6 x float> [[MANT]]
+float3x2 test_frexp_f3x2(float3x2 X, out float3x2 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <9 x float> @{{.*}}test_frexp_f3x3{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <9 x float> %X to <9 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <9 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <9 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<9 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <9 x float> %X, <9 x float> {{.*}}
+// CHECK:      lshr {{.*}}<9 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<9 x i32> {{.*}} to <9 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <9 x float> zeroinitializer, <9 x float> {{.*}}
+// CHECK-NEXT: store <9 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <9 x float> [[MANT]]
+float3x3 test_frexp_f3x3(float3x3 X, out float3x3 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <12 x float> @{{.*}}test_frexp_f3x4{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <12 x float> %X to <12 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <12 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <12 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<12 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <12 x float> %X, <12 x float> {{.*}}
+// CHECK:      lshr {{.*}}<12 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<12 x i32> {{.*}} to <12 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <12 x float> zeroinitializer, <12 x float> {{.*}}
+// CHECK-NEXT: store <12 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <12 x float> [[MANT]]
+float3x4 test_frexp_f3x4(float3x4 X, out float3x4 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <4 x float> @{{.*}}test_frexp_f4x1{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <4 x float> %X to <4 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <4 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<4 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <4 x float> %X, <4 x float> {{.*}}
+// CHECK:      lshr {{.*}}<4 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<4 x i32> {{.*}} to <4 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <4 x float> zeroinitializer, <4 x float> {{.*}}
+// CHECK-NEXT: store <4 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <4 x float> [[MANT]]
+float4x1 test_frexp_f4x1(float4x1 X, out float4x1 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <8 x float> @{{.*}}test_frexp_f4x2{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <8 x float> %X to <8 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <8 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <8 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<8 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <8 x float> %X, <8 x float> {{.*}}
+// CHECK:      lshr {{.*}}<8 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<8 x i32> {{.*}} to <8 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <8 x float> zeroinitializer, <8 x float> {{.*}}
+// CHECK-NEXT: store <8 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <8 x float> [[MANT]]
+float4x2 test_frexp_f4x2(float4x2 X, out float4x2 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <12 x float> @{{.*}}test_frexp_f4x3{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <12 x float> %X to <12 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <12 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <12 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<12 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <12 x float> %X, <12 x float> {{.*}}
+// CHECK:      lshr {{.*}}<12 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<12 x i32> {{.*}} to <12 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <12 x float> zeroinitializer, <12 x float> {{.*}}
+// CHECK-NEXT: store <12 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <12 x float> [[MANT]]
+float4x3 test_frexp_f4x3(float4x3 X, out float4x3 E) { return frexp(X, E); }
+
+// CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <16 x float> @{{.*}}test_frexp_f4x4{{.*}}(
+// CHECK:      [[BITS:%.*]] = bitcast <16 x float> %X to <16 x i32>
+// CHECK:      [[EXPBITS:%.*]] = and <16 x i32> [[BITS]], splat (i32 2139095040)
+// CHECK:      and <16 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      or {{.*}}<16 x i32> {{.*}}, splat (i32 1056964608)
+// CHECK:      [[MANT:%.*]] = select {{.*}}, <16 x float> %X, <16 x float> {{.*}}
+// CHECK:      lshr {{.*}}<16 x i32> [[EXPBITS]], splat (i32 23)
+// CHECK:      sitofp {{.*}}<16 x i32> {{.*}} to <16 x float>
+// CHECK:      [[EXP:%.*]] = select {{.*}}, <16 x float> zeroinitializer, <16 x float> {{.*}}
+// CHECK-NEXT: store <16 x float> [[EXP]], ptr %E
+// CHECK-NEXT: ret <16 x float> [[MANT]]
+float4x4 test_frexp_f4x4(float4x4 X, out float4x4 E) { return frexp(X, E); }
diff --git a/clang/test/SemaHLSL/BuiltIns/frexp-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/frexp-errors.hlsl
new file mode 100644
index 00000000000000..676c40c43733f7
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/frexp-errors.hlsl
@@ -0,0 +1,50 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library -fsyntax-only -verify %s
+
+float test_no_second_arg(float X) {
+  return frexp(X);
+  // expected-error at -1 {{no matching function for call to 'frexp'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* 4 {{candidate function not viable: requires 2 arguments, but 1 was provided}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* 2 {{candidate function template not viable: requires 2 arguments, but 1 was provided}}
+}
+
+float test_too_many_args(float X) {
+  float E, F;
+  return frexp(X, E, F);
+  // expected-error at -1 {{no matching function for call to 'frexp'}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* 4 {{candidate function not viable: requires 2 arguments, but 3 were provided}}
+  // expected-note at hlsl/hlsl_intrinsics.h:* 2 {{candidate function template not viable: requires 2 arguments, but 3 were provided}}
+}
+
+float test_exp_rvalue(float X) {
+  return frexp(X, 1.0f);
+  // expected-error at -1 {{cannot bind non-lvalue argument '1.F' to out parameter}}
+}
+
+float test_exp_const(float X) {
+  const float E = 0;
+  return frexp(X, E);
+  // expected-error at -1 {{read-only variable is not assignable}}
+}
+
+float3 test_vector_exp_rvalue(float3 X) {
+  return frexp(X, X * 2);
+  // expected-error at -1 {{cannot bind non-lvalue argument 'X * 2' to out parameter}}
+}
+
+vector<float, 8> test_long_vector_exp_rvalue(vector<float, 8> X) {
+  return frexp(X, X * 2);
+  // expected-error at -1 {{cannot bind non-lvalue argument 'X * 2' to out parameter}}
+}
+
+// Only float is supported, so other types are converted, as with DXC.
+float test_double_input(double X) {
+  float E;
+  return frexp(X, E);
+  // expected-warning at -1 {{implicit conversion loses floating-point precision: 'double' to 'float'}}
+}
+
+float test_int_input(int X) {
+  float E;
+  return frexp(X, E);
+  // expected-warning at -1 {{implicit conversion from 'int' to 'float' may lose precision}}
+}
diff --git a/clang/test/SemaHLSL/BuiltIns/frexp_mat-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/frexp_mat-errors.hlsl
new file mode 100644
index 00000000000000..c9e15dd7d00a16
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/frexp_mat-errors.hlsl
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library -fsyntax-only -verify %s
+
+float2x2 test_exp_rvalue(float2x2 X) {
+  return frexp(X, X * 2);
+  // expected-error at -1 {{cannot bind non-lvalue argument 'X * 2' to out parameter}}
+}
+
+float2x2 test_exp_const(float2x2 X) {
+  const float2x2 E = 0;
+  return frexp(X, E);
+  // expected-error at -1 {{read-only variable is not assignable}}
+}

>From 2c62032c1d5418e08ed2435449a8b30baa844732 Mon Sep 17 00:00:00 2001
From: danbrown-amd <danbrown at amd.com>
Date: Tue, 24 Nov 2026 18:09:11 -0600
Subject: [PATCH 2/2] [HLSL] Normalize denormals in frexp()

Make frexp() return a mantissa with an absolute value in [0.5, 1) and an
exponent such that X = mantissa * 2^exponent for denormal inputs too,
instead of returning the denormal unchanged with an exponent of 0.

The fraction of a denormal is an integer below 2^23, so converting it to
a float is exact. The exponent field of that float gives the position of
the highest set bit and its fraction field is the normalized fraction.
This only uses integer operations on the bits of the input, so it does
not depend on how denormal floats are handled by the target.

Zeros are still returned as they are with an exponent of 0. The result
is unspecified when the exponent would be outside the range of a 32-bit
integer, which is the case for infinities and NaNs.

The check for zeros and non-finite values shifts out the sign instead of
masking it, because masking is turned into fabs, which flushes denormals
on some targets.

Co-Authored-By: Claude Sonnet 5.5 <noreply at anthropic.com>
---
 .../lib/Headers/hlsl/hlsl_intrinsic_helpers.h | 34 +++++--
 clang/lib/Headers/hlsl/hlsl_intrinsics.h      |  6 +-
 clang/test/CodeGenHLSL/builtins/frexp.hlsl    | 35 +++++---
 .../test/CodeGenHLSL/builtins/frexp_mat.hlsl  | 90 ++++++++++++-------
 4 files changed, 114 insertions(+), 51 deletions(-)

diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
index 1c63fd9aff02e9..c6f161d0b26d2d 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
@@ -110,16 +110,32 @@ template <typename T> constexpr T fmod_impl(T X, T Y) {
 template <typename T> constexpr T frexp_impl(T X, out T Exp) {
   auto Bits = bit_cast<uint>(X);
   auto ExpBits = Bits & 0x7f800000u;
-  // Zeros, denormals, infinities and NaNs are returned as they are, with an
-  // exponent of 0. These are exactly the cases where the exponent field is 0 or
-  // all ones, so ExpBits - 1 is at least 0x7f7fffff, whether it wraps around or
-  // not. This avoids ||, which is not supported on vectors.
-  auto IsSpecial = (ExpBits - 1u) >= 0x7f7fffffu;
-  // Keep the sign and the fraction, and replace the exponent with the one for
-  // 0.5, which scales X into [0.5, 1).
-  auto MantBits = (Bits & 0x807fffffu) | 0x3f000000u;
+  auto Fraction = Bits & 0x007fffffu;
+  // Zeros are returned as they are, with an exponent of 0. Infinities and NaNs
+  // have no exponent in the range of a 32-bit integer, so the result for them
+  // is unspecified, and they are handled the same way. Shifting the sign out of
+  // the bits of X leaves 0 for zeros and at least 0xff000000 for infinities and
+  // NaNs. Subtracting 1 makes these all at least 0xfeffffff, with 0 wrapping
+  // around. This avoids ||, which is not supported on vectors. It also avoids
+  // masking the sign, which is turned into fabs, and that can flush denormals.
+  auto IsSpecial = ((Bits << 1u) - 1u) >= 0xfeffffffu;
+  // A denormal has no implicit leading one bit. Its fraction is an integer
+  // below 2^23, which converts to a float exactly. The exponent of that float
+  // is the position of the highest set bit and its fraction is the normalized
+  // fraction. This only uses integer operations on the bits of X, so it does
+  // not depend on how denormal floats are handled.
+  auto IsDenormal = ExpBits == 0u;
+  auto Normalized = bit_cast<uint>(static_cast<T>(Fraction));
+  // The exponent field X would have if it could be normalized, which is below
+  // 1 for a denormal.
+  auto ExpField =
+      select(IsDenormal, (Normalized >> 23u) - 149u, ExpBits >> 23u);
+  auto MantFraction = select(IsDenormal, Normalized & 0x007fffffu, Fraction);
+  // Keep the sign and the fraction, and use the exponent field for 0.5, which
+  // scales X into [0.5, 1).
+  auto MantBits = (Bits & 0x80000000u) | 0x3f000000u | MantFraction;
   T Mant = select(IsSpecial, X, bit_cast<float>(MantBits));
-  auto RawExp = (ExpBits >> 23u) - 126u;
+  auto RawExp = ExpField - 126u;
   Exp = select(IsSpecial, static_cast<T>(0),
                static_cast<T>(bit_cast<int>(RawExp)));
   return Mant;
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index f331be96ac8c92..c1ed0d86817fcc 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -231,8 +231,10 @@ firstbithigh(vector<T, N> X) {
 /// \param Exp The returned exponent of X.
 ///
 /// The mantissa has the same sign as X and an absolute value in [0.5, 1), such
-/// that X = mantissa * 2^Exp. If X is 0, both the mantissa and the exponent
-/// are 0.
+/// that X = mantissa * 2^Exp, including when X is a denormal. If X is 0, both
+/// the mantissa and the exponent are 0. The result is unspecified if the
+/// exponent would be outside the range of a 32-bit integer, which is the case
+/// for infinities and NaNs.
 inline float frexp(float X, out float Exp) {
   return __detail::frexp_impl(X, Exp);
 }
diff --git a/clang/test/CodeGenHLSL/builtins/frexp.hlsl b/clang/test/CodeGenHLSL/builtins/frexp.hlsl
index d627b53abb6071..066800f02a47fe 100644
--- a/clang/test/CodeGenHLSL/builtins/frexp.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/frexp.hlsl
@@ -5,6 +5,11 @@
 // RUN:   spirv-unknown-vulkan-library %s -O1 -emit-llvm -o - | FileCheck %s \
 // RUN:   --check-prefixes=CHECK,SPVCHECK
 
+// The code works on the bits of the float, so it does not depend on how
+// denormals are handled. A denormal is normalized by converting its fraction
+// to a float, which is why there is a uitofp. The sign is kept by or-ing in the
+// sign bit, and the exponent field of 0.5 is or-ed in, which is 0x3f000000.
+
 // The only difference between the targets is the calling convention on the
 // definitions. It is captured once, in the first function, as [[FN_TYPE]].
 
@@ -12,10 +17,12 @@
 // SPVCHECK: define hidden [[FN_TYPE:spir_func ]]noundef nofpclass(nan inf) float @{{.*}}test_frexp_f{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast float %X to i32
 // CHECK:      [[EXPBITS:%.*]] = and i32 [[BITS]], 2139095040
-// CHECK:      and i32 [[BITS]], -2139095041
+// CHECK:      [[FRACTION:%.*]] = and i32 [[BITS]], 8388607
+// CHECK:      shl i32 [[BITS]], 1
+// CHECK:      uitofp {{.*}}i32 [[FRACTION]] to float
+// CHECK:      and i32 [[BITS]], -2147483648
 // CHECK:      or {{.*}}i32 {{.*}}, 1056964608
 // CHECK:      [[MANT:%.*]] = select {{.*}}, float %X, float {{.*}}
-// CHECK:      lshr {{.*}}i32 [[EXPBITS]], 23
 // CHECK:      sitofp {{.*}}i32 {{.*}} to float
 // CHECK:      [[EXP:%.*]] = select {{.*}}, float 0.000000e+00, float {{.*}}
 // CHECK-NEXT: store float [[EXP]], ptr %E
@@ -25,10 +32,12 @@ float test_frexp_f(float X, out float E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <2 x float> @{{.*}}test_frexp_f2{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <2 x float> %X to <2 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <2 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <2 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <2 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <2 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<2 x i32> [[FRACTION]] to <2 x float>
+// CHECK:      and <2 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<2 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <2 x float> %X, <2 x float> {{.*}}
-// CHECK:      lshr {{.*}}<2 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<2 x i32> {{.*}} to <2 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <2 x float> zeroinitializer, <2 x float> {{.*}}
 // CHECK-NEXT: store <2 x float> [[EXP]], ptr %E
@@ -38,10 +47,12 @@ float2 test_frexp_f2(float2 X, out float2 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <3 x float> @{{.*}}test_frexp_f3{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <3 x float> %X to <3 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <3 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <3 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <3 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <3 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<3 x i32> [[FRACTION]] to <3 x float>
+// CHECK:      and <3 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<3 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <3 x float> %X, <3 x float> {{.*}}
-// CHECK:      lshr {{.*}}<3 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<3 x i32> {{.*}} to <3 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <3 x float> zeroinitializer, <3 x float> {{.*}}
 // CHECK-NEXT: store <3 x float> [[EXP]], ptr %E
@@ -51,10 +62,12 @@ float3 test_frexp_f3(float3 X, out float3 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <4 x float> @{{.*}}test_frexp_f4{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <4 x float> %X to <4 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <4 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <4 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <4 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<4 x i32> [[FRACTION]] to <4 x float>
+// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<4 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <4 x float> %X, <4 x float> {{.*}}
-// CHECK:      lshr {{.*}}<4 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<4 x i32> {{.*}} to <4 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <4 x float> zeroinitializer, <4 x float> {{.*}}
 // CHECK-NEXT: store <4 x float> [[EXP]], ptr %E
@@ -64,10 +77,12 @@ float4 test_frexp_f4(float4 X, out float4 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <5 x float> @{{.*}}test_frexp_f5{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <5 x float> %X to <5 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <5 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <5 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <5 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <5 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<5 x i32> [[FRACTION]] to <5 x float>
+// CHECK:      and <5 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<5 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <5 x float> %X, <5 x float> {{.*}}
-// CHECK:      lshr {{.*}}<5 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<5 x i32> {{.*}} to <5 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <5 x float> zeroinitializer, <5 x float> {{.*}}
 // CHECK-NEXT: store <5 x float> [[EXP]], ptr %E
diff --git a/clang/test/CodeGenHLSL/builtins/frexp_mat.hlsl b/clang/test/CodeGenHLSL/builtins/frexp_mat.hlsl
index 8e71aa3d811940..bda11cf3da8163 100644
--- a/clang/test/CodeGenHLSL/builtins/frexp_mat.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/frexp_mat.hlsl
@@ -15,10 +15,12 @@
 // SPVCHECK: define hidden [[FN_TYPE:spir_func ]]noundef nofpclass(nan inf) <2 x float> @{{.*}}test_frexp_f1x2{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <2 x float> %X to <2 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <2 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <2 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <2 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <2 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<2 x i32> [[FRACTION]] to <2 x float>
+// CHECK:      and <2 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<2 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <2 x float> %X, <2 x float> {{.*}}
-// CHECK:      lshr {{.*}}<2 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<2 x i32> {{.*}} to <2 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <2 x float> zeroinitializer, <2 x float> {{.*}}
 // CHECK-NEXT: store <2 x float> [[EXP]], ptr %E
@@ -28,10 +30,12 @@ float1x2 test_frexp_f1x2(float1x2 X, out float1x2 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <3 x float> @{{.*}}test_frexp_f1x3{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <3 x float> %X to <3 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <3 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <3 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <3 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <3 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<3 x i32> [[FRACTION]] to <3 x float>
+// CHECK:      and <3 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<3 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <3 x float> %X, <3 x float> {{.*}}
-// CHECK:      lshr {{.*}}<3 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<3 x i32> {{.*}} to <3 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <3 x float> zeroinitializer, <3 x float> {{.*}}
 // CHECK-NEXT: store <3 x float> [[EXP]], ptr %E
@@ -41,10 +45,12 @@ float1x3 test_frexp_f1x3(float1x3 X, out float1x3 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <4 x float> @{{.*}}test_frexp_f1x4{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <4 x float> %X to <4 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <4 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <4 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <4 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<4 x i32> [[FRACTION]] to <4 x float>
+// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<4 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <4 x float> %X, <4 x float> {{.*}}
-// CHECK:      lshr {{.*}}<4 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<4 x i32> {{.*}} to <4 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <4 x float> zeroinitializer, <4 x float> {{.*}}
 // CHECK-NEXT: store <4 x float> [[EXP]], ptr %E
@@ -54,10 +60,12 @@ float1x4 test_frexp_f1x4(float1x4 X, out float1x4 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <2 x float> @{{.*}}test_frexp_f2x1{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <2 x float> %X to <2 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <2 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <2 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <2 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <2 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<2 x i32> [[FRACTION]] to <2 x float>
+// CHECK:      and <2 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<2 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <2 x float> %X, <2 x float> {{.*}}
-// CHECK:      lshr {{.*}}<2 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<2 x i32> {{.*}} to <2 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <2 x float> zeroinitializer, <2 x float> {{.*}}
 // CHECK-NEXT: store <2 x float> [[EXP]], ptr %E
@@ -67,10 +75,12 @@ float2x1 test_frexp_f2x1(float2x1 X, out float2x1 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <4 x float> @{{.*}}test_frexp_f2x2{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <4 x float> %X to <4 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <4 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <4 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <4 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<4 x i32> [[FRACTION]] to <4 x float>
+// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<4 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <4 x float> %X, <4 x float> {{.*}}
-// CHECK:      lshr {{.*}}<4 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<4 x i32> {{.*}} to <4 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <4 x float> zeroinitializer, <4 x float> {{.*}}
 // CHECK-NEXT: store <4 x float> [[EXP]], ptr %E
@@ -80,10 +90,12 @@ float2x2 test_frexp_f2x2(float2x2 X, out float2x2 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <6 x float> @{{.*}}test_frexp_f2x3{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <6 x float> %X to <6 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <6 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <6 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <6 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <6 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<6 x i32> [[FRACTION]] to <6 x float>
+// CHECK:      and <6 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<6 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <6 x float> %X, <6 x float> {{.*}}
-// CHECK:      lshr {{.*}}<6 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<6 x i32> {{.*}} to <6 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <6 x float> zeroinitializer, <6 x float> {{.*}}
 // CHECK-NEXT: store <6 x float> [[EXP]], ptr %E
@@ -93,10 +105,12 @@ float2x3 test_frexp_f2x3(float2x3 X, out float2x3 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <8 x float> @{{.*}}test_frexp_f2x4{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <8 x float> %X to <8 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <8 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <8 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <8 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <8 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<8 x i32> [[FRACTION]] to <8 x float>
+// CHECK:      and <8 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<8 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <8 x float> %X, <8 x float> {{.*}}
-// CHECK:      lshr {{.*}}<8 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<8 x i32> {{.*}} to <8 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <8 x float> zeroinitializer, <8 x float> {{.*}}
 // CHECK-NEXT: store <8 x float> [[EXP]], ptr %E
@@ -106,10 +120,12 @@ float2x4 test_frexp_f2x4(float2x4 X, out float2x4 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <3 x float> @{{.*}}test_frexp_f3x1{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <3 x float> %X to <3 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <3 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <3 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <3 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <3 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<3 x i32> [[FRACTION]] to <3 x float>
+// CHECK:      and <3 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<3 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <3 x float> %X, <3 x float> {{.*}}
-// CHECK:      lshr {{.*}}<3 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<3 x i32> {{.*}} to <3 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <3 x float> zeroinitializer, <3 x float> {{.*}}
 // CHECK-NEXT: store <3 x float> [[EXP]], ptr %E
@@ -119,10 +135,12 @@ float3x1 test_frexp_f3x1(float3x1 X, out float3x1 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <6 x float> @{{.*}}test_frexp_f3x2{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <6 x float> %X to <6 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <6 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <6 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <6 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <6 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<6 x i32> [[FRACTION]] to <6 x float>
+// CHECK:      and <6 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<6 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <6 x float> %X, <6 x float> {{.*}}
-// CHECK:      lshr {{.*}}<6 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<6 x i32> {{.*}} to <6 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <6 x float> zeroinitializer, <6 x float> {{.*}}
 // CHECK-NEXT: store <6 x float> [[EXP]], ptr %E
@@ -132,10 +150,12 @@ float3x2 test_frexp_f3x2(float3x2 X, out float3x2 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <9 x float> @{{.*}}test_frexp_f3x3{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <9 x float> %X to <9 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <9 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <9 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <9 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <9 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<9 x i32> [[FRACTION]] to <9 x float>
+// CHECK:      and <9 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<9 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <9 x float> %X, <9 x float> {{.*}}
-// CHECK:      lshr {{.*}}<9 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<9 x i32> {{.*}} to <9 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <9 x float> zeroinitializer, <9 x float> {{.*}}
 // CHECK-NEXT: store <9 x float> [[EXP]], ptr %E
@@ -145,10 +165,12 @@ float3x3 test_frexp_f3x3(float3x3 X, out float3x3 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <12 x float> @{{.*}}test_frexp_f3x4{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <12 x float> %X to <12 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <12 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <12 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <12 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <12 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<12 x i32> [[FRACTION]] to <12 x float>
+// CHECK:      and <12 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<12 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <12 x float> %X, <12 x float> {{.*}}
-// CHECK:      lshr {{.*}}<12 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<12 x i32> {{.*}} to <12 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <12 x float> zeroinitializer, <12 x float> {{.*}}
 // CHECK-NEXT: store <12 x float> [[EXP]], ptr %E
@@ -158,10 +180,12 @@ float3x4 test_frexp_f3x4(float3x4 X, out float3x4 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <4 x float> @{{.*}}test_frexp_f4x1{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <4 x float> %X to <4 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <4 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <4 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <4 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<4 x i32> [[FRACTION]] to <4 x float>
+// CHECK:      and <4 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<4 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <4 x float> %X, <4 x float> {{.*}}
-// CHECK:      lshr {{.*}}<4 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<4 x i32> {{.*}} to <4 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <4 x float> zeroinitializer, <4 x float> {{.*}}
 // CHECK-NEXT: store <4 x float> [[EXP]], ptr %E
@@ -171,10 +195,12 @@ float4x1 test_frexp_f4x1(float4x1 X, out float4x1 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <8 x float> @{{.*}}test_frexp_f4x2{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <8 x float> %X to <8 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <8 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <8 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <8 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <8 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<8 x i32> [[FRACTION]] to <8 x float>
+// CHECK:      and <8 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<8 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <8 x float> %X, <8 x float> {{.*}}
-// CHECK:      lshr {{.*}}<8 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<8 x i32> {{.*}} to <8 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <8 x float> zeroinitializer, <8 x float> {{.*}}
 // CHECK-NEXT: store <8 x float> [[EXP]], ptr %E
@@ -184,10 +210,12 @@ float4x2 test_frexp_f4x2(float4x2 X, out float4x2 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <12 x float> @{{.*}}test_frexp_f4x3{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <12 x float> %X to <12 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <12 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <12 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <12 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <12 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<12 x i32> [[FRACTION]] to <12 x float>
+// CHECK:      and <12 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<12 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <12 x float> %X, <12 x float> {{.*}}
-// CHECK:      lshr {{.*}}<12 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<12 x i32> {{.*}} to <12 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <12 x float> zeroinitializer, <12 x float> {{.*}}
 // CHECK-NEXT: store <12 x float> [[EXP]], ptr %E
@@ -197,10 +225,12 @@ float4x3 test_frexp_f4x3(float4x3 X, out float4x3 E) { return frexp(X, E); }
 // CHECK: define hidden [[FN_TYPE]]noundef nofpclass(nan inf) <16 x float> @{{.*}}test_frexp_f4x4{{.*}}(
 // CHECK:      [[BITS:%.*]] = bitcast <16 x float> %X to <16 x i32>
 // CHECK:      [[EXPBITS:%.*]] = and <16 x i32> [[BITS]], splat (i32 2139095040)
-// CHECK:      and <16 x i32> [[BITS]], splat (i32 -2139095041)
+// CHECK:      [[FRACTION:%.*]] = and <16 x i32> [[BITS]], splat (i32 8388607)
+// CHECK:      shl <16 x i32> [[BITS]], splat (i32 1)
+// CHECK:      uitofp {{.*}}<16 x i32> [[FRACTION]] to <16 x float>
+// CHECK:      and <16 x i32> [[BITS]], splat (i32 -2147483648)
 // CHECK:      or {{.*}}<16 x i32> {{.*}}, splat (i32 1056964608)
 // CHECK:      [[MANT:%.*]] = select {{.*}}, <16 x float> %X, <16 x float> {{.*}}
-// CHECK:      lshr {{.*}}<16 x i32> [[EXPBITS]], splat (i32 23)
 // CHECK:      sitofp {{.*}}<16 x i32> {{.*}} to <16 x float>
 // CHECK:      [[EXP:%.*]] = select {{.*}}, <16 x float> zeroinitializer, <16 x float> {{.*}}
 // CHECK-NEXT: store <16 x float> [[EXP]], ptr %E



More information about the cfe-commits mailing list