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.
37 #if defined (__ARC64_ARCH32__)
40 LSRP.f 0, r2, 4 ; counter for 16-byte chunks
41 beq.d @.L_start_1_byte_search
50 ; r1 is now setup with the special 4 byte repetition of the target byte
51 ; We use r1 because we dont have any more registers free inside the main loop
52 ; r9 can be repurposed
60 #if defined (__ARC64_LL64__)
102 brne.d r3, 0, @.L_found_in_16B
104 ; Keep going we have more 16 byte chunks
107 brge r2, 16, @.L_search_16_bytes
109 ; Reset byte repetition of r1 to 1 single byte
112 .L_start_1_byte_search:
114 breq.d r2, 0, @.L_byte_not_found
119 breq r10, r1, @.L_found_byte
121 dbnz.d r2, @.L_search_1_byte
137 ; Select appropriate register to analyze [4]
140 ; Point r13 to first NULL byte containing double word [3]
157 xbfu r2, r2, 0b0111000011 ; [7]
167 lsrl.f 0, r2, 5 ; counter for 32-byte chunks
168 beq.d @.L_start_1_byte_search
179 ; r1 is now setup with the special 4 byte repetition of the target byte
180 ; We use r1 because we dont have any more registers free inside the main loop
181 ; r9 can be repurposed
182 vpack2wl r8, NULL_32DT_1, NULL_32DT_1
189 ; Using 128-bit memory operations
190 #if defined (__ARC64_M128__)
192 lddl.ab r4r5, [r0, +16]
193 lddl.ab r6r7, [r0, +16]
195 ; The 64-bit crunching implementation.
196 #elif defined (__ARC64_ARCH64__)
204 # error Unknown configuration
235 brne.d r3, 0, @.L_found_in_32B
237 ; Keep going we have more 16 byte chunks
239 brge r2, 32, @.L_search_32_bytes
241 ; Reset byte repetition of r1 to 1 single byte
244 .L_start_1_byte_search:
246 breq.d r2, 0, @.L_byte_not_found
251 breq r10, r1, @.L_found_byte
253 dbnz.d r2, @.L_search_1_byte
269 ; Select appropriate register to analyze [4]
272 ; Point r13 to first NULL byte containing double word [3]
284 andl r2, r2, r9 ; [5]
288 xbful r2, r2, 0b0111000011 ; [7]
291 addl r0, r0, r2 ; [8]
296 ;; This code uses a common technique for NULL byte detection inside a word.
297 ;; Details on this technique can be found in:
298 ;; (https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord)
300 ; In sum, this technique allows for detecting a NULL byte inside any given
301 ; amount of bits by performing the following operation
302 ; DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) [0]
304 ; The code above implements this by setting r8 to a 0x01010101... sequence and
305 ; r9 to a 0x80808080... sequence of appropriate length
306 ; As LIMM are 32 bit only, we need to perform MOVHL and ORL [1] operations to
307 ; have the appropriate 64 bit values in place
309 ; As we want a specific byte and not a NULL byte, we create in r1 a constant
310 ; that is made up of the target byte, on each byte position, that we xor with
311 ; the loaded data to force a NULL byte only if the target byte is present.
312 ; After that we can use the technique directly
314 ;; Search is done 32 bytes at a time, either with 64 bit loads or 128 bit loads
315 ;; If the target byte is detected, the position of the double word is encoded
316 ;; in r3, which is eventually used to adjust r0
318 ; r3 is set via bset, which means we can simply use a fls to obtain the first
319 ; match (or ffs depending on the values in bset) [2].
320 ; The reason for starting at 1 and not 0 is so r3 encodes how many double
321 ; words to go back, and it wouldnt make sense to go back 0 (the byte would be
322 ; in the next loop iteration).
324 ; The first step to take is point r0 to the appropriate double word.
325 ; As the chosen encoded information is how many double words to go back,
326 ; we can simply multiply r3 by 8 and reduce r0 by that amount [3]
328 ; Then, we need to place the loaded double word containing the first target byte
329 ; found, into a "common" register we can operate on later [4].
331 ; To do this without any jumps, we can shift r3 and perform a conditional mov
332 ; based on the carry flag value.
333 ; The order is very important because the byte can appear in several double
334 ; words, so we want to analyze from last to first.
336 ; We can ignore the first asr (which would be asr.f 2, as we started r3 on 1)
337 ; because if r13 isnt the target byte, r2 will always be overwritten so we can
338 ; just decide to start at r7, and overwrite it if needed.
340 ; Now comes the tricky part. In order to obtain the first target byte, we need
341 ; to understand the NULL byte detection operation. It is explained in depth in
342 ; the link above but in short, it works by first setting the highest bit of each
343 ; byte to 1, if the corresponding byte is either 0 or more than 0x80
344 ; Then, separately, it makes the highest bit of each byte 1, if the byte is
345 ; less than 0x80. The last step is to AND these two values (this operation is
346 ; simplified with the SUB, BIC and TST instructions).
348 ; This means that the evaluated equation result value [5] has zeros for all non
349 ; zero bytes, except for the NULL bytes (which are the target bytes after the
350 ; xor). Therefore, we can simply find the first non zero bit (counting from bit
351 ; 0) which will be inside the position of the first NULL byte.
353 ; One thing to note, is that ffs oddly returns 31 if no bit is found, setting
354 ; the zero flag. As r9 is never all 0s at this stage (would mean there is no
355 ; NULL byte and we wouldnt be here) we dont need to worry about that. [6]
357 ; We can then convert the bit position into the last byte position by looking
358 ; into bits 3 to 5, and shifting 3 bits to the right. This can be combined into
359 ; a single xbful operation. The bottom 000011 represent shift by 3 and the top
360 ; 0111 represents the mask (3 to 5 shifted by 3 is 0 to 2). We dont need to
361 ; worry about the case where ffs does not find a bit, because we know for sure
362 ; there is at least one NULL byte, and therefore one of the highest bits is set
365 ; Finally, we can add the NULL/target byte position inside the loaded double
366 ; word to r0 to obtain the bytes absolute position [8]
369 ; Some operations are re-ordered such that register dependency is reduced,
370 ; allowing the CPU to run more instructions in parallel