Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _ASM_WORD_AT_A_TIME_H
0003 #define _ASM_WORD_AT_A_TIME_H
0004 
0005 #include <linux/kernel.h>
0006 #include <asm/byteorder.h>
0007 
0008 #ifdef __BIG_ENDIAN
0009 
0010 struct word_at_a_time {
0011     const unsigned long high_bits, low_bits;
0012 };
0013 
0014 #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0xfe) + 1, REPEAT_BYTE(0x7f) }
0015 
0016 /* Bit set in the bytes that have a zero */
0017 static inline long prep_zero_mask(unsigned long val, unsigned long rhs, const struct word_at_a_time *c)
0018 {
0019     unsigned long mask = (val & c->low_bits) + c->low_bits;
0020     return ~(mask | rhs);
0021 }
0022 
0023 #define create_zero_mask(mask) (mask)
0024 
0025 static inline long find_zero(unsigned long mask)
0026 {
0027     long byte = 0;
0028 #ifdef CONFIG_64BIT
0029     if (mask >> 32)
0030         mask >>= 32;
0031     else
0032         byte = 4;
0033 #endif
0034     if (mask >> 16)
0035         mask >>= 16;
0036     else
0037         byte += 2;
0038     return (mask >> 8) ? byte : byte + 1;
0039 }
0040 
0041 static inline bool has_zero(unsigned long val, unsigned long *data, const struct word_at_a_time *c)
0042 {
0043     unsigned long rhs = val | c->low_bits;
0044     *data = rhs;
0045     return (val + c->high_bits) & ~rhs;
0046 }
0047 
0048 #ifndef zero_bytemask
0049 #define zero_bytemask(mask) (~1ul << __fls(mask))
0050 #endif
0051 
0052 #else
0053 
0054 /*
0055  * The optimal byte mask counting is probably going to be something
0056  * that is architecture-specific. If you have a reliably fast
0057  * bit count instruction, that might be better than the multiply
0058  * and shift, for example.
0059  */
0060 struct word_at_a_time {
0061     const unsigned long one_bits, high_bits;
0062 };
0063 
0064 #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
0065 
0066 #ifdef CONFIG_64BIT
0067 
0068 /*
0069  * Jan Achrenius on G+: microoptimized version of
0070  * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
0071  * that works for the bytemasks without having to
0072  * mask them first.
0073  */
0074 static inline long count_masked_bytes(unsigned long mask)
0075 {
0076     return mask*0x0001020304050608ul >> 56;
0077 }
0078 
0079 #else   /* 32-bit case */
0080 
0081 /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
0082 static inline long count_masked_bytes(long mask)
0083 {
0084     /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
0085     long a = (0x0ff0001+mask) >> 23;
0086     /* Fix the 1 for 00 case */
0087     return a & mask;
0088 }
0089 
0090 #endif
0091 
0092 /* Return nonzero if it has a zero */
0093 static inline unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
0094 {
0095     unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
0096     *bits = mask;
0097     return mask;
0098 }
0099 
0100 static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits, const struct word_at_a_time *c)
0101 {
0102     return bits;
0103 }
0104 
0105 static inline unsigned long create_zero_mask(unsigned long bits)
0106 {
0107     bits = (bits - 1) & ~bits;
0108     return bits >> 7;
0109 }
0110 
0111 /* The mask we created is directly usable as a bytemask */
0112 #define zero_bytemask(mask) (mask)
0113 
0114 static inline unsigned long find_zero(unsigned long mask)
0115 {
0116     return count_masked_bytes(mask);
0117 }
0118 
0119 #endif /* __BIG_ENDIAN */
0120 
0121 #endif /* _ASM_WORD_AT_A_TIME_H */