Skip to content

Commit d6ff4c7

Browse files
committed
Workaround WSL misconfiguring the x87 FPU
1 parent f44d887 commit d6ff4c7

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

libc/runtime/cosmo.S

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@ cosmo: push %rbp
4444
mov %eax,%r12d
4545
#endif /* SYSDEBUG */
4646

47+
// Windows always initializes FPU to douuble precision.
48+
// WSL breaks Linux ABI by initializing FPU to double precision.
49+
// This code makes long double long again.
50+
//
51+
// @see System V Application Binary Interface NexGen32e Architecture
52+
// Processor Supplement, Version 1.0, December 5th, 2018
53+
// Section 3.4.1: Initial Stack and Register State
54+
fldcw 1f(%rip)
55+
.rodata
56+
.align 2
57+
// 8087 FPU Control Word
58+
// IM: Invalid Operation ───────────────┐
59+
// DM: Denormal Operand ───────────────┐│
60+
// ZM: Zero Divide ───────────────────┐││
61+
// OM: Overflow ─────────────────────┐│││
62+
// UM: Underflow ───────────────────┐││││
63+
// PM: Precision ──────────────────┐│││││
64+
// PC: Precision Control ───────┐ ││││││
65+
// {float,∅,double,long double}│ ││││││
66+
// RC: Rounding Control ──────┐ │ ││││││
67+
// {even, →-∞, →+∞, →0} │┌┤ ││││││
68+
// ┌┤││ ││││││
69+
// d││││rr││││││
70+
1: .short 0b00000000000000000001101111111
71+
.previous
72+
4773
#ifdef __FAST_MATH__
4874
push %rax
4975
stmxcsr (%rsp)

libc/runtime/winmain.greg.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,6 @@ static const short kConsoleModes[3] = {
7676
kNtEnableVirtualTerminalProcessing,
7777
};
7878

79-
forceinline void MakeLongDoubleLongAgain(void) {
80-
/* 8087 FPU Control Word
81-
IM: Invalid Operation ───────────────┐
82-
DM: Denormal Operand ───────────────┐│
83-
ZM: Zero Divide ───────────────────┐││
84-
OM: Overflow ─────────────────────┐│││
85-
UM: Underflow ───────────────────┐││││
86-
PM: Precision ──────────────────┐│││││
87-
PC: Precision Control ────────┐ ││││││
88-
{float,∅,double,long double} │ ││││││
89-
RC: Rounding Control ───────┐ │ ││││││
90-
{even, →-∞, →+∞, →0} │┌┤ ││││││
91-
┌┤││ ││││││
92-
d││││rr││││││*/
93-
int x87cw = 0b0000000000000000001101111111;
94-
asm volatile("fldcw\t%0" : /* no outputs */ : "m"(x87cw));
95-
}
96-
9779
// https://nullprogram.com/blog/2022/02/18/
9880
static inline char16_t *MyCommandLine(void) {
9981
void *cmd;
@@ -259,7 +241,6 @@ __msabi textwindows int64_t WinMain(int64_t hInstance, int64_t hPrevInstance,
259241
if (__strstr16(cmdline, u"--strace")) ++__strace;
260242
#endif
261243
NTTRACE("WinMain()");
262-
MakeLongDoubleLongAgain();
263244
if (_weaken(WinSockInit)) _weaken(WinSockInit)();
264245
if (_weaken(WinMainForked)) _weaken(WinMainForked)();
265246
WinMainNew(cmdline);

0 commit comments

Comments
 (0)