about summary refs log tree commit diff
path: root/lib/util/wordaccess_generic.h
blob: 7f27ef74894efca9811797a85a01b4d129c9a3fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*=============================================================================

  This file is the part of wordaccess.h for use under these
  conditions:

  * Compilers other than GCC
  * GCC before version 3.4
  * c libraries other than Glibc
  * Specified by the user with WORDACCESS_GENERIC
=============================================================================*/

typedef uint32_t wordint;
typedef unsigned char wordintBytes[sizeof(wordint)];
    
static __inline__ wordint
bytesToWordint(wordintBytes const bytes) {
    wordint retval;
    unsigned int i;

    /* Note that 'bytes' is a pointer, due to C array degeneration.
       That means sizeof(bytes) isn't what you think it is.
    */
    
    for (i = 1, retval = bytes[0]; i < sizeof(wordint); ++i) {
        retval = (retval << 8) + bytes[i];
    }
    return retval;
}



static __inline__ void
wordintToBytes(wordintBytes * const bytesP,
               wordint        const wordInt) {

    wordint buffer;
    int i;

    for (i = sizeof(*bytesP)-1, buffer = wordInt; i >= 0; --i) {
        (*bytesP)[i] = buffer & 0xFF;
        buffer >>= 8;
    }
}
    
static unsigned char const clz8[256]= {
    8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
};



static __inline__ unsigned int
clz16(wordint const x) {
    if (x >> 8  != 0)
        return clz8[x >> 8];
    else
        return clz8[x] + 8;
}



static __inline__  unsigned int
clz32(wordint const x) {
    if (x >> 16  != 0)
        return clz16(x >> 16);
    else
        return clz16(x) +16;
}



static __inline__  unsigned int
wordintClz(wordint const x) {
    return clz32(x);
}