about summary refs log tree commit diff
path: root/lib/util/wordaccess_gcc3_be.h
blob: 6f5d86fc90566c3bc1d052e1475e74daadf9154b (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
/*=============================================================================
  This file is the part of wordaccess.h for use under these
  conditions:

  * GCC (>=3.4), GLIBC
  * Big-Endian machines
  
  __builtin_clz is available on GCC 3.4 and above
     
  Note that the clz scheme does not work and requires adjustment
  if long type does not make use of all bits for data storage.
  
  This is unlikely.  According to GNU MP (http://www.swox.com/gmp/),
  in rare cases such as Cray, there are smaller data types that take up
  the same space as long, but leave the higher bits silent.  Currently,
  there are no known such cases for data type long.
*===========================================================================*/

typedef unsigned long int wordint;
typedef unsigned char wordintBytes[sizeof(wordint)];

static __inline__ wordint
bytesToWordint(wordintBytes bytes) {
    return *((wordint *)bytes);
}



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



static __inline__ unsigned int
wordintClz(wordint const x) {
    return (x==0 ? sizeof(wordint)*8 : __builtin_clzl(x));
}