c - Any reason to use 32 bit integers for common operations on 64 bit CPU? -


i wonder if it's idea keep using int (which 32 bit on both x86 , x86_64) on 64 bit programs variables have nothing special , not need span 2^64, iteration counters, or if it's better use size_t matches word size of cpu.

for sure if keep using int save half of memory, , mean speaking cpu cache, don't know if on 64 bit machine every 32 bit number has extended 64 bit before use.

edit: i've ran test program of mine (see self answer, still keep janneb's accepted though because good). turns out there significant performance improvement.

for array indices , pointer arithmetic, types of same size pointer (typically, size_t , ptrdiff_t) can better, avoid need 0 or sign extend register. consider

 float onei(float *a, int n) {   return a[n]; }  float oneu(float *a, unsigned n) {   return a[n]; }  float onep(float *a, ptrdiff_t n) {   return a[n]; }  float ones(float *a, size_t n) {   return a[n]; } 

with gcc 4.4 -o2 on x86_64 following asm generated:

     .p2align 4,,15 .globl onei     .type   onei, @function onei: .lfb3:     .cfi_startproc     movslq  %esi,%rsi     movss   (%rdi,%rsi,4), %xmm0     ret     .cfi_endproc .lfe3:     .size   onei, .-onei     .p2align 4,,15 .globl oneu     .type   oneu, @function oneu: .lfb4:     .cfi_startproc     mov %esi, %esi     movss   (%rdi,%rsi,4), %xmm0     ret     .cfi_endproc .lfe4:     .size   oneu, .-oneu     .p2align 4,,15 .globl onep     .type   onep, @function onep: .lfb5:     .cfi_startproc     movss   (%rdi,%rsi,4), %xmm0     ret     .cfi_endproc .lfe5:     .size   onep, .-onep     .p2align 4,,15 .globl ones     .type   ones, @function ones: .lfb6:     .cfi_startproc     movss   (%rdi,%rsi,4), %xmm0     ret     .cfi_endproc .lfe6:     .size   ones, .-ones 

as can seen, versions int , unsigned int index (onei , oneu) requires instruction (movslq/mov) sign/zero extend register.

as mentioned in comment, downside encoding 64-bit register takes more space 32-bit part, bloating code size. secondly, ptrdiff_t/size_t variables need more memory equivalent int; if have such arrays can affect performance more relatively small benefit of avoiding zero/sign extension. if unsure, profile!


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -