Skip to content

Commit 949b0d5

Browse files
authored
Merge pull request #6271 from M-Colley/fix/x87-nrm2-precision
x86/x86_64: force 64-bit x87 precision in nrm2 and znrm2 kernels
2 parents f80d6e8 + 69b5a23 commit 949b0d5

5 files changed

Lines changed: 129 additions & 2 deletions

File tree

‎kernel/x86/nrm2.S‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include "common.h"
4141

4242
#define STACK 8
43-
#define ARGS 0
43+
#define ARGS 8
4444

4545
#define STACK_M 4 + STACK + ARGS(%esp)
4646
#define STACK_X 8 + STACK + ARGS(%esp)
@@ -56,6 +56,7 @@
5656

5757
PROLOGUE
5858

59+
subl $ARGS, %esp
5960
pushl %esi
6061
pushl %ebx
6162

@@ -74,6 +75,12 @@
7475
movl (INCX), INCX
7576
#endif
7677

78+
fnstcw 0 + STACK(%esp)
79+
movzwl 0 + STACK(%esp), %eax
80+
orl $0x0300, %eax
81+
movw %ax, 4 + STACK(%esp)
82+
fldcw 4 + STACK(%esp)
83+
7784
fldz
7885
testl M, M
7986
jle .L999
@@ -219,8 +226,10 @@
219226

220227
.L999:
221228
fsqrt
229+
fldcw 0 + STACK(%esp)
222230
popl %ebx
223231
popl %esi
232+
addl $ARGS, %esp
224233
ret
225234

226235
EPILOGUE

‎kernel/x86/znrm2.S‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include "common.h"
4141

4242
#define STACK 8
43-
#define ARGS 0
43+
#define ARGS 8
4444

4545
#define STACK_M 4 + STACK + ARGS(%esp)
4646
#define STACK_X 8 + STACK + ARGS(%esp)
@@ -56,6 +56,7 @@
5656

5757
PROLOGUE
5858

59+
subl $ARGS, %esp
5960
pushl %esi
6061
pushl %ebx
6162

@@ -74,6 +75,12 @@
7475
movl (INCX), INCX
7576
#endif
7677

78+
fnstcw 0 + STACK(%esp)
79+
movzwl 0 + STACK(%esp), %eax
80+
orl $0x0300, %eax
81+
movw %ax, 4 + STACK(%esp)
82+
fldcw 4 + STACK(%esp)
83+
7784
fldz
7885
testl M, M
7986
jle .L999
@@ -221,8 +228,10 @@
221228

222229
.L999:
223230
fsqrt
231+
fldcw 0 + STACK(%esp)
224232
popl %ebx
225233
popl %esi
234+
addl $ARGS, %esp
226235
ret
227236

228237
EPILOGUE

‎kernel/x86_64/nrm2.S‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@
5454
emms
5555
#endif
5656

57+
subq $8, %rsp
58+
fnstcw (%rsp)
59+
movzwl (%rsp), %eax
60+
orl $0x0300, %eax
61+
movw %ax, 4(%rsp)
62+
fldcw 4(%rsp)
63+
5764
fldz
5865
testq M, M
5966
jle .L999
@@ -204,6 +211,8 @@
204211
MOVSD (%rsp), %xmm0
205212
add $2 * SIZE, %rsp
206213
#endif
214+
fldcw (%rsp)
215+
addq $8, %rsp
207216
ret
208217

209218
EPILOGUE

‎kernel/x86_64/znrm2.S‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@
5454
emms
5555
#endif
5656

57+
subq $8, %rsp
58+
fnstcw (%rsp)
59+
movzwl (%rsp), %eax
60+
orl $0x0300, %eax
61+
movw %ax, 4(%rsp)
62+
fldcw 4(%rsp)
63+
5764
fldz
5865
testq M, M
5966
jle .L999
@@ -206,6 +213,8 @@
206213
MOVSD (%rsp), %xmm0
207214
add $2 * SIZE, %rsp
208215
#endif
216+
fldcw (%rsp)
217+
addq $8, %rsp
209218
ret
210219

211220
EPILOGUE

