Skip to content

Commit 66580d4

Browse files
committed
make libc sqrtf signal EDOM/FE_INVALID
fidas
1 parent 21a2f5e commit 66580d4

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/crt/fpsqrt.src

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ __fpsqrt: ; CHECK: same(bitcast(float, pair8_24_t, { out.BC, out.A }), (float)sq
1818
sbc hl, bc
1919
jr z, .L.zero
2020
rlca
21-
jr c, .L.nan
21+
jr c, .L.return_nan
2222
inc e
2323
jr z, .L.nonfinite
2424
push hl
@@ -107,7 +107,9 @@ __fpsqrt: ; CHECK: same(bitcast(float, pair8_24_t, { out.BC, out.A }), (float)sq
107107
pop de
108108
ret
109109

110-
.L.nan:
110+
.L.return_nan:
111+
; input was negative and not -0.0
112+
; return negative NaN
111113
sbc a, a
112114
set 7, b
113115
pop hl

src/libc/sqrtf.src

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,26 @@ _sqrtf:
1919
pop bc
2020
ex (sp), hl
2121
ld a, l
22+
; assumes [-infinity, -FLT_TRUE_MIN] returns -NaN
2223
call __fpsqrt
2324
push bc
2425
ex (sp), hl
2526
push de
2627
ld e, a
28+
inc a ; test if the result was -NaN
29+
; +NaN returns here while -NaN falls through to raise EDOM/FE_INVALID.
30+
; Distinguishing them properly is not worth the extra cost.
31+
ret nz
32+
push hl
33+
ld hl, 4 ; EDOM
34+
ld (_errno), hl
35+
ld hl, ___fe_cur_env
36+
set 4, (hl) ; FE_INVALID
37+
pop hl
2738
ret
2839

2940
.extern __fpsqrt
41+
.extern _errno
42+
.extern ___fe_cur_env
3043

3144
.endif

0 commit comments

Comments
 (0)