~sheosi/helenos/lua

« back to all changes in this revision

Viewing changes to uspace/lib/softint/generic/bits.c

  • Committer: Sergio Tortosa (sheosi)
  • Date: 2013-12-22 14:13:23 UTC
  • mfrom: (2032.1.12 mainline)
  • Revision ID: sertorbe@gmail.com-20131222141323-gbiqm4j2w9sbjty5
MergedĀ mainline

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
 */
28
28
 
29
 
/** @addtogroup libc
 
29
/** @addtogroup softint
30
30
 * @{
31
31
 */
32
32
 
33
 
#include <bitops.h>
34
 
 
 
33
#include <bits.h>
 
34
 
 
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
 */
 
84
int __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. */
35
93
int __popcountsi2(int a)
36
94
{
37
95
        int bits = 0;
43
101
        return bits;                                                                    
44
102
}
45
103
 
 
104
/** Compute number of set bits in a number. */
 
105
int __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
}
46
115
 
47
116
/** @}
48
117
 */