source: mainline/uspace/lib/softint/generic/bits.c@ 1b20da0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1b20da0 was 1b20da0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on non-empty lines, in certain file types.

Command used: tools/srepl '\([^[:space:]]\)\s\+$' '\1' -- *.c *.h *.py *.sh *.s *.S *.ag

  • 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 if (a == 0) {
86 return 0;
87 }
88
89 return 1 + __ctzdi2(a);
90}
91
92/** Compute number of set bits in a number. */
93int __popcountsi2(int a)
94{
95 int bits = 0;
96 for (unsigned int i = 0; i < sizeof(a) * 8; i++) {
97 if (((a >> i) & 1) != 0) {
98 bits++;
99 }
100 }
101 return bits;
102}
103
104/** Compute number of set bits in a number. */
105int __popcountdi2(long a)
106{
107 int bits = 0;
108 for (unsigned int i = 0; i < sizeof(a) * 8; i++) {
109 if (((a >> i) & 1) != 0) {
110 bits++;
111 }
112 }
113 return bits;
114}
115
116/** @}
117 */
Note: See TracBrowser for help on using the repository browser.