;This is PART of my QLIB Math LIBs.

; This version is for the Watcom compiler which expects returned values
; to be in general regs. (NOT on the FPU stack as BC expects)

; Math Library to be used in C programs (WATCOM)

;results are returned in CPU regs

.data?
  result REAL8 ?     ;a 80bit REAL number
  merror dw ?        ;=1 if last operation was out of range (failed)

.code

;these functions use double only (REAL8 = 64bit)

sin proc,a:REAL8
  fld a                    ;load value into FPU
  fsin                     ;do sin()
  fstsw ax                 ;get status word
  .if ax&4  ;mask C2       ;check C2 flag
    mov merror,1
  .else
    mov merror,0
  .endif
  fstp result              ;store result in 'result' variable
  wait                     ;wait till FPU is done
  mov eax,dptr[result]     ;load 'result' into registers as Watcom needs them
  mov edx,dptr[result+4]
  ret
sin endp

cos proc,a:REAL8
  fld a                    ;load value into FPU stack
  fcos                     ;do cos()
  fstsw ax
  .if ax&4  ;mask C2
    mov merror,1
  .else
    mov merror,0
  .endif
  fstp result
  wait
  mov eax,dptr[result]
  mov edx,dptr[result+4]
  ret
cos endp

tan proc,a:REAL8
  fld a                    ;load value into stack
  fsincos                  ;do sin() and cos() on value
  fstsw ax                 ;get status
  .if ax&4  ;mask C2
    mov merror,1           ;if there was an error then I simply
    fstp result  ;ignore   ;remove some unneed data on the FPU
  .else
    mov merror,0           ;if the sin and cos was OK then I do
    fdivp st(1),st         ;sin() / cos() which equals tan()
  .endif
  fstp result              ;store final result
  wait                     ;wait till done
  mov eax,dptr[result]     ;store as needed
  mov edx,dptr[result+4]
  ret
tan endp

ftol proc,a:REAL8         ;convert float to dword
  fld a                   ;load value into stack
  fistp dptr[result]      ;save into dword integer
  wait                    ;wait till done
  mov eax,dptr[result]    ;return as a dword integer (decimal part is striped)
  ret
ftol endp

