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