Skip to content

Commit 265ec51

Browse files
igorban-inteligcbot
authored andcommitted
.
1 parent adb0b17 commit 265ec51

File tree

7 files changed

+403
-37
lines changed

7 files changed

+403
-37
lines changed

IGC/VectorCompiler/lib/BiF/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ set(BUILTIN_SOURCES
4444

4545
Library/Math/F64/fdiv.cpp
4646
Library/Math/F64/fptoi.cpp
47+
Library/Math/F64/frem.cpp
4748
Library/Math/F64/fsqrt.cpp
4849
Library/Math/F64/itofp.cpp
4950

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2024 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
#ifndef F64CONSTS_H
10+
#define F64CONSTS_H
11+
12+
// We have to use 32-bit integers when it's possible
13+
constexpr unsigned exp_shift = 52 - 32;
14+
constexpr unsigned exp_mask = 0x7ff;
15+
constexpr unsigned exp_bias = 0x3ff;
16+
constexpr unsigned exp_32bitmask = exp_mask << exp_shift;
17+
constexpr unsigned exp_invmask = ~(exp_32bitmask);
18+
constexpr unsigned exp_32threshold_shift = 0x432;
19+
constexpr unsigned mantissa_32loss = -2;
20+
constexpr unsigned nan_hi = 0x7ff80000;
21+
constexpr unsigned inf_hi = 0x7ff00000;
22+
constexpr unsigned sign_32bit = 1 << (63 - 32);
23+
24+
// Double consts
25+
constexpr double twoPow1023 = 0x1p+1023;
26+
constexpr double twoPow1022 = 0x1p+1022;
27+
constexpr double twoPow64 = 0x1p+64;
28+
constexpr double twoPowm64 = 0x1p-64;
29+
constexpr double roundInt = 0x1.8p+52;
30+
31+
#endif

IGC/VectorCompiler/lib/BiF/Library/Math/F64/fdiv.cpp

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,13 @@ SPDX-License-Identifier: MIT
66
77
============================= end_copyright_notice ===========================*/
88

9+
#include "f64consts.h"
910
#include <cm-cl/math.h>
1011
#include <cm-cl/vector.h>
1112

1213
using namespace cm;
1314

1415
namespace {
15-
// We have to use 32-bit integers when it's possible
16-
constexpr unsigned exp_shift = 52 - 32;
17-
constexpr unsigned exp_mask = 0x7ff;
18-
constexpr unsigned exp_bias = 0x3ff;
19-
constexpr unsigned exp_invmask = ~(exp_mask << exp_shift);
20-
21-
constexpr unsigned nan_hi = 0x7ff80000;
22-
constexpr unsigned inf_hi = 0x7ff00000;
23-
2416
template <bool NNaN, bool NInf, bool NSZ, int N>
2517
CM_NODEBUG CM_INLINE vector<double, N>
2618
__impl_fdiv_special(vector<double, N> a, vector<double, N> b) {
@@ -42,8 +34,8 @@ __impl_fdiv_special(vector<double, N> a, vector<double, N> b) {
4234
auto result_lo = result.template select<N, 2>(0);
4335
auto result_hi = result.template select<N, 2>(1);
4436

45-
auto ex_x_max = x_exp == 0x7ff;
46-
auto ex_y_max = y_exp == 0x7ff;
37+
auto ex_x_max = x_exp == exp_mask;
38+
auto ex_y_max = y_exp == exp_mask;
4739

4840
if constexpr (!NInf) // Inf / y == Inf
4941
result_hi.merge(x_sgn ^ y_sgn | inf_hi, ex_x_max);
@@ -112,28 +104,25 @@ CM_NODEBUG CM_INLINE vector<double, N> __impl_fdiv_fast(vector<double, N> a,
112104

113105
// Long path, scale is needed
114106
if (long_path.any()) {
115-
constexpr double two64 = 0x1p+64;
116-
constexpr double twom64 = 0x1p-64;
117-
118107
// Handle subnormal a
119108
mask<N> x_unorm = x_exp == 0;
120109
if (x_unorm.any()) {
121-
a.merge(a * two64, x_unorm);
110+
a.merge(a * twoPow64, x_unorm);
122111
x_exp = (vector<uint32_t, N>(x_hi) >> exp_shift) & exp_mask;
123112
// if exp is still 0, we have zero or FTZ enabled
124-
scale0.merge(scale0 * twom64, x_unorm & (x_exp != 0));
113+
scale0.merge(scale0 * twoPowm64, x_unorm & (x_exp != 0));
125114
}
126115

127116
// Handle subnormal b
128117
mask<N> y_unorm = y_exp == 0;
129118
if (y_unorm.any()) {
130-
b.merge(b * two64, y_unorm);
119+
b.merge(b * twoPow64, y_unorm);
131120
y_exp = (vector<uint32_t, N>(y_hi) >> exp_shift) & exp_mask;
132121
// if exp is still 0, we have zero or FTZ enabled
133-
scale0.merge(scale0 * two64, y_unorm & (y_exp != 0));
122+
scale0.merge(scale0 * twoPow64, y_unorm & (y_exp != 0));
134123
}
135124

136-
auto exp_diff = x_exp - y_exp + 0x7ff;
125+
auto exp_diff = x_exp - y_exp + exp_mask;
137126

138127
auto scale1_hi =
139128
scale1.template format<uint32_t>().template select<N, 2>(1);
@@ -286,28 +275,25 @@ CM_NODEBUG CM_INLINE vector<double, N> __impl_fdiv_ieee(vector<double, N> a,
286275

287276
// Long path, scale is needed
288277
if (long_path.any()) {
289-
constexpr double two64 = 0x1p+64;
290-
constexpr double twom64 = 0x1p-64;
291-
292278
// Handle subnormal a
293279
mask<N> x_unorm = x_exp == 0;
294280
if (x_unorm.any()) {
295-
a.merge(a * two64, x_unorm);
281+
a.merge(a * twoPow64, x_unorm);
296282
x_exp = (vector<uint32_t, N>(x_hi) >> exp_shift) & exp_mask;
297283
// if exp is still 0, we have zero or FTZ enabled
298-
scale0.merge(scale0 * twom64, x_unorm & (x_exp != 0));
284+
scale0.merge(scale0 * twoPowm64, x_unorm & (x_exp != 0));
299285
}
300286

301287
// Handle subnormal b
302288
mask<N> y_unorm = y_exp == 0;
303289
if (y_unorm.any()) {
304-
b.merge(b * two64, y_unorm);
290+
b.merge(b * twoPow64, y_unorm);
305291
y_exp = (vector<uint32_t, N>(y_hi) >> exp_shift) & exp_mask;
306292
// if exp is still 0, we have zero or FTZ enabled
307-
scale0.merge(scale0 * two64, y_unorm & (y_exp != 0));
293+
scale0.merge(scale0 * twoPow64, y_unorm & (y_exp != 0));
308294
}
309295

310-
auto exp_diff = x_exp - y_exp + 0x7ff;
296+
auto exp_diff = x_exp - y_exp + exp_mask;
311297

312298
auto scale1_hi =
313299
scale1.template format<uint32_t>().template select<N, 2>(1);
@@ -339,8 +325,8 @@ CM_NODEBUG CM_INLINE vector<double, N> __impl_fdiv_ieee(vector<double, N> a,
339325
b.merge(mb.template format<double>(), long_path);
340326

341327
// g_ediff value is needed to detect gradual underflow
342-
vector<double, N> abs_a = detail::__cm_cl_abs_float(a.cl_vector());
343-
vector<double, N> abs_b = detail::__cm_cl_abs_float(b.cl_vector());
328+
vector<double, N> abs_a = math::absolute(a.cl_vector());
329+
vector<double, N> abs_b = math::absolute(b.cl_vector());
344330

345331
vector<int64_t, N> i_abs_a = abs_a.template format<int64_t>();
346332
vector<int64_t, N> i_abs_b = abs_b.template format<int64_t>();
@@ -472,4 +458,4 @@ CM_NODEBUG CM_NOINLINE extern "C" double __vc_builtin_fdiv_fast_f64(double a,
472458
FDIV(1)
473459
FDIV(2)
474460
FDIV(4)
475-
FDIV(8)
461+
FDIV(8)

0 commit comments

Comments
 (0)