2 Copyright (c) 2024, Synopsys, Inc. All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
7 1) Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
10 2) Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 3) Neither the name of the Synopsys, Inc., nor the names of its contributors
15 may be used to endorse or promote products derived from this software
16 without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 POSSIBILITY OF SUCH DAMAGE.
33 ; Code Brief (more info at the bottom):
34 ; Searches the provided string, 32 bytes at a time, using 128 bit loads
35 ; Finds the NULL bytes inside the loaded data
36 ; Analyzes the first NULL byte containing double word and calculates
39 ; R0 const char* ptr (string to measure)
41 ; - unsigned (string size)
44 #if defined (__ARC64_ARCH32__)
48 ; Preserve r0 for size calculation when returning
52 ; Setup byte detector (more information bellow) [1]
54 ; Set r9 as a copy of r8 for vectorized sub
61 #if defined (__ARC64_LL64__)
63 ldd.ab r2r3, [r13, +8]
64 ldd.ab r4r5, [r13, +8]
75 ; NULL byte position is detected and encoded in r12 [0] [9]
97 breq.d r12, 0, @.L_4_4B_search
101 ; Point r13 to first NULL byte containing double word [3]
104 ; Select appropriate register to analyze [4]
116 ; Point r13 to first NULL byte in selected double word
122 xbfu r1, r1, 0b0111000011 ; [7]
124 add r13, r13, r1 ; [8]
136 ; Preserve r0 for size calculation when returning
140 ; Setup byte detector (more information bellow) [1]
141 vpack2wl r8, NULL_32DT_1, NULL_32DT_1
147 ; Using 128-bit memory operations
148 #if defined (__ARC64_M128__)
150 lddl.ab r2r3, [r13, +16]
151 lddl.ab r4r5, [r13, +16]
153 ; The 64-bit crunching implementation.
154 #elif defined (__ARC64_ARCH64__)
162 # error Unknown configuration
165 ; NULL byte position is detected and encoded in r6 [0] [9]
188 breq.d r12, 0, @.L_4_8B_search
192 ; Point r13 to first NULL byte containing double word [3]
195 ; Select appropriate register to analyze [4]
207 ; Point r13 to first NULL byte in selected double word
209 andl r1, r2, r1 ; [5]
213 xbful r1, r1, 0b0111000011 ; [7]
215 addl r13, r13, r1 ; [8]
225 ;; This code uses a common technique for NULL byte detection inside a word.
226 ;; Details on this technique can be found in:
227 ;; (https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord)
229 ; In sum, this technique allows for detecting a NULL byte inside any given
230 ; amount of bits by performing the following operation
231 ; DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) [0]
233 ; The code above implements this by setting r8 to a
234 ; 0x01010101... sequence and r1 to a 0x80808080... sequence of
235 ; appropriate length As LIMM are 32 bit only, we need to perform MOVHL
236 ; and ORL [1] operations to have the appropriate 64 bit values in
239 ;; Search is done 32 bytes at a time, either with 64 bit loads or 128
240 ;; bit loads If a NULL byte is detected, the position of the double
241 ;; word is encoded in r12, which is then used to adjust r13
243 ; r12 is set via bset, which means we can simply use a fls to obtain
244 ; the first match (or ffs depending on the values in bset) [2]. The
245 ; reason for starting at 1 and not 0 is so r12 encodes how many double
246 ; words to go back, and it wouldnt make sense to go back 0 (the NULL
247 ; would be in the next loop iteration).
249 ; The first step to take is point r13 to the appropriate double word.
250 ; As the chosen encoded information is how many double words to go
251 ; back, we can simply multiply r12 by 8 and reduce r13 by that amount
254 ; Then, we need to place the loaded double word containing the first
255 ; NULL byte into a "common" register we can operate on later [4].
257 ; To do this without any jumps, we can shift r12 and perform a
258 ; conditional mov based on the carry flag value. The order is very
259 ; important because the NULL byte can appear in several double words,
260 ; so we want to analyze from last to first.
262 ; We can ignore the first asr (which would be asr.f 2, as we started
263 ; r12 on 1) because if r7 isnt the NULL byte, r2 will always be
264 ; overwritten so we can just decide to start at r7, and overwrite it
267 ; Now comes the tricky part. In order to obtain the first NULL byte,
268 ; we need to understand the NULL byte detection operation. It is
269 ; explained in depth in the link above but in short, it works by first
270 ; setting the highest bit of each byte to 1, if the corresponding byte
271 ; is either 0 or more than 0x80 Then, separately, it makes the highest
272 ; bit of each byte 1, if the byte is less than 0x80. The last step is
273 ; to AND these two values (this operation is simplified with the SUB,
274 ; BIC and TST instructions).
276 ; This means that the evaluated equation result value [5] has zeros
277 ; for all non zero bytes, except for the NULL bytes. Therefore, we can
278 ; simply find the first non zero bit (counting from bit 0) which will
279 ; be inside the position of the first NULL byte.
281 ; One thing to note, is that ffs oddly returns 31 if no bit is found,
282 ; setting the zero flag. As r9 is never all 0s at this stage (would
283 ; mean there is no NULL byte and we wouldnt be here) we dont need to
284 ; worry about that. [6]
286 ; We can then convert the bit position into the last byte position by
287 ; looking into bits 3 to 5, and shifting 3 bits to the right. This can
288 ; be combined into a single xbful operation. The bottom 000011
289 ; represent shift by 3 and the top 0111 represents the mask (3 to 5
290 ; shifted by 3 is 0 to 2). We dont need to worry about the case where
291 ; ffs does not find a bit, because we know for sure there is at least
292 ; one NULL byte, and therefore one of the highest bits is set to 1 [7]
294 ; Finally, we can add the NULL byte position inside the loaded double
295 ; word to r13 and subtract r0 from r13 to obtain the string size [8]
298 ; Some operations are re-ordered such that register dependency is
299 ; reduced, allowing the CPU to run more instructions in parallel [9]