[Issue 13548] wrong sqrt

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Aug 31 09:16:49 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=13548

--- Comment #3 from Walter Bright <bugzilla at digitalmars.com> ---
Code:

  import core.stdc.math;
  void main() {
    double two = 2.0;
    assert(sqrt(two) == sqrt(2.0));
  }

Generates for main():

        enter   014h,0
        fld     qword ptr FLAT:CONST[00h]
        fstp    qword ptr -8[EBP]
        push    dword ptr -4[EBP]
        push    dword ptr -8[EBP]
        call    near ptr _sqrt
        add     ESP,8
        push    dword ptr FLAT:CONST[04h]
        push    dword ptr FLAT:CONST[00h]
        fstp    qword ptr -014h[EBP]
        call    near ptr _sqrt
        add     ESP,8
        fld     qword ptr -014h[EBP]
        fxch    ST(1)
        fucompp ST(1),ST
        fstsw   AX
        sahf
        jp      L40
        je      L4D
L40:    push    6
        mov     EAX,offset FLAT:___a7_74657374322e64
        push    EAX
        call    near ptr __d_assertp
L4D:    xor     EAX,EAX
        leave
        ret

Both calls to sqrt() produce the same result in ST0. But the first one stores
and reloads it as a double, thus causing a round-to-double operation. The
second does not, so they compare unequal. yebblies is right.

I'm not sure what the right fix would be. I could change sqrt() in DMC's
runtime library to store/load the value it returns, but that wouldn't fix
things for FreeBSD32.

On Win64 there isn't a problem because the sqrt() result is returned in XMM0
rather than ST0.

--


More information about the Digitalmars-d-bugs mailing list