tags:

views:

94

answers:

2

I need to combine two 32-bit values to create a 64-bit value. I'm looking for something analogous to MAKEWORD and MAKELONG. I can easily define my own macro or function, but if the API already provides one, I'd prefer to use that.

A: 

Use Int32x32To64.

mcandre
@mcandre: You do know that Int32x32To64 multiplies the two arguments, right?
Andreas Rejbrand
A: 

I cannot find any in the Windows API. However, I do know that you work mostly (or, at least, a lot) with Delphi, so here is a quick Delphi function:

function MAKELONGLONG(A, B: cardinal): UInt64; inline;
begin
  PCardinal(@result)^ := A;
  PCardinal(cardinal(@result) + sizeof(cardinal))^ := B;
end;

Even faster:

function MAKELONGLONG(A, B: cardinal): UInt64;
asm
end;

Explanation: In the normal register calling convention, the first two arguments (if cardinal-sized) are stored in EAX and EDX, respetively. A (cardinal-sized) result is stored in EAX. Now, a 64-bit result is stored in EAX (less significant bits, low address) and EDX (more significant bits, high address); hence we need to move A to EAX and B to EDX, but they are already there!

Andreas Rejbrand
Thanks for considering my background, but as it turns out, I'm working in C and C++ right now. Your asm trick is clever. I'd name the variables `H` and `L` so that Code Completion would remind me what order they go in. My Delphi implementation would have been comparable to a C implementation: `Result := (UInt64(H) shl 32) or L`.
Rob Kennedy
I had hoped for an answer a little more definitive than "I don't see one," but I probably set my sights too high, so this answer gets the prize. Thanks.
Rob Kennedy