‎utest/test_dnrm2.c‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,95 @@ CTEST(dnrm2,dnrm2_neg_incx)
7777
res2 = sqrt(500.0);
7878
ASSERT_DBL_NEAR_TOL(res2, res1, DOUBLE_EPS);
7979
}
80+
81+
#if (defined(__x86_64__) || (defined(__i386__) && !defined(OPENBLAS_GENERIC))) && (defined(__GNUC__) || defined(__clang__))
82+
CTEST(dnrm2,dnrm2_x87_precision_control)
83+
{
84+
int i;
85+
blasint n = 4 + 4096;
86+
blasint n_pair = 2;
87+
blasint n_zero = 0;
88+
blasint incx = 1;
89+
blasint inc_zero = 0;
90+
double x[4 + 4096];
91+
double x_pair[2];
92+
unsigned short cw_orig = 0, cw_64 = 0, cw_53 = 0, cw_24 = 0, cw_after = 0;
93+
double res_64, res_53, res_24, res_pair;
94+
95+
for (i = 0; i < 4; i++) x[i] = 1.0;
96+
for (i = 4; i < n; i++) x[i] = ldexp(1.0, -27);
97+
x_pair[0] = 1.0;
98+
x_pair[1] = ldexp(1.0, -15);
99+
100+
__asm__ __volatile__("fnstcw %0" : "=m"(cw_orig));
101+
102+
cw_64 = (unsigned short)(cw_orig | 0x0300u);
103+
__asm__ __volatile__("fldcw %0" : : "m"(cw_64));
104+
res_64 = BLASFUNC(dnrm2)(&n, x, &incx);
105+
106+
/* 53-bit significand (Windows MSVC default 0x027f) must match 64-bit mode */
107+
cw_53 = (unsigned short)((cw_orig & ~0x0300u) | 0x0200u);
108+
__asm__ __volatile__("fldcw %0" : : "m"(cw_53));
109+
res_53 = BLASFUNC(dnrm2)(&n, x, &incx);
110+
__asm__ __volatile__("fnstcw %0" : "=m"(cw_after));
111+
__asm__ __volatile__("fldcw %0" : : "m"(cw_orig));
112+
ASSERT_EQUAL(cw_53, cw_after);
113+
ASSERT_DBL_NEAR_TOL(res_64, res_53, DOUBLE_EPS);
114+
115+
/* 24-bit significand (0x007f) and early-return paths */
116+
cw_24 = (unsigned short)(cw_orig & ~0x0300u);
117+
__asm__ __volatile__("fldcw %0" : : "m"(cw_24));
118+
res_24 = BLASFUNC(dnrm2)(&n, x, &incx);
119+
__asm__ __volatile__("fnstcw %0" : "=m"(cw_after));
120+
ASSERT_EQUAL(cw_24, cw_after);
121+
ASSERT_DBL_NEAR_TOL(res_64, res_24, DOUBLE_EPS);
122+
123+
res_pair = BLASFUNC(dnrm2)(&n_pair, x_pair, &incx);
124+
__asm__ __volatile__("fnstcw %0" : "=m"(cw_after));
125+
ASSERT_EQUAL(cw_24, cw_after);
126+
ASSERT_DBL_NEAR_TOL(sqrt(1.0 + ldexp(1.0, -30)), res_pair, DOUBLE_EPS);
127+
128+
res_24 = BLASFUNC(dnrm2)(&n_zero, x, &incx);
129+
__asm__ __volatile__("fnstcw %0" : "=m"(cw_after));
130+
ASSERT_EQUAL(cw_24, cw_after);
131+
ASSERT_DBL_NEAR_TOL(0.0, res_24, DOUBLE_EPS);
132+
133+
res_24 = BLASFUNC(dnrm2)(&n, x, &inc_zero);
134+
__asm__ __volatile__("fnstcw %0" : "=m"(cw_after));
135+
__asm__ __volatile__("fldcw %0" : : "m"(cw_orig));
136+
ASSERT_EQUAL(cw_24, cw_after);
137+
}
138+
#endif
139+
#endif
140+
141+
#if defined(BUILD_COMPLEX16)
142+
#if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__))
143+
CTEST(dnrm2,dznrm2_x87_precision_control)
144+
{
145+
int i;
146+
blasint n = 2 + 2048;
147+
blasint incx = 1;
148+
double x[(2 + 2048) * 2];
149+
unsigned short cw_orig = 0, cw_64 = 0, cw_53 = 0, cw_after = 0;
150+
double res_64, res_53;
151+
152+
for (i = 0; i < 4; i++) x[i] = 1.0;
153+
for (i = 4; i < n * 2; i++) x[i] = ldexp(1.0, -27);
154+
155+
__asm__ __volatile__("fnstcw %0" : "=m"(cw_orig));
156+
157+
cw_64 = (unsigned short)(cw_orig | 0x0300u);
158+
__asm__ __volatile__("fldcw %0" : : "m"(cw_64));
159+
res_64 = BLASFUNC(dznrm2)(&n, x, &incx);
160+
161+
cw_53 = (unsigned short)((cw_orig & ~0x0300u) | 0x0200u);
162+
__asm__ __volatile__("fldcw %0" : : "m"(cw_53));
163+
res_53 = BLASFUNC(dznrm2)(&n, x, &incx);
164+
__asm__ __volatile__("fnstcw %0" : "=m"(cw_after));
165+
__asm__ __volatile__("fldcw %0" : : "m"(cw_orig));
166+
167+
ASSERT_EQUAL(cw_53, cw_after);
168+
ASSERT_DBL_NEAR_TOL(res_64, res_53, DOUBLE_EPS);
169+
}
170+
#endif
80171
#endif

0 commit comments

Comments
 (0)