Skip to content
Advertisement

X86-64 passing float and int as arguments

When passing a float to a function as the first argument the %xmm0 register gets set. What if we want to pass an integer as the second argument in the function. Which one of %rdi or %rsi gets the argument?

e.g. avg ( float f, int i )

Is the order preserved, so that %rsi will get the int?

Edit: it is an Ubuntu machine.

Advertisement

Answer

float args don’t take arg-passing slots away from integer/pointer args in the x86-64 System V calling convention.

All that matters is that i is the first integer/pointer arg that’s a candidate for passing in GP register, so it goes in RDI. (Actually EDI, the upper 32 bits of RDI can be garbage.)

Large structs (passed in memory) also don’t take away arg-passing slots, but small structs get packed into up-to-2 integer registers. See the ABI doc, or more simply just look at compiler output.


In Windows x64, by contrast, the logic for assigning args to registers is pretty much totally different, because their calling convention makes different design choices (optimized for variadic functions, not efficiency of normal functions.)

i is the 2nd arg overall, so it can be passed in a register. And since it’s integer, it goes in a GP register, specifically RDX. (Actually EDX). Windows x64 passes the first 4 args in RCX or XMM0, RDX or XMM1, R8 or XMM2, R9 or XMM3.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement