Fast rounding functions in pascal¶
- Author or source: moc.liamtoh@abuob
- Type: round/ceil/floor/trunc
- Created: 2008-03-09 13:09:44
Original documentation with cpp samples:
http://ldesoras.free.fr/prod.html#doc_rounding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 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¶
- Date: 2008-04-23 11:46:58
- By: eb.mapstenykson@didid
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.