IamIC IamIC - 1 year ago 77
C Question

When targeting 64-bit platforms in C, is it better to use 64-bit variables for array references?

I was looking at the assembly of a code snippet I wrote and noticed that the indicated

op only appears if the
variable is 32-bit. If
is 64-bit, it is used directly:
mov BYTE PTR [rdi+rbp+86], al

; 861 : _BitScanForward(&depth, subject);

movsx edx, dx

; 862 : qry_args->lo_refs[++ret] = (BYTE)depth;

inc ebp // ret is in ebp
bsf ecx, edx
movsxd rax, ebp // convert 32-bit ebp to 64-bit rax

; 865 : subject ^= (1 << depth);
; 866 : nulls_mask.lo |= (1 << depth);

movsx r9d, r9w
btc edx, ecx
bts r9d, ecx
mov BYTE PTR [rax+rbx+86], cl // 64-bit rax used by mov

Since the
op requires 64-bit registers in 64-bit mode, it makes sense to me that any variables used to reference memory (such as array referencers) should ideally be in 64-bit.

However, I know it's common to simply use
in a loop that is not going to exceed 2^^31 iterations. Should we in fact be using
long long
) as a best practice for 64-bit code? Any comments on this?

I haven't gone to the trouble of testing this beyond what is shown here.

Ps. This isn't a micro-optimization question. It's a question of form. To me, it makes sense to use the type used by the compiler.

Info: I'm compiling with VS 2016 with max optimizations on.

Answer Source

Use the size_t type for array indexes. It is large enough to hold array indexes. Usually it holds 64 bits on 64-bit platforms and 32 bits on 32-bit platforms.

See http://stackoverflow.com/a/2550799/909655

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download