switch back to inline assembly in compression::instructions
[folly.git] / folly / experimental / Instructions.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <glog/logging.h>
20
21 #include <folly/CpuId.h>
22 #include <folly/Portability.h>
23 #include <folly/portability/Builtins.h>
24
25 #if defined(__GNUC__) || defined(__clang__)
26 // For compilers supporting AT&T assembly syntax.
27 #define FOLLY_INSTRUCTIONS_SUPPORTED 1
28 #else
29 #define FOLLY_INSTRUCTIONS_SUPPORTED 0
30 #endif
31
32 namespace folly { namespace compression { namespace instructions {
33
34 // NOTE: It's recommended to compile EF coding with -msse4.2, starting
35 // with Nehalem, Intel CPUs support POPCNT instruction and gcc will emit
36 // it for __builtin_popcountll intrinsic.
37 // But we provide an alternative way for the client code: it can switch to
38 // the appropriate version of EliasFanoReader<> in realtime (client should
39 // implement this switching logic itself) by specifying instruction set to
40 // use explicitly.
41
42 struct Default {
43   static bool supported(const folly::CpuId& /* cpuId */ = {}) {
44     return true;
45   }
46   static FOLLY_ALWAYS_INLINE uint64_t popcount(uint64_t value) {
47     return __builtin_popcountll(value);
48   }
49   static FOLLY_ALWAYS_INLINE int ctz(uint64_t value) {
50     DCHECK_GT(value, 0);
51     return __builtin_ctzll(value);
52   }
53   static FOLLY_ALWAYS_INLINE int clz(uint64_t value) {
54     DCHECK_GT(value, 0);
55     return __builtin_clzll(value);
56   }
57   static FOLLY_ALWAYS_INLINE uint64_t blsr(uint64_t value) {
58     return value & (value - 1);
59   }
60 };
61
62 #if FOLLY_INSTRUCTIONS_SUPPORTED
63
64 struct Nehalem : public Default {
65   static bool supported(const folly::CpuId& cpuId = {}) {
66     return cpuId.popcnt();
67   }
68
69   static FOLLY_ALWAYS_INLINE uint64_t popcount(uint64_t value) {
70     // POPCNT is supported starting with Intel Nehalem, AMD K10.
71     uint64_t result;
72     asm ("popcntq %1, %0" : "=r" (result) : "r" (value));
73     return result;
74   }
75 };
76
77 struct Haswell : public Nehalem {
78   static bool supported(const folly::CpuId& cpuId = {}) {
79     return Nehalem::supported(cpuId) && cpuId.bmi1();
80   }
81
82   static FOLLY_ALWAYS_INLINE uint64_t blsr(uint64_t value) {
83     // BMI1 is supported starting with Intel Haswell, AMD Piledriver.
84     // BLSR combines two instuctions into one and reduces register pressure.
85     uint64_t result;
86     asm ("blsrq %1, %0" : "=r" (result) : "r" (value));
87     return result;
88   }
89 };
90
91 #else // FOLLY_INSTRUCTIONS_SUPPORTED
92
93 struct Nehalem : public Default {};
94 struct Haswell : public Nehalem {};
95
96 #endif // FOLLY_INSTRUCTIONS_SUPPORTED
97
98 }}} // namespaces