Changeset 18dc93c in mainline for uspace/lib/softint/generic/bits.c


Ignore:
Timestamp:
2013-11-19T08:30:03Z (10 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5f9768bb
Parents:
879d7745
Message:

More bit counting functions (libsoftint)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/softint/generic/bits.c

    r879d7745 r18dc93c  
    3333#include <bits.h>
    3434
     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
    3592/** Compute number of set bits in a number. */
    3693int __popcountsi2(int a)
Note: See TracChangeset for help on using the changeset viewer.