summaryrefslogtreecommitdiff
path: root/libraries/ghc-prim/cbits/clz.c
blob: b0637b5dfe02669f2402cfa53255614c3fe9b0f2 (plain)
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
#include "MachDeps.h"
#include "Rts.h"
#include <stdint.h>

// Fall-back implementations for count-leading-zeros primop
//
// __builtin_clz*() is supported by GCC and Clang

#if SIZEOF_UNSIGNED_INT == 4
StgWord
hs_clz8(StgWord x)
{
  return (uint8_t)x ? __builtin_clz((uint8_t)x)-24 : 8;
}

StgWord
hs_clz16(StgWord x)
{
  return (uint16_t)x ? __builtin_clz((uint16_t)x)-16 : 16;
}

StgWord
hs_clz32(StgWord x)
{
  return (uint32_t)x ? __builtin_clz((uint32_t)x) : 32;
}
#else
# error no suitable __builtin_clz() found
#endif

StgWord
hs_clz64(StgWord64 x)
{
#if SIZEOF_UNSIGNED_LONG == 8
  return x ? __builtin_clzl(x) : 64;
#elif SIZEOF_UNSIGNED_LONG_LONG == 8
  return x ? __builtin_clzll(x) : 64;
#else
# error no suitable __builtin_clz() found
#endif
}