summaryrefslogtreecommitdiff
path: root/libraries/ghc-prim/cbits/ctz.c
blob: fc98716968e769ba32b2e2b33bae53f73667b10a (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "MachDeps.h"
#include "Rts.h"
#include <stdint.h>

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

#if SIZEOF_UNSIGNED_INT == 4
StgWord
hs_ctz8(StgWord x)
{
  return (uint8_t)x ? __builtin_ctz(x) : 8;
}

StgWord
hs_ctz16(StgWord x)
{
  return (uint16_t)x ? __builtin_ctz(x) : 16;
}

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

StgWord
hs_ctz64(StgWord64 x)
{
#if defined(__GNUC__) && (defined(i386_HOST_ARCH) || defined(powerpc_HOST_ARCH))
  /* On Linux/i386, the 64bit `__builtin_ctzll()` instrinsic doesn't
     get inlined by GCC but rather a short `__ctzdi2` runtime function
     is inserted when needed into compiled object files.

     This workaround forces GCC on 32bit x86 to express `hs_ctz64` in
     terms of the 32bit `__builtin_ctz()` (this is no loss, as there's no
     64bit BSF instruction on i686 anyway) and thus avoid the problematic
     out-of-line runtime function.
  */

  if (!x) return 64;

  return ((uint32_t)x ? __builtin_ctz((uint32_t)x)
                      : (__builtin_ctz(x >> 32) + 32));

#elif SIZEOF_UNSIGNED_LONG == 8
  return x ? __builtin_ctzl(x) : 64;
#elif SIZEOF_UNSIGNED_LONG_LONG == 8
  return x ? __builtin_ctzll(x) : 64;
#else
# error no suitable __builtin_ctz() found
#endif
}