6a1d6ed653be3819c6664e6c4f6fbb3cc3310475
[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 #include <immintrin.h>
21
22 #include <folly/CpuId.h>
23 #include <folly/portability/Builtins.h>
24
25 namespace folly { namespace compression { namespace instructions {
26
27 // NOTE: It's recommended to compile EF coding with -msse4.2, starting
28 // with Nehalem, Intel CPUs support POPCNT instruction and gcc will emit
29 // it for __builtin_popcountll intrinsic.
30 // But we provide an alternative way for the client code: it can switch to
31 // the appropriate version of EliasFanoReader<> in realtime (client should
32 // implement this switching logic itself) by specifying instruction set to
33 // use explicitly.
34
35 struct Default {
36   static bool supported(const folly::CpuId& /* cpuId */ = {}) { return true; }
37   static inline uint64_t popcount(uint64_t value) {
38     return __builtin_popcountll(value);
39   }
40   static inline int ctz(uint64_t value) {
41     DCHECK_GT(value, 0);
42     return __builtin_ctzll(value);
43   }
44   static inline int clz(uint64_t value) {
45     DCHECK_GT(value, 0);
46     return __builtin_clzll(value);
47   }
48   static inline uint64_t blsr(uint64_t value) {
49     return value & (value - 1);
50   }
51 };
52
53 struct Nehalem : public Default {
54   static bool supported(const folly::CpuId& cpuId = {}) {
55     return cpuId.popcnt();
56   }
57   static inline uint64_t popcount(uint64_t value)
58       FOLLY_TARGET_ATTRIBUTE("popcnt") {
59     // POPCNT is supported starting with Intel Nehalem, AMD K10.
60 #if defined(__GNUC__) && !__GNUC_PREREQ(4, 9)
61     // GCC 4.8 doesn't support the intrinsics.
62     uint64_t result;
63     asm ("popcntq %1, %0" : "=r" (result) : "r" (value));
64     return result;
65 #else
66     return _mm_popcnt_u64(value);
67 #endif
68   }
69 };
70
71 struct Haswell : public Nehalem {
72   static bool supported(const folly::CpuId& cpuId = {}) {
73     return Nehalem::supported(cpuId) && cpuId.bmi1();
74   }
75   static inline uint64_t blsr(uint64_t value) FOLLY_TARGET_ATTRIBUTE("bmi") {
76     // BMI1 is supported starting with Intel Haswell, AMD Piledriver.
77     // BLSR combines two instuctions into one and reduces register pressure.
78 #if defined(__GNUC__) && !__GNUC_PREREQ(4, 9)
79     // GCC 4.8 doesn't support the intrinsics.
80     uint64_t result;
81     asm ("blsrq %1, %0" : "=r" (result) : "r" (value));
82     return result;
83 #else
84     return _blsr_u64(value);
85 #endif
86   }
87 };
88
89 }}}  // namespaces