News:

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

FPU truncate

Started by Jimg, November 16, 2007, 04:59:38 PM

Previous topic - Next topic

Jimg

What is the proper way to truncate a value in an FPU register to an integer.  Not round but truncate.


GregL

The example I posted here uses FISTTP if you have SSE3, CVTTSD2SI if you have SSE2, or the standard x87 way.

http://www.masm32.com/board/index.php?topic=7891.msg57858#msg57858


Jimg

Thanks Greg.  That wasn't that long ago, I don't know why I didn't remember your post.

In the meantime, since I already had the value in the FPU, and I knew it was positive, I found I could just subtract .5 and let the FPU round like normal to get the result I wanted.  A lot easier and more efficient, in my case, than anything else I could think of.

GregL

Jimq,

Just remember the normal rounding mode is "round to the nearest even integer".

Here's another method.


TruncateR8 PROC pR8:PTR REAL8, pInt32:PTR SDWORD
    .CONST
        dblMinusOneHalf REAL8 -0.5
    .DATA
        i SDWORD 0
    .CODE
        mov  edx, pR8
        finit
        fld  REAL8 PTR [edx]
        fadd st(0), st(0)
        fabs
        fld  dblMinusOneHalf
        fadd
        fistp i
        sar i, 1
        mov eax, pR8
        mov edx, [eax+4]
        bt  edx, 31
        jnc positive
        neg i
      positive:
        mov edx, i
        mov eax, pInt32
        mov [eax], edx
        ret
TruncateR8 ENDP


I got the idea for this from here.


Jimg

QuoteJust remember the normal rounding mode is "round to the nearest even integer".
That's only if it's exactly something.5, right?  I'll have to see if that's could be a problem for me.  Thanks.

GregL

Jimq,

Yeah, only if it's something.5.