循环与字符串操作的深入解析
1. 内存块复制例程
在实际应用中,内存复制例程可能会在每次迭代中复制 4 或 8 字节,使用 SIMD、向量化等技术。但为了简单起见,下面是一个最简单的内存复制函数示例:
#include <stdio.h>
void my_memcpy (unsigned char* dst, unsigned char* src, size_t cnt)
{
size_t i;
for (i=0; i<cnt; i++)
dst[i]=src[i];
};
不同编译器对这个函数的优化结果如下:
- GCC 4.9 x64 优化版(-Os)
my_memcpy:
; RDI = destination address
; RSI = source address
; RDX = size of block
; initialize counter (i) at 0
xor eax, eax
.L2:
; all bytes copied? exit then:
cmp rax, rdx
je .L5
; load byte at RSI+i:
mov cl, BYTE PTR [rsi+rax]
; store byte at RDI+i:
mov BYTE PTR [rdi+rax], cl
inc rax ; i++
超级会员免费看
订阅专栏 解锁全文
1540

被折叠的 条评论
为什么被折叠?



