fast floor
Sönke Ludwig
sludwig at outerproduct.org
Fri Oct 11 06:05:36 PDT 2013
Am 11.10.2013 00:27, schrieb Spacen Jasset:
> Hello,
>
> I am after a fast floor function; In fact a fast truncation and
> conversion to integer. I see that std.math takes a real, and that
> std.c.math takes a double.
>
> Is there a quicker function, and what might cast(int)1.5f do?
>
> Regards,
>
> Spacen.
For a noise generator I wrote some little assembler routines for that
some years ago. It's main speed advantage comes from the fact that it
allows to set the rounding mode once (ftoi_init) instead of for every
conversion (as a cast(int) does for example). However, it requires great
care that the rounding mode isn't accidentally changed during the
computation by some function.
Today I would use core.simd instead, though (using XMM.CVTSS2SI), but
that requires that the whole algorithm is in SIMD for optimal performance.
This is the old code:
void ftoi_init() {
fesetround(FE_DOWNWARD);
}
void ftoi_toint2(int* dst, float src) {
asm {
mov EAX, dst;
fld src;
fistp [EAX];
}
}
void ftio_fracint(int* int_part, float* frac_part, float src) {
asm {
mov EAX, int_part;
mov EBX, frac_part;
fld src;
frndint;
fist [EAX];
fld src;
fsubr;
fstp [EBX];
}
}
void ftoi_round(float* dst, float src) {
asm {
mov EAX, dst;
fld src;
frndint;
fstp [EAX];
}
}
More information about the Digitalmars-d-learn
mailing list