#include <string.h>
#include <wchar.h>
#include <uchar.h>
#define char8_t char
#define strlen8 strlen
#define strlen16 strlen16
#define strlen32(s) wcslen((const wchar_t*)s)
static inline size_t strlen16(register const char16_t * string) {
if (!string) return 0;
register size_t len = 0;
while(string[len++]);
return len;
}
您应该期望char16_t
返回的字符数,而不是字节数。
优化的 32 位 Intel Atom 组装视图:
gcc -Wpedantic -std=iso9899:2011 -g3 -O2 -MMD -faggressive-loop-optimizations -fkeep-inline-functions -march=atom -mtune=atom -fomit-frame-pointer -mssse3 -mieee-fp -mfpmath=sse -fexcess-precision=fast -mpush-args -mhard-float -fPIC ...
.Ltext0:
.p2align 4,,15
.type strlen16, @function
strlen16:
.LFB20:
.cfi_startproc
.LVL0:
mov edx, DWORD PTR 4[esp]
xor eax, eax
test edx, edx
je .L4
.p2align 4,,15
.L3:
.LVL1:
lea eax, 1[eax]
.LVL2:
cmp WORD PTR -2[edx+eax*2], 0
jne .L3
ret
.LVL3:
.p2align 4,,7
.p2align 3
.L4:
ret
.cfi_endproc
.LFE20:
.size strlen16, .-strlen16
这是英特尔的反汇编:
static inline size_t strlen16(register const char16_t * string) {
0: 8b 54 24 04 mov edx,DWORD PTR [esp+0x4]
if (!string) return 0;
4: 31 c0 xor eax,eax
6: 85 d2 test edx,edx
8: 74 16 je 20 <strlen16+0x20>
a: 8d b6 00 00 00 00 lea esi,[esi+0x0]
register size_t len = 0;
while(string[len++]);
10: 8d 40 01 lea eax,[eax+0x1]
13: 66 83 7c 42 fe 00 cmp WORD PTR [edx+eax*2-0x2],0x0
19: 75 f5 jne 10 <strlen16+0x10>
1b: c3 ret
1c: 8d 74 26 00 lea esi,[esi+eiz*1+0x0]
return len;
}
20: c3 ret
21: eb 0d jmp 30 <AnonymousFunction0>
23: 90 nop
24: 90 nop
25: 90 nop
26: 90 nop
27: 90 nop
28: 90 nop
29: 90 nop
2a: 90 nop
2b: 90 nop
2c: 90 nop
2d: 90 nop
2e: 90 nop
2f: 90 nop