Re-entrant FPU code

Hi all,

If you have the TP6/BP7 RTL, four of the routines in F87H FTrunc,
FRound ,FInt & FFrac can be made re-entrant by replacing the Borland
code with the following code, which requires at least a 386 (but can
be recoded for older CPUs)

; Trunc function

FTrunc  PROC    FAR
        push    eax               ; make room for FPU control word and
        push    cx                ;  temporary longint
        mov     bx, sp

        fstcw   ss:[bx + 4]       ; save FPU control word on stack
        fldcw   CWChop            ; set rounding mode to CHOP
        fistp   dword ptr ss:[bx] ; store ST(0) to longint variable
        fldcw   ss:[bx + 4]       ; restore FPU control word from
stack

        pop     ax                ; pop temporary longint variable
into
        pop     edx               ;  DX:AX (and remove control word)
        ret
FTrunc  ENDP

; Round function

FRound  PROC    FAR
        push    eax               ; make room for temporary longint
        mov     bx, sp

        fistp   dword ptr ss:[bx] ; store ST(0) to longint variable
        fwait                     ; is this required on a 386(+) ?

        pop     ax
        pop     dx
        ret
FRound  ENDP

; Int function

FInt    PROC    FAR
        push    ax                ; make room for control word
        mov     bx, sp
        fstcw   ss:[bx]           ; save FPU control word on stack
        fldcw   CWChop
        frndint
        fldcw   ss:[bx]           ; restore FPU control word from
stack
        add     sp, 2             ; clean up stack
        ret
FInt    ENDP

; Frac function

FFrac   PROC    FAR
        push    ax                ; make room for control word
        mov     bx, sp
        fstcw   ss:[bx]           ; save FPU control word on stack
        fldcw   CWChop
        fld     st(0)
        frndint
        fsub
        fldcw   ss:[bx]           ; restore FPU control word from
stack
        add     sp, 2             ; clean up stack
        ret
FFrac   ENDP

Obviously this code is slower than the original Borland code, but not
a great deal.

Regards,

Robert
--
Robert AH Prins
pr...@bigfoot.com
adira(at)takas(dot)lt (Only until 2002-01-04!)