JoelKatz

Don't hide alignment from 'memset'.

Dec 19th, 2012
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. Test code:
  2.  
  3. #include <string.h>
  4. char* mem_demo_1(char *j)
  5. { // *BAD* compiler cannot tell pointer alignment, must test
  6. memset(j, 0, 64);
  7. return j;
  8. }
  9. char* mem_demo_2(void)
  10. { // *GOOD* compiler can tell pointer alignment
  11. char * j = malloc(64);
  12. memset(j, 0, 64);
  13. return j;
  14. }
  15.  
  16. Compiled with GCC 4.6.3, options -O3. Also tested with -O2, results are about the same.
  17.  
  18. mem_demo_1:
  19. .LFB12:
  20. .cfi_startproc
  21. movq %rdi, %rsi
  22. movl $64, %edx
  23. testb $1, %sil
  24. jne .L9
  25. testb $2, %dil
  26. jne .L10
  27. .L3:
  28. testb $4, %dil
  29. jne .L11
  30. .L4:
  31. movl %edx, %ecx
  32. xorl %eax, %eax
  33. shrl $3, %ecx
  34. testb $4, %dl
  35. rep stosq
  36. je .L5
  37. movl $0, (%rdi)
  38. addq $4, %rdi
  39. .L5:
  40. testb $2, %dl
  41. je .L6
  42. movw $0, (%rdi)
  43. addq $2, %rdi
  44. .L6:
  45. andl $1, %edx
  46. je .L7
  47. movb $0, (%rdi)
  48. .L7:
  49. movq %rsi, %rax
  50. ret
  51. .p2align 4,,10
  52. .p2align 3
  53. .L9:
  54. leaq 1(%rsi), %rdi
  55. movb $0, (%rsi)
  56. movb $63, %dl
  57. testb $2, %dil
  58. je .L3
  59. .p2align 4,,10
  60. .p2align 3
  61. .L10:
  62. movw $0, (%rdi)
  63. addq $2, %rdi
  64. subl $2, %edx
  65. testb $4, %dil
  66. je .L4
  67. .p2align 4,,10
  68. .p2align 3
  69. .L11:
  70. movl $0, (%rdi)
  71. subl $4, %edx
  72. addq $4, %rdi
  73. jmp .L4
  74. .cfi_endproc
  75. ...
  76.  
  77. mem_demo_2:
  78. .LFB13:
  79. .cfi_startproc
  80. subq $8, %rsp
  81. .cfi_def_cfa_offset 16
  82. movl $64, %edi
  83. call malloc
  84. movq %rax, %rdx
  85. movl $8, %ecx
  86. xorl %eax, %eax
  87. movq %rdx, %rdi
  88. rep stosq
  89. movq %rdx, %rax
  90. addq $8, %rsp
  91. .cfi_def_cfa_offset 8
  92. ret
  93. .cfi_endproc
Advertisement
Add Comment
Please, Sign In to add comment