-
Notifications
You must be signed in to change notification settings - Fork 6
/
strcountset32.asm
194 lines (162 loc) · 6.55 KB
/
strcountset32.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
;************************* strcountinset32.asm *********************************
; Author: Agner Fog
; Date created: 2011-07-20
; Last modified: 2011-08-21
; Description:
; size_t strCountInSet(const char * str, const char * set);
;
; Counts how many characters in str that belong to the set defined by set.
; Both strings are zero-terminated ASCII strings.
;
; Note that this functions may read up to 15 bytes beyond the end of the strings.
; This is rarely a problem but it can in principle generate a protection violation
; if a string is placed at the end of the data segment.
;
; Position-independent code is generated if POSITIONINDEPENDENT is defined.
;
; CPU dispatching included for 386 and SSE4.2 instruction sets.
;
; Copyright (c) 2011 GNU General Public License www.gnu.org/licenses
;******************************************************************************
global _strCountInSet: function
; Direct entries to CPU-specific versions
global _strCountInSetGeneric: function
global _strCountInSetSSE42: function
; Imported from instrset32.asm:
extern _InstructionSet ; Instruction set for CPU dispatcher
section .text
;******************************************************************************
; strCountInSet function
;******************************************************************************
_strCountInSet: ; function dispatching
%IFNDEF POSITIONINDEPENDENT
jmp near [strCountInSetDispatch] ; Go to appropriate version, depending on instruction set
%ELSE ; Position-independent code
call get_thunk_edx ; get reference point for position-independent code
RP1: ; reference point edx = offset RP1
; Make the following instruction with address relative to RP1:
jmp near [edx+strCountInSetDispatch-RP1]
%ENDIF
align 16
_strCountInSetSSE42: ; SSE4.2 version
push esi
push edi
mov esi, [esp+12] ; str
mov edi, [esp+16] ; set
xor eax, eax ; match counter
str_next:
movdqu xmm2, [esi] ; str
movdqu xmm1, [edi] ; set
pcmpistrm xmm1, xmm2, 00000000b; find in set, return bit mask in xmm0
movd ecx, xmm0
jns set_extends ; the set is more than 16 bytes
jz str_finished
set_finished:
popcnt ecx, ecx
add eax, ecx
; first 16 characters checked, continue with next 16 characters (a terminating zero would never match)
add esi, 16 ; next 16 bytes of str
jmp str_next
set_and_str_finished:
or ecx, edx ; accumulate matches
str_finished:
popcnt ecx, ecx
add eax, ecx
pop edi
pop esi
ret
set_loop:
or ecx, edx ; accumulate matches
set_extends:
add edi, 16
movdqu xmm1, [edi] ; next part of set
pcmpistrm xmm1, xmm2, 00000000b; find in set, return bit mask in xmm0
movd edx, xmm0
jns set_loop
jz set_and_str_finished
mov edi, [esp+16] ; restore set pointer
or ecx, edx ; accumulate matches
jmp set_finished
;_strCountInSetSSE42 end
;******************************************************************************
; strCountInSet function generic
;******************************************************************************
align 8
_strCountInSetGeneric: ; Generic version
push esi
push edi
mov esi, [esp+12] ; str pointer
mov edi, [esp+16] ; set pointer
xor eax, eax ; match counter
str_next10:
mov cl, [esi] ; read one byte from str
test cl, cl
jz str_finished10 ; str finished
set_next10:
mov dl, [edi]
test dl, dl
jz set_finished10
inc edi ; next in set
cmp cl, dl
jne set_next10
; character match found, goto next character
inc eax ; count match
inc esi
jmp str_next10
set_finished10: ; end of set, no match found
mov edi, [esp+16] ; restore set pointer
inc esi
jmp str_next10 ; next in string
str_finished10: ; end of str, count is in eax
pop edi
pop esi
ret
;_strCountInSetGeneric end
; ********************************************************************************
%IFDEF POSITIONINDEPENDENT
get_thunk_edx: ; load caller address into edx for position-independent code
mov edx, [esp]
ret
%ENDIF
; ********************************************************************************
; CPU dispatching for strCountInSet. This is executed only once
; ********************************************************************************
strCountInSetCPUDispatch:
%IFNDEF POSITIONINDEPENDENT
; get supported instruction set
call _InstructionSet
; Point to generic version of strstr
mov ecx, _strCountInSetGeneric
cmp eax, 10 ; check SSE4.2
jb Q100
; SSE4.2 supported
; Point to SSE4.2 version of strstr
mov ecx, _strCountInSetSSE42
Q100: mov [strCountInSetDispatch], ecx
; Continue in appropriate version
jmp ecx
%ELSE ; Position-independent version
; get supported instruction set
call _InstructionSet
call get_thunk_edx
RP11: ; reference point edx
; Point to generic version
lea ecx, [edx+_strCountInSetGeneric-RP11]
cmp eax, 10 ; check SSE4.2
jb Q100
; SSE4.2 supported
; Point to SSE4.2 version of strstr
lea ecx, [edx+_strCountInSetSSE42-RP11]
Q100: mov [edx+strCountInSetDispatch-RP11], ecx
; Continue in appropriate version
jmp ecx
%ENDIF
SECTION .data
; Pointer to appropriate versions. Initially point to dispatcher
strCountInSetDispatch DD strCountInSetCPUDispatch
%IFDEF POSITIONINDEPENDENT
; Fix potential problem in Mac linker
DD 0, 0
%ENDIF
SECTION .bss
dq 0, 0