/* SPDX-License-Identifier: GPL-2.0 */1/*2* strlen() for PPC323*4* Copyright (C) 2018 Christophe Leroy CS Systemes d'Information.5*6* Inspired from glibc implementation7*/8#include <linux/export.h>9#include <asm/ppc_asm.h>10#include <asm/cache.h>1112.text1314/*15* Algorithm:16*17* 1) Given a word 'x', we can test to see if it contains any 0 bytes18* by subtracting 0x01010101, and seeing if any of the high bits of each19* byte changed from 0 to 1. This works because the least significant20* 0 byte must have had no incoming carry (otherwise it's not the least21* significant), so it is 0x00 - 0x01 == 0xff. For all other22* byte values, either they have the high bit set initially, or when23* 1 is subtracted you get a value in the range 0x00-0x7f, none of which24* have their high bit set. The expression here is25* (x - 0x01010101) & ~x & 0x80808080), which gives 0x00000000 when26* there were no 0x00 bytes in the word. You get 0x80 in bytes that27* match, but possibly false 0x80 matches in the next more significant28* byte to a true match due to carries. For little-endian this is29* of no consequence since the least significant match is the one30* we're interested in, but big-endian needs method 2 to find which31* byte matches.32* 2) Given a word 'x', we can test to see _which_ byte was zero by33* calculating ~(((x & ~0x80808080) - 0x80808080 - 1) | x | ~0x80808080).34* This produces 0x80 in each byte that was zero, and 0x00 in all35* the other bytes. The '| ~0x80808080' clears the low 7 bits in each36* byte, and the '| x' part ensures that bytes with the high bit set37* produce 0x00. The addition will carry into the high bit of each byte38* iff that byte had one of its low 7 bits set. We can then just see39* which was the most significant bit set and divide by 8 to find how40* many to add to the index.41* This is from the book 'The PowerPC Compiler Writer's Guide',42* by Steve Hoxey, Faraydon Karim, Bill Hay and Hank Warren.43*/4445_GLOBAL(strlen)46andi. r0, r3, 347lis r7, 0x010148addi r10, r3, -449addic r7, r7, 0x0101 /* r7 = 0x01010101 (lomagic) & clear XER[CA] */50rotlwi r6, r7, 31 /* r6 = 0x80808080 (himagic) */51bne- 3f52.balign IFETCH_ALIGN_BYTES531: lwzu r9, 4(r10)542: subf r8, r7, r955and. r8, r8, r656beq+ 1b57andc. r8, r8, r958beq+ 1b59andc r8, r9, r660orc r9, r9, r661subfe r8, r6, r862nor r8, r8, r963cntlzw r8, r864subf r3, r3, r1065srwi r8, r8, 366add r3, r3, r867blr6869/* Missaligned string: make sure bytes before string are seen not 0 */703: xor r10, r10, r071orc r8, r8, r872lwzu r9, 4(r10)73slwi r0, r0, 374srw r8, r8, r075orc r9, r9, r876b 2b77EXPORT_SYMBOL(strlen)787980