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