[a269d05] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[879d7745] | 29 | /** @addtogroup softint
|
---|
[a269d05] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[879d7745] | 33 | #include <bits.h>
|
---|
[a269d05] | 34 |
|
---|
[18dc93c] | 35 | /** Compute number of trailing 0-bits in a number. */
|
---|
| 36 | int __ctzdi2(long a)
|
---|
| 37 | {
|
---|
| 38 | unsigned int bits = 0;
|
---|
| 39 | while (((a >> bits) & 1) == 0) {
|
---|
| 40 | bits++;
|
---|
| 41 | if (bits >= sizeof(a) * 8) {
|
---|
| 42 | break;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | return bits;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /** Compute number of trailing 0-bits in a number. */
|
---|
| 50 | int __ctzsi2(int a)
|
---|
| 51 | {
|
---|
| 52 | unsigned int bits = 0;
|
---|
| 53 | while (((a >> bits) & 1) == 0) {
|
---|
| 54 | bits++;
|
---|
| 55 | if (bits >= sizeof(a) * 8) {
|
---|
| 56 | break;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | return bits;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /** Compute number of leading 0-bits in a number. */
|
---|
| 64 | int __clzdi2(long a)
|
---|
| 65 | {
|
---|
| 66 | int index = sizeof(a) * 8 - 1;
|
---|
| 67 | int bits = 0;
|
---|
| 68 | while (index >= 0) {
|
---|
| 69 | if (((a >> index) & 1) == 0) {
|
---|
| 70 | bits++;
|
---|
| 71 | } else {
|
---|
| 72 | break;
|
---|
| 73 | }
|
---|
| 74 | index--;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return bits;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /** Compute index of the first 1-bit in a number increased by one.
|
---|
| 81 | *
|
---|
| 82 | * If the number is zero, zero is returned.
|
---|
| 83 | */
|
---|
[3bacee1] | 84 | int __ffsdi2(long a)
|
---|
| 85 | {
|
---|
[18dc93c] | 86 | if (a == 0) {
|
---|
| 87 | return 0;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | return 1 + __ctzdi2(a);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[879d7745] | 93 | /** Compute number of set bits in a number. */
|
---|
[2923c7b] | 94 | int __popcountsi2(int a)
|
---|
[a269d05] | 95 | {
|
---|
[2923c7b] | 96 | int bits = 0;
|
---|
| 97 | for (unsigned int i = 0; i < sizeof(a) * 8; i++) {
|
---|
| 98 | if (((a >> i) & 1) != 0) {
|
---|
| 99 | bits++;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
[1b20da0] | 102 | return bits;
|
---|
[a269d05] | 103 | }
|
---|
| 104 |
|
---|
[879d7745] | 105 | /** Compute number of set bits in a number. */
|
---|
[860a7bb] | 106 | int __popcountdi2(long a)
|
---|
| 107 | {
|
---|
| 108 | int bits = 0;
|
---|
| 109 | for (unsigned int i = 0; i < sizeof(a) * 8; i++) {
|
---|
| 110 | if (((a >> i) & 1) != 0) {
|
---|
| 111 | bits++;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
[1b20da0] | 114 | return bits;
|
---|
[860a7bb] | 115 | }
|
---|
[a269d05] | 116 |
|
---|
| 117 | /** @}
|
---|
| 118 | */
|
---|