[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
**Subject**: **Re: bitop multiplication overflow semantics**
**From**: Daurnimator <quae@...>
**Date**: Tue, 22 Jan 2013 00:04:41 +1100

On 22 January 2013 00:01, Daurnimator <quae@daurnimator.com> wrote:
> Use the ffi:
>
>> ffi = require "ffi"
>> to_uint32 = ffi.ctype("uint32_t")
>> x=12345678
>> =to_uint32(x*0xcc9e2d51)
> cdata<unsigned int>: 0x40e2eb28
>
>
> On 21 January 2013 23:34, Cosmin Apreutesei <cosmin.apreutesei@gmail.com> wrote:
>> Hi,
>>
>> I wanted to port a hash function from C to LuaJIT. First thing the
>> algorithm does is it takes the first uint32_t of the key and
>> multiplies it with 0xcc9e2d51, the destination variable being itself a
>> uint32_t. I don't know the semantics of this in C, but whatever it is,
>> can I emulate this in LuaJIT / LuaBitOp or other library? This
>> multiplication would exceed 53bit if it were between Lua numbers.
>>
>> Thanks.
>>
Apologies, I was confused about the issue.
Just make sure your variable is already a cdata type.
>From http://luajit.org/ext_ffi_semantics.html - Arithmetic on cdata
objects - 64 bit integer arithmetic:
the standard arithmetic operators (+ - * / % ^ and unary minus) can be
applied to two cdata numbers, or a cdata number and a Lua number. If
one of them is an uint64_t, the other side is converted to an uint64_t
and an unsigned arithmetic operation is performed. Otherwise both
sides are converted to an int64_t and a signed arithmetic operation is
performed. The result is a boxed 64 bit cdata object.
> =to_uint32(to_uint32(x)*0xcc9e2d51)
cdata<unsigned int>: 0x40e2f0d8