News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

ftol2

Started by guga, May 05, 2012, 04:48:33 AM

Previous topic - Next topic

guga

Ftol2 similar to the one existant on msvcr100.dll.
It allows outouting on Buffers the integer and remainder parts.
Feel free to review/improve the code.

Usage example:

[teste: R$ -1.012341649e+5]
[FloattolResult: Q$ 0]
[RemainderResult: Q$ 0]

fld R$teste
call ftol2 FloattolResult, RemainderResult




Proc ftol2:
    Arguments @pOutputInteger, @pOutPutRemainder
    Local @Remainder
    Structure @StoredNumber 8, @NumHiDis 0,  @NumLowDis 4
    Uses ecx, esi, edi, edx
       
    mov edi D@pOutputInteger
    mov esi D@StoredNumber

    fld ST0
    fst F@Remainder
    fistp R$esi
    fild R$esi

    mov edx D@Remainder
    mov eax D@NumHiDis
    test eax eax | je @integer_QnaN_or_zero

@arg_is_not_integer_QnaN:

    fsubp ST1 ST0
    test edx edx | jns @positive
    fstp F@Remainder
    mov ecx D@Remainder
    xor ecx 080000000
    add ecx 07FFFFFFF
    adc eax 00
    mov edx D@NumLowDis
    adc edx 00
    jmp @localexit

@positive:

    fstp F@Remainder
    mov ecx D@Remainder
    add ecx 07FFFFFFF
    sbb eax 00
    mov edx D@NumLowDis
    sbb edx 00
    jmp @localexit

@integer_QnaN_or_zero:   

    mov edx D@NumLowDis
    test edx 07FFFFFFF | jne @arg_is_not_integer_QnaN
    fstp F@Remainder
    fstp F@Remainder

@localexit:

    ; Output the integer part here
    mov D$edi eax
    mov D$edi+4 edx
   
    ; Output the remainder part here
    mov edi D@pOutPutRemainder
    fld F@Remainder | fabs
    fstp R$edi

EndP