Created
May 14, 2012 01:59
-
-
Save martok/2691272 to your computer and use it in GitHub Desktop.
TryTrunc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function TryTrunc(x: Extended; out Val: Int64): boolean; | |
var | |
oldcw, cw, sw: word; | |
asm | |
fld x | |
fwait | |
fnstcw oldcw | |
fnstcw cw | |
fwait | |
or cw, $0f00 // towards zero, full precision | |
fldcw cw | |
mov eax, dword ptr val | |
fistp qword ptr [eax] | |
fnstsw sw | |
mov ax, sw | |
not ax | |
and ax, 1 // IE = 1 | |
mov Result, al | |
test al,al | |
jnz @noexception | |
fnclex // if IE, eat exception | |
@noexception: | |
fwait // raise all others | |
FLDCW oldcw | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment