xref: /openbmc/qemu/include/qemu/bitops.h (revision 3d7680fb)
1 /*
2  * Bitops Module
3  *
4  * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
5  *
6  * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
7  *
8  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
9  * See the COPYING.LIB file in the top-level directory.
10  */
11 
12 #ifndef BITOPS_H
13 #define BITOPS_H
14 
15 
16 #include "host-utils.h"
17 #include "atomic.h"
18 
19 #define BITS_PER_BYTE           CHAR_BIT
20 #define BITS_PER_LONG           (sizeof (unsigned long) * BITS_PER_BYTE)
21 #define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
22 #define BITS_TO_U32S(nr)        DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(uint32_t))
23 
24 #define BIT(nr)                 (1UL << (nr))
25 #define BIT_ULL(nr)             (1ULL << (nr))
26 
27 #define MAKE_64BIT_MASK(shift, length) \
28     (((~0ULL) >> (64 - (length))) << (shift))
29 
30 /**
31  * DOC: Functions operating on arrays of bits
32  *
33  * We provide a set of functions which work on arbitrary-length arrays of
34  * bits. These come in several flavours which vary in what the type of the
35  * underlying storage for the bits is:
36  *
37  * - Bits stored in an array of 'unsigned long': set_bit(), clear_bit(), etc
38  * - Bits stored in an array of 'uint32_t': set_bit32(), clear_bit32(), etc
39  *
40  * Because the 'unsigned long' type has a size which varies between
41  * host systems, the versions using 'uint32_t' are often preferable.
42  * This is particularly the case in a device model where there may
43  * be some guest-visible register view of the bit array.
44  *
45  * We do not currently implement uint32_t versions of find_last_bit(),
46  * find_next_bit(), find_next_zero_bit(), find_first_bit() or
47  * find_first_zero_bit(), because we haven't yet needed them. If you
48  * need them you should implement them similarly to the 'unsigned long'
49  * versions.
50  *
51  * You can declare a bitmap to be used with these functions via the
52  * DECLARE_BITMAP and DECLARE_BITMAP32 macros in bitmap.h.
53  */
54 
55 /**
56  * DOC:  'unsigned long' bit array APIs
57  */
58 
59 #define BIT_MASK(nr)            (1UL << ((nr) % BITS_PER_LONG))
60 #define BIT_WORD(nr)            ((nr) / BITS_PER_LONG)
61 
62 /**
63  * set_bit - Set a bit in memory
64  * @nr: the bit to set
65  * @addr: the address to start counting from
66  */
set_bit(long nr,unsigned long * addr)67 static inline void set_bit(long nr, unsigned long *addr)
68 {
69     unsigned long mask = BIT_MASK(nr);
70     unsigned long *p = addr + BIT_WORD(nr);
71 
72     *p  |= mask;
73 }
74 
75 /**
76  * set_bit_atomic - Set a bit in memory atomically
77  * @nr: the bit to set
78  * @addr: the address to start counting from
79  */
set_bit_atomic(long nr,unsigned long * addr)80 static inline void set_bit_atomic(long nr, unsigned long *addr)
81 {
82     unsigned long mask = BIT_MASK(nr);
83     unsigned long *p = addr + BIT_WORD(nr);
84 
85     qatomic_or(p, mask);
86 }
87 
88 /**
89  * clear_bit - Clears a bit in memory
90  * @nr: Bit to clear
91  * @addr: Address to start counting from
92  */
clear_bit(long nr,unsigned long * addr)93 static inline void clear_bit(long nr, unsigned long *addr)
94 {
95     unsigned long mask = BIT_MASK(nr);
96     unsigned long *p = addr + BIT_WORD(nr);
97 
98     *p &= ~mask;
99 }
100 
101 /**
102  * clear_bit_atomic - Clears a bit in memory atomically
103  * @nr: Bit to clear
104  * @addr: Address to start counting from
105  */
clear_bit_atomic(long nr,unsigned long * addr)106 static inline void clear_bit_atomic(long nr, unsigned long *addr)
107 {
108     unsigned long mask = BIT_MASK(nr);
109     unsigned long *p = addr + BIT_WORD(nr);
110 
111     return qatomic_and(p, ~mask);
112 }
113 
114 /**
115  * change_bit - Toggle a bit in memory
116  * @nr: Bit to change
117  * @addr: Address to start counting from
118  */
change_bit(long nr,unsigned long * addr)119 static inline void change_bit(long nr, unsigned long *addr)
120 {
121     unsigned long mask = BIT_MASK(nr);
122     unsigned long *p = addr + BIT_WORD(nr);
123 
124     *p ^= mask;
125 }
126 
127 /**
128  * test_and_set_bit - Set a bit and return its old value
129  * @nr: Bit to set
130  * @addr: Address to count from
131  */
test_and_set_bit(long nr,unsigned long * addr)132 static inline int test_and_set_bit(long nr, unsigned long *addr)
133 {
134     unsigned long mask = BIT_MASK(nr);
135     unsigned long *p = addr + BIT_WORD(nr);
136     unsigned long old = *p;
137 
138     *p = old | mask;
139     return (old & mask) != 0;
140 }
141 
142 /**
143  * test_and_clear_bit - Clear a bit and return its old value
144  * @nr: Bit to clear
145  * @addr: Address to count from
146  */
test_and_clear_bit(long nr,unsigned long * addr)147 static inline int test_and_clear_bit(long nr, unsigned long *addr)
148 {
149     unsigned long mask = BIT_MASK(nr);
150     unsigned long *p = addr + BIT_WORD(nr);
151     unsigned long old = *p;
152 
153     *p = old & ~mask;
154     return (old & mask) != 0;
155 }
156 
157 /**
158  * test_and_change_bit - Change a bit and return its old value
159  * @nr: Bit to change
160  * @addr: Address to count from
161  */
test_and_change_bit(long nr,unsigned long * addr)162 static inline int test_and_change_bit(long nr, unsigned long *addr)
163 {
164     unsigned long mask = BIT_MASK(nr);
165     unsigned long *p = addr + BIT_WORD(nr);
166     unsigned long old = *p;
167 
168     *p = old ^ mask;
169     return (old & mask) != 0;
170 }
171 
172 /**
173  * test_bit - Determine whether a bit is set
174  * @nr: bit number to test
175  * @addr: Address to start counting from
176  */
test_bit(long nr,const unsigned long * addr)177 static inline int test_bit(long nr, const unsigned long *addr)
178 {
179     return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
180 }
181 
182 /**
183  * find_last_bit - find the last set bit in a memory region
184  * @addr: The address to start the search at
185  * @size: The maximum size to search
186  *
187  * Returns the bit number of the last set bit,
188  * or @size if there is no set bit in the bitmap.
189  */
190 unsigned long find_last_bit(const unsigned long *addr,
191                             unsigned long size);
192 
193 /**
194  * find_next_bit - find the next set bit in a memory region
195  * @addr: The address to base the search on
196  * @offset: The bitnumber to start searching at
197  * @size: The bitmap size in bits
198  *
199  * Returns the bit number of the next set bit,
200  * or @size if there are no further set bits in the bitmap.
201  */
202 unsigned long find_next_bit(const unsigned long *addr,
203                             unsigned long size,
204                             unsigned long offset);
205 
206 /**
207  * find_next_zero_bit - find the next cleared bit in a memory region
208  * @addr: The address to base the search on
209  * @offset: The bitnumber to start searching at
210  * @size: The bitmap size in bits
211  *
212  * Returns the bit number of the next cleared bit,
213  * or @size if there are no further clear bits in the bitmap.
214  */
215 
216 unsigned long find_next_zero_bit(const unsigned long *addr,
217                                  unsigned long size,
218                                  unsigned long offset);
219 
220 /**
221  * find_first_bit - find the first set bit in a memory region
222  * @addr: The address to start the search at
223  * @size: The maximum size to search
224  *
225  * Returns the bit number of the first set bit,
226  * or @size if there is no set bit in the bitmap.
227  */
find_first_bit(const unsigned long * addr,unsigned long size)228 static inline unsigned long find_first_bit(const unsigned long *addr,
229                                            unsigned long size)
230 {
231     unsigned long result, tmp;
232 
233     for (result = 0; result < size; result += BITS_PER_LONG) {
234         tmp = *addr++;
235         if (tmp) {
236             result += ctzl(tmp);
237             return result < size ? result : size;
238         }
239     }
240     /* Not found */
241     return size;
242 }
243 
244 /**
245  * find_first_zero_bit - find the first cleared bit in a memory region
246  * @addr: The address to start the search at
247  * @size: The maximum size to search
248  *
249  * Returns the bit number of the first cleared bit,
250  * or @size if there is no clear bit in the bitmap.
251  */
find_first_zero_bit(const unsigned long * addr,unsigned long size)252 static inline unsigned long find_first_zero_bit(const unsigned long *addr,
253                                                 unsigned long size)
254 {
255     return find_next_zero_bit(addr, size, 0);
256 }
257 
258 /**
259  * DOC:  'uint32_t' bit array APIs
260  */
261 
262 #define BIT32_MASK(nr)            (1UL << ((nr) % 32))
263 #define BIT32_WORD(nr)            ((nr) / 32)
264 
265 /**
266  * set_bit32 - Set a bit in memory
267  * @nr: the bit to set
268  * @addr: the address to start counting from
269  */
set_bit32(long nr,uint32_t * addr)270 static inline void set_bit32(long nr, uint32_t *addr)
271 {
272     uint32_t mask = BIT32_MASK(nr);
273     uint32_t *p = addr + BIT32_WORD(nr);
274 
275     *p  |= mask;
276 }
277 
278 /**
279  * set_bit32_atomic - Set a bit in memory atomically
280  * @nr: the bit to set
281  * @addr: the address to start counting from
282  */
set_bit32_atomic(long nr,uint32_t * addr)283 static inline void set_bit32_atomic(long nr, uint32_t *addr)
284 {
285     uint32_t mask = BIT32_MASK(nr);
286     uint32_t *p = addr + BIT32_WORD(nr);
287 
288     qatomic_or(p, mask);
289 }
290 
291 /**
292  * clear_bit32 - Clears a bit in memory
293  * @nr: Bit to clear
294  * @addr: Address to start counting from
295  */
clear_bit32(long nr,uint32_t * addr)296 static inline void clear_bit32(long nr, uint32_t *addr)
297 {
298     uint32_t mask = BIT32_MASK(nr);
299     uint32_t *p = addr + BIT32_WORD(nr);
300 
301     *p &= ~mask;
302 }
303 
304 /**
305  * clear_bit32_atomic - Clears a bit in memory atomically
306  * @nr: Bit to clear
307  * @addr: Address to start counting from
308  */
clear_bit32_atomic(long nr,uint32_t * addr)309 static inline void clear_bit32_atomic(long nr, uint32_t *addr)
310 {
311     uint32_t mask = BIT32_MASK(nr);
312     uint32_t *p = addr + BIT32_WORD(nr);
313 
314     return qatomic_and(p, ~mask);
315 }
316 
317 /**
318  * change_bit32 - Toggle a bit in memory
319  * @nr: Bit to change
320  * @addr: Address to start counting from
321  */
change_bit32(long nr,uint32_t * addr)322 static inline void change_bit32(long nr, uint32_t *addr)
323 {
324     uint32_t mask = BIT32_MASK(nr);
325     uint32_t *p = addr + BIT32_WORD(nr);
326 
327     *p ^= mask;
328 }
329 
330 /**
331  * test_and_set_bit32 - Set a bit and return its old value
332  * @nr: Bit to set
333  * @addr: Address to count from
334  */
test_and_set_bit32(long nr,uint32_t * addr)335 static inline int test_and_set_bit32(long nr, uint32_t *addr)
336 {
337     uint32_t mask = BIT32_MASK(nr);
338     uint32_t *p = addr + BIT32_WORD(nr);
339     uint32_t old = *p;
340 
341     *p = old | mask;
342     return (old & mask) != 0;
343 }
344 
345 /**
346  * test_and_clear_bit32 - Clear a bit and return its old value
347  * @nr: Bit to clear
348  * @addr: Address to count from
349  */
test_and_clear_bit32(long nr,uint32_t * addr)350 static inline int test_and_clear_bit32(long nr, uint32_t *addr)
351 {
352     uint32_t mask = BIT32_MASK(nr);
353     uint32_t *p = addr + BIT32_WORD(nr);
354     uint32_t old = *p;
355 
356     *p = old & ~mask;
357     return (old & mask) != 0;
358 }
359 
360 /**
361  * test_and_change_bit32 - Change a bit and return its old value
362  * @nr: Bit to change
363  * @addr: Address to count from
364  */
test_and_change_bit32(long nr,uint32_t * addr)365 static inline int test_and_change_bit32(long nr, uint32_t *addr)
366 {
367     uint32_t mask = BIT32_MASK(nr);
368     uint32_t *p = addr + BIT32_WORD(nr);
369     uint32_t old = *p;
370 
371     *p = old ^ mask;
372     return (old & mask) != 0;
373 }
374 
375 /**
376  * test_bit32 - Determine whether a bit is set
377  * @nr: bit number to test
378  * @addr: Address to start counting from
379  */
test_bit32(long nr,const uint32_t * addr)380 static inline int test_bit32(long nr, const uint32_t *addr)
381 {
382     return 1U & (addr[BIT32_WORD(nr)] >> (nr & 31));
383 }
384 
385 /**
386  * DOC: Miscellaneous bit operations on single values
387  *
388  * These functions are a collection of useful operations
389  * (rotations, bit extract, bit deposit, etc) on single
390  * integer values.
391  */
392 
393 /**
394  * rol8 - rotate an 8-bit value left
395  * @word: value to rotate
396  * @shift: bits to roll
397  */
rol8(uint8_t word,unsigned int shift)398 static inline uint8_t rol8(uint8_t word, unsigned int shift)
399 {
400     return (word << (shift & 7)) | (word >> (-shift & 7));
401 }
402 
403 /**
404  * ror8 - rotate an 8-bit value right
405  * @word: value to rotate
406  * @shift: bits to roll
407  */
ror8(uint8_t word,unsigned int shift)408 static inline uint8_t ror8(uint8_t word, unsigned int shift)
409 {
410     return (word >> (shift & 7)) | (word << (-shift & 7));
411 }
412 
413 /**
414  * rol16 - rotate a 16-bit value left
415  * @word: value to rotate
416  * @shift: bits to roll
417  */
rol16(uint16_t word,unsigned int shift)418 static inline uint16_t rol16(uint16_t word, unsigned int shift)
419 {
420     return (word << (shift & 15)) | (word >> (-shift & 15));
421 }
422 
423 /**
424  * ror16 - rotate a 16-bit value right
425  * @word: value to rotate
426  * @shift: bits to roll
427  */
ror16(uint16_t word,unsigned int shift)428 static inline uint16_t ror16(uint16_t word, unsigned int shift)
429 {
430     return (word >> (shift & 15)) | (word << (-shift & 15));
431 }
432 
433 /**
434  * rol32 - rotate a 32-bit value left
435  * @word: value to rotate
436  * @shift: bits to roll
437  */
rol32(uint32_t word,unsigned int shift)438 static inline uint32_t rol32(uint32_t word, unsigned int shift)
439 {
440     return (word << (shift & 31)) | (word >> (-shift & 31));
441 }
442 
443 /**
444  * ror32 - rotate a 32-bit value right
445  * @word: value to rotate
446  * @shift: bits to roll
447  */
ror32(uint32_t word,unsigned int shift)448 static inline uint32_t ror32(uint32_t word, unsigned int shift)
449 {
450     return (word >> (shift & 31)) | (word << (-shift & 31));
451 }
452 
453 /**
454  * rol64 - rotate a 64-bit value left
455  * @word: value to rotate
456  * @shift: bits to roll
457  */
rol64(uint64_t word,unsigned int shift)458 static inline uint64_t rol64(uint64_t word, unsigned int shift)
459 {
460     return (word << (shift & 63)) | (word >> (-shift & 63));
461 }
462 
463 /**
464  * ror64 - rotate a 64-bit value right
465  * @word: value to rotate
466  * @shift: bits to roll
467  */
ror64(uint64_t word,unsigned int shift)468 static inline uint64_t ror64(uint64_t word, unsigned int shift)
469 {
470     return (word >> (shift & 63)) | (word << (-shift & 63));
471 }
472 
473 /**
474  * hswap32 - swap 16-bit halfwords within a 32-bit value
475  * @h: value to swap
476  */
hswap32(uint32_t h)477 static inline uint32_t hswap32(uint32_t h)
478 {
479     return rol32(h, 16);
480 }
481 
482 /**
483  * hswap64 - swap 16-bit halfwords within a 64-bit value
484  * @h: value to swap
485  */
hswap64(uint64_t h)486 static inline uint64_t hswap64(uint64_t h)
487 {
488     uint64_t m = 0x0000ffff0000ffffull;
489     h = rol64(h, 32);
490     return ((h & m) << 16) | ((h >> 16) & m);
491 }
492 
493 /**
494  * wswap64 - swap 32-bit words within a 64-bit value
495  * @h: value to swap
496  */
wswap64(uint64_t h)497 static inline uint64_t wswap64(uint64_t h)
498 {
499     return rol64(h, 32);
500 }
501 
502 /**
503  * extract32:
504  * @value: the value to extract the bit field from
505  * @start: the lowest bit in the bit field (numbered from 0)
506  * @length: the length of the bit field
507  *
508  * Extract from the 32 bit input @value the bit field specified by the
509  * @start and @length parameters, and return it. The bit field must
510  * lie entirely within the 32 bit word. It is valid to request that
511  * all 32 bits are returned (ie @length 32 and @start 0).
512  *
513  * Returns: the value of the bit field extracted from the input value.
514  */
extract32(uint32_t value,int start,int length)515 static inline uint32_t extract32(uint32_t value, int start, int length)
516 {
517     assert(start >= 0 && length > 0 && length <= 32 - start);
518     return (value >> start) & (~0U >> (32 - length));
519 }
520 
521 /**
522  * extract8:
523  * @value: the value to extract the bit field from
524  * @start: the lowest bit in the bit field (numbered from 0)
525  * @length: the length of the bit field
526  *
527  * Extract from the 8 bit input @value the bit field specified by the
528  * @start and @length parameters, and return it. The bit field must
529  * lie entirely within the 8 bit word. It is valid to request that
530  * all 8 bits are returned (ie @length 8 and @start 0).
531  *
532  * Returns: the value of the bit field extracted from the input value.
533  */
extract8(uint8_t value,int start,int length)534 static inline uint8_t extract8(uint8_t value, int start, int length)
535 {
536     assert(start >= 0 && length > 0 && length <= 8 - start);
537     return extract32(value, start, length);
538 }
539 
540 /**
541  * extract16:
542  * @value: the value to extract the bit field from
543  * @start: the lowest bit in the bit field (numbered from 0)
544  * @length: the length of the bit field
545  *
546  * Extract from the 16 bit input @value the bit field specified by the
547  * @start and @length parameters, and return it. The bit field must
548  * lie entirely within the 16 bit word. It is valid to request that
549  * all 16 bits are returned (ie @length 16 and @start 0).
550  *
551  * Returns: the value of the bit field extracted from the input value.
552  */
extract16(uint16_t value,int start,int length)553 static inline uint16_t extract16(uint16_t value, int start, int length)
554 {
555     assert(start >= 0 && length > 0 && length <= 16 - start);
556     return extract32(value, start, length);
557 }
558 
559 /**
560  * extract64:
561  * @value: the value to extract the bit field from
562  * @start: the lowest bit in the bit field (numbered from 0)
563  * @length: the length of the bit field
564  *
565  * Extract from the 64 bit input @value the bit field specified by the
566  * @start and @length parameters, and return it. The bit field must
567  * lie entirely within the 64 bit word. It is valid to request that
568  * all 64 bits are returned (ie @length 64 and @start 0).
569  *
570  * Returns: the value of the bit field extracted from the input value.
571  */
extract64(uint64_t value,int start,int length)572 static inline uint64_t extract64(uint64_t value, int start, int length)
573 {
574     assert(start >= 0 && length > 0 && length <= 64 - start);
575     return (value >> start) & (~0ULL >> (64 - length));
576 }
577 
578 /**
579  * sextract32:
580  * @value: the value to extract the bit field from
581  * @start: the lowest bit in the bit field (numbered from 0)
582  * @length: the length of the bit field
583  *
584  * Extract from the 32 bit input @value the bit field specified by the
585  * @start and @length parameters, and return it, sign extended to
586  * an int32_t (ie with the most significant bit of the field propagated
587  * to all the upper bits of the return value). The bit field must lie
588  * entirely within the 32 bit word. It is valid to request that
589  * all 32 bits are returned (ie @length 32 and @start 0).
590  *
591  * Returns: the sign extended value of the bit field extracted from the
592  * input value.
593  */
sextract32(uint32_t value,int start,int length)594 static inline int32_t sextract32(uint32_t value, int start, int length)
595 {
596     assert(start >= 0 && length > 0 && length <= 32 - start);
597     /* Note that this implementation relies on right shift of signed
598      * integers being an arithmetic shift.
599      */
600     return ((int32_t)(value << (32 - length - start))) >> (32 - length);
601 }
602 
603 /**
604  * sextract64:
605  * @value: the value to extract the bit field from
606  * @start: the lowest bit in the bit field (numbered from 0)
607  * @length: the length of the bit field
608  *
609  * Extract from the 64 bit input @value the bit field specified by the
610  * @start and @length parameters, and return it, sign extended to
611  * an int64_t (ie with the most significant bit of the field propagated
612  * to all the upper bits of the return value). The bit field must lie
613  * entirely within the 64 bit word. It is valid to request that
614  * all 64 bits are returned (ie @length 64 and @start 0).
615  *
616  * Returns: the sign extended value of the bit field extracted from the
617  * input value.
618  */
sextract64(uint64_t value,int start,int length)619 static inline int64_t sextract64(uint64_t value, int start, int length)
620 {
621     assert(start >= 0 && length > 0 && length <= 64 - start);
622     /* Note that this implementation relies on right shift of signed
623      * integers being an arithmetic shift.
624      */
625     return ((int64_t)(value << (64 - length - start))) >> (64 - length);
626 }
627 
628 /**
629  * deposit32:
630  * @value: initial value to insert bit field into
631  * @start: the lowest bit in the bit field (numbered from 0)
632  * @length: the length of the bit field
633  * @fieldval: the value to insert into the bit field
634  *
635  * Deposit @fieldval into the 32 bit @value at the bit field specified
636  * by the @start and @length parameters, and return the modified
637  * @value. Bits of @value outside the bit field are not modified.
638  * Bits of @fieldval above the least significant @length bits are
639  * ignored. The bit field must lie entirely within the 32 bit word.
640  * It is valid to request that all 32 bits are modified (ie @length
641  * 32 and @start 0).
642  *
643  * Returns: the modified @value.
644  */
deposit32(uint32_t value,int start,int length,uint32_t fieldval)645 static inline uint32_t deposit32(uint32_t value, int start, int length,
646                                  uint32_t fieldval)
647 {
648     uint32_t mask;
649     assert(start >= 0 && length > 0 && length <= 32 - start);
650     mask = (~0U >> (32 - length)) << start;
651     return (value & ~mask) | ((fieldval << start) & mask);
652 }
653 
654 /**
655  * deposit64:
656  * @value: initial value to insert bit field into
657  * @start: the lowest bit in the bit field (numbered from 0)
658  * @length: the length of the bit field
659  * @fieldval: the value to insert into the bit field
660  *
661  * Deposit @fieldval into the 64 bit @value at the bit field specified
662  * by the @start and @length parameters, and return the modified
663  * @value. Bits of @value outside the bit field are not modified.
664  * Bits of @fieldval above the least significant @length bits are
665  * ignored. The bit field must lie entirely within the 64 bit word.
666  * It is valid to request that all 64 bits are modified (ie @length
667  * 64 and @start 0).
668  *
669  * Returns: the modified @value.
670  */
deposit64(uint64_t value,int start,int length,uint64_t fieldval)671 static inline uint64_t deposit64(uint64_t value, int start, int length,
672                                  uint64_t fieldval)
673 {
674     uint64_t mask;
675     assert(start >= 0 && length > 0 && length <= 64 - start);
676     mask = (~0ULL >> (64 - length)) << start;
677     return (value & ~mask) | ((fieldval << start) & mask);
678 }
679 
680 /**
681  * half_shuffle32:
682  * @x: 32-bit value (of which only the bottom 16 bits are of interest)
683  *
684  * Given an input value::
685  *
686  *   xxxx xxxx xxxx xxxx ABCD EFGH IJKL MNOP
687  *
688  * return the value where the bottom 16 bits are spread out into
689  * the odd bits in the word, and the even bits are zeroed::
690  *
691  *   0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N 0O0P
692  *
693  * Any bits set in the top half of the input are ignored.
694  *
695  * Returns: the shuffled bits.
696  */
half_shuffle32(uint32_t x)697 static inline uint32_t half_shuffle32(uint32_t x)
698 {
699     /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
700      * It ignores any bits set in the top half of the input.
701      */
702     x = ((x & 0xFF00) << 8) | (x & 0x00FF);
703     x = ((x << 4) | x) & 0x0F0F0F0F;
704     x = ((x << 2) | x) & 0x33333333;
705     x = ((x << 1) | x) & 0x55555555;
706     return x;
707 }
708 
709 /**
710  * half_shuffle64:
711  * @x: 64-bit value (of which only the bottom 32 bits are of interest)
712  *
713  * Given an input value::
714  *
715  *   xxxx xxxx xxxx .... xxxx xxxx ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
716  *
717  * return the value where the bottom 32 bits are spread out into
718  * the odd bits in the word, and the even bits are zeroed::
719  *
720  *   0A0B 0C0D 0E0F 0G0H 0I0J 0K0L 0M0N .... 0U0V 0W0X 0Y0Z 0a0b 0c0d 0e0f
721  *
722  * Any bits set in the top half of the input are ignored.
723  *
724  * Returns: the shuffled bits.
725  */
half_shuffle64(uint64_t x)726 static inline uint64_t half_shuffle64(uint64_t x)
727 {
728     /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
729      * It ignores any bits set in the top half of the input.
730      */
731     x = ((x & 0xFFFF0000ULL) << 16) | (x & 0xFFFF);
732     x = ((x << 8) | x) & 0x00FF00FF00FF00FFULL;
733     x = ((x << 4) | x) & 0x0F0F0F0F0F0F0F0FULL;
734     x = ((x << 2) | x) & 0x3333333333333333ULL;
735     x = ((x << 1) | x) & 0x5555555555555555ULL;
736     return x;
737 }
738 
739 /**
740  * half_unshuffle32:
741  * @x: 32-bit value (of which only the odd bits are of interest)
742  *
743  * Given an input value::
744  *
745  *   xAxB xCxD xExF xGxH xIxJ xKxL xMxN xOxP
746  *
747  * return the value where all the odd bits are compressed down
748  * into the low half of the word, and the high half is zeroed::
749  *
750  *   0000 0000 0000 0000 ABCD EFGH IJKL MNOP
751  *
752  * Any even bits set in the input are ignored.
753  *
754  * Returns: the unshuffled bits.
755  */
half_unshuffle32(uint32_t x)756 static inline uint32_t half_unshuffle32(uint32_t x)
757 {
758     /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
759      * where it is called an inverse half shuffle.
760      */
761     x &= 0x55555555;
762     x = ((x >> 1) | x) & 0x33333333;
763     x = ((x >> 2) | x) & 0x0F0F0F0F;
764     x = ((x >> 4) | x) & 0x00FF00FF;
765     x = ((x >> 8) | x) & 0x0000FFFF;
766     return x;
767 }
768 
769 /**
770  * half_unshuffle64:
771  * @x: 64-bit value (of which only the odd bits are of interest)
772  *
773  * Given an input value::
774  *
775  *   xAxB xCxD xExF xGxH xIxJ xKxL xMxN .... xUxV xWxX xYxZ xaxb xcxd xexf
776  *
777  * return the value where all the odd bits are compressed down
778  * into the low half of the word, and the high half is zeroed::
779  *
780  *   0000 0000 0000 .... 0000 0000 ABCD EFGH IJKL MNOP QRST UVWX YZab cdef
781  *
782  * Any even bits set in the input are ignored.
783  *
784  * Returns: the unshuffled bits.
785  */
half_unshuffle64(uint64_t x)786 static inline uint64_t half_unshuffle64(uint64_t x)
787 {
788     /* This algorithm is from _Hacker's Delight_ section 7-2 "Shuffling Bits".
789      * where it is called an inverse half shuffle.
790      */
791     x &= 0x5555555555555555ULL;
792     x = ((x >> 1) | x) & 0x3333333333333333ULL;
793     x = ((x >> 2) | x) & 0x0F0F0F0F0F0F0F0FULL;
794     x = ((x >> 4) | x) & 0x00FF00FF00FF00FFULL;
795     x = ((x >> 8) | x) & 0x0000FFFF0000FFFFULL;
796     x = ((x >> 16) | x) & 0x00000000FFFFFFFFULL;
797     return x;
798 }
799 
800 #endif
801