Re: small gotcha with cdata and number

  • From: Dimiter 'malkia' Stanev <malkia@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 30 May 2012 11:28:13 -0700

Hex numbers are only 32-bits, and probably done so because the 64-bit cannot be used fully due to the way luajit uses double to encode other things, and also that double has only 53-bits mantissa

And this could be probably to avoid (or in more negative sense - force) people to not write code that relies on 64-bit operands if possible, but use 32-bit instead. I think arithmetic with 64-bits might still work, if you deal with uint64_t arrays and do operations in between them, but not with variables
flying around.

And although almost all modern architectures have native 64-bit ops (even for 32-bit cpus), almost any algorithms that I've seen out there still rely on 32-bit logical ops.

I could be far off this assumption, but that's my gut feeling.

On 5/30/2012 11:18 AM, William Adams wrote:
Yah, I actually use bit.tobit, but, before I did that,

the part that actually was biting me was this:

avar = 0x100000000ULL

This creates a value of cdata type, rather than number.

Ignoring this specific usage, should this hex literal

be converted to a number or cdata?  I'd assume number.

This is with beta9, and possibly it's been fixed?

-- William


- Shaping clay is easier than digging it out of the ground.

Date: Wed, 30 May 2012 19:57:04 +0200
From: mike-1205@xxxxxxxxxx
To: luajit@xxxxxxxxxxxxx
Subject: Re: small gotcha with cdata and number

William Adams wrote:
As part of a SHA1 calculation, there is this function

which will add two 32-bit numbers, and ensure the

result is still 32 bits.

-- adding 2 32bit numbers, cutting off the remainder on 33rd bit
local function w32_add (a,b)
return (a+b) % 4294967296
Ick, please don't do that -- it's very slow.

Use bit.tobit(a+b) explicitly, if you must. But you can avoid that
in most cases, since you're passing these things to other bit.*
functions, which implicitly perform the bit.tobit() conversion.

In fact, the JIT compiler optimizes a+b passed to a bit operation
into a simple 32 bit integer ADD in the machine code.

The Lua BitOp distribution has an example MD5 implementation that
generates pretty good code with LuaJIT 2.0:


Other related posts: