Skip to content

Instantly share code, notes, and snippets.

@martok
Created May 14, 2012 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martok/2691272 to your computer and use it in GitHub Desktop.
Save martok/2691272 to your computer and use it in GitHub Desktop.
TryTrunc
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