xref: /reactos/sdk/lib/ucrt/string/amd64/strlen.s (revision b09b5584)
1#include <asm.inc>
2#include <ksamd64.inc>
3.code64
4#if 0
5        page    ,132
6        title   strlen - return the length of a null-terminated string
7;***
8;strlen.asm - contains strlen() routine
9;
10;       Copyright (c) Microsoft Corporation. All rights reserved.
11;
12;Purpose:
13;       strlen returns the length of a null-terminated string,
14;       not including the null byte itself.
15;
16;*******************************************************************************
17include ksamd64.inc
18        subttl  "strlen"
19;***
20;strlen - return the length of a null-terminated string
21;
22;Purpose:
23;       Finds the length in bytes of the given string, not including
24;       the final null character.
25;
26;       Algorithm:
27;       int strlen (const char * str)
28;       {
29;           int length = 0;
30;
31;           while( *str++ )
32;                   ++length;
33;
34;           return( length );
35;       }
36;
37;Entry:
38;       const char * str - string whose length is to be computed
39;
40;Exit:
41;       EAX = length of the string "str", exclusive of the final null byte
42;
43;Uses:
44;       EAX, ECX, EDX
45;
46;Exceptions:
47;
48;*******************************************************************************
49#endif
50
51LEAF_ENTRY_ARG1 strlen, _TEXT, buf:ptr byte
52
53    //OPTION PROLOGUE:NONE, EPILOGUE:NONE
54
55    mov   rax, rcx
56    neg   rcx          // for later
57    test  rax, 7
58    jz    main_loop_entry
59
60.byte HEX(66), HEX(90)
61
62byte_loop_begin:
63    mov   dl, [rax]
64    inc   rax
65    test  dl, dl
66    jz    return_byte_7
67    test  al, 7
68    jnz   byte_loop_begin
69
70main_loop_entry:
71    mov   r8, HEX(7efefefefefefeff)
72    mov   r11, HEX(8101010101010100)
73
74main_loop_begin:
75    mov   rdx, [rax]
76
77    mov   r9,  r8
78    add   rax, 8
79    add   r9,  rdx
80    not   rdx
81    xor   rdx, r9
82    and   rdx, r11
83    je    main_loop_begin
84
85main_loop_end:
86
87    mov   rdx, [rax-8]
88
89    test  dl, dl
90    jz    return_byte_0
91    test  dh, dh
92    jz    return_byte_1
93    shr   rdx, 16
94    test  dl, dl
95    jz    return_byte_2
96    test  dh, dh
97    jz    return_byte_3
98    shr   rdx, 16
99    test  dl, dl
100    jz    return_byte_4
101    test  dh, dh
102    jz    return_byte_5
103    shr   edx, 16
104    test  dl, dl
105    jz    return_byte_6
106    test  dh, dh
107    jnz   main_loop_begin
108
109return_byte_7:
110    lea   rax, [rax+rcx-1]
111    ret
112return_byte_6:
113    lea   rax, [rax+rcx-2]
114    ret
115return_byte_5:
116    lea   rax, [rax+rcx-3]
117    ret
118return_byte_4:
119    lea   rax, [rax+rcx-4]
120    ret
121return_byte_3:
122    lea   rax, [rax+rcx-5]
123    ret
124return_byte_2:
125    lea   rax, [rax+rcx-6]
126    ret
127return_byte_1:
128    lea   rax, [rax+rcx-7]
129    ret
130return_byte_0:
131    lea   rax, [rax+rcx-8]
132    ret
133
134LEAF_END strlen, _TEXT
135    end
136