source: mainline/uspace/lib/softint/generic/bits.c@ 6ff23ff

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6ff23ff was 3bacee1, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Make ccheck-fix again and commit more good files.

  • Property mode set to 100644
File size: 2.7 KB
Line 
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
29/** @addtogroup softint
30 * @{
31 */
32
33#include <bits.h>
34
35/** Compute number of trailing 0-bits in a number. */
36int __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. */
50int __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. */
64int __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 */
84int __ffsdi2(long a)
85{
86 if (a == 0) {
87 return 0;
88 }
89
90 return 1 + __ctzdi2(a);
91}
92
93/** Compute number of set bits in a number. */
94int __popcountsi2(int a)
95{
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 }
102 return bits;
103}
104
105/** Compute number of set bits in a number. */
106int __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 }
114 return bits;
115}
116
117/** @}
118 */
Note: See TracBrowser for help on using the repository browser.