Main Archive Specials Wiki | FAQ Links Submit Forum


Fast rounding functions in pascal

Type : round/ceil/floor/trunc
References : Posted by bouba@hotmail.com

Notes :
Original documentation with cpp samples:
http://ldesoras.free.fr/prod.html#doc_rounding


Code :
Pascal translation of the functions (accurates ones) :

function ffloor(f:double):integer;
var
i:integer;
t : double;
begin
t := -0.5 ;
asm
fld f
fadd st,st(0)
fadd t
fistp i
sar i, 1
end;
result:= i
end;

function fceil(f:double):integer;
var
i:integer;
t : double;
begin
t := -0.5 ;
asm
fld f
fadd st,st(0)
fsubr t
fistp i
sar i, 1
end;
result:= -i
end;

function ftrunc(f:double):integer;
var
i:integer;
t : double;
begin
t := -0.5 ;
asm
fld f
fadd st,st(0)
fabs
fadd t
fistp i
sar i, 1
end;
if f<0 then i := -i;
result:= i
end;

function fround(f:double):integer;
var
i:integer;
t : double;
begin
t := 0.5 ;
asm
fld f
fadd st,st(0)
fadd t
fistp i
sar i, 1
end;
result:= i
end;



Comments


Added on : 23/04/08 by didid[ AT ]noskynetspam[ DOT ]be
Comment :
the fround doesn't make much sense in Pascal, as in Pascal (well, Delphi & I'm pretty sure FreePascal too), the default rounding is already a fast rounding. The default being FPU rounding to nearest mode, the compiler doesn't change it back & forth. & since it's inlined (well, compiler magic), it's very fast.



Add your own comment
Comments are displayed in fixed width, no HTML code allowed!
Email:

Comment:

Are you human?



Site created and maintained by Bram
Graphic design by line.out | Server sponsered by fxpansion