Skip to content

Commit 42e4c0d

Browse files
committed
make LIBC sqrtf signal EDOM/FE_INVALID
1 parent 21a2f5e commit 42e4c0d

2 files changed

Lines changed: 16 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,25 @@ _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 does not, but this is not worth fixing.
30+
ret nz
31+
push hl
32+
ld hl, 4 ; EDOM
33+
ld (_errno), hl
34+
ld hl, ___fe_cur_env
35+
set 4, (hl) ; FE_INVALID
36+
pop hl
2737
ret
2838

2939
.extern __fpsqrt
40+
.extern __errno
41+
.extern ___fe_cur_env
3042

3143
.endif

0 commit comments

Comments
 (0)