lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Matt Campbell wrote:
> Mike Pall wrote:
>> Oh, and you realize that converting doubles to/from unsigned
>> integers is _dead slow_ on many platforms (but not necessarily so
>> for signed integers).
>
> On which platforms?

$ cat conv.c
#include <stdint.h>
intmax_t d2i(double x) { return (intmax_t)x; }
double i2d(intmax_t x) { return (double)x; }
uintmax_t d2u(double x) { return (uintmax_t)x; }
double u2d(uintmax_t x) { return (double)x; }

Check the assembler output with:
  cc -Os -fomit-frame-pointer -S -o - conv.c

On x86 (x87 FP):

d2i: basically fld + fistp, but rounding is set to truncation mode and back
i2d: fild qword [esp+4]      // One instruction!
d2u: call __fixunsdfdi       // Ouch!!
u2d: two cases for +/- and some bias tricks

On x64 (SSE FP):

d2i: cvttsd2si rax, xmm0     // One instruction!
i2d: cvtsi2sd  xmm0, rax     // One instruction!
d2u: two cases for +/- with FP comparison and some bias tricks
u2d: two cases for +/- and some bit shifting tricks

Don't have a PPC host at the moment, but AFAIR the situation was
pretty similar.

--Mike