^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef _ASM_WORD_AT_A_TIME_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _ASM_WORD_AT_A_TIME_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #ifdef __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) struct word_at_a_time {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) const unsigned long high_bits, low_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0xfe) + 1, REPEAT_BYTE(0x7f) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /* Bit set in the bytes that have a zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static inline long prep_zero_mask(unsigned long val, unsigned long rhs, const struct word_at_a_time *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned long mask = (val & c->low_bits) + c->low_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return ~(mask | rhs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define create_zero_mask(mask) (mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static inline long find_zero(unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) long byte = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (mask >> 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) mask >>= 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) byte = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (mask >> 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) mask >>= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) byte += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return (mask >> 8) ? byte : byte + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static inline bool has_zero(unsigned long val, unsigned long *data, const struct word_at_a_time *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned long rhs = val | c->low_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *data = rhs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return (val + c->high_bits) & ~rhs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #ifndef zero_bytemask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define zero_bytemask(mask) (~1ul << __fls(mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * The optimal byte mask counting is probably going to be something
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * that is architecture-specific. If you have a reliably fast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * bit count instruction, that might be better than the multiply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * and shift, for example.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct word_at_a_time {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) const unsigned long one_bits, high_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * Jan Achrenius on G+: microoptimized version of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * that works for the bytemasks without having to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * mask them first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static inline long count_masked_bytes(unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return mask*0x0001020304050608ul >> 56;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #else /* 32-bit case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static inline long count_masked_bytes(long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) long a = (0x0ff0001+mask) >> 23;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* Fix the 1 for 00 case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return a & mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* Return nonzero if it has a zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static inline unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) *bits = mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits, const struct word_at_a_time *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static inline unsigned long create_zero_mask(unsigned long bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) bits = (bits - 1) & ~bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return bits >> 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* The mask we created is directly usable as a bytemask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #define zero_bytemask(mask) (mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static inline unsigned long find_zero(unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return count_masked_bytes(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #endif /* __BIG_ENDIAN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #endif /* _ASM_WORD_AT_A_TIME_H */