Remove disallowed &* of FwdIterator
[folly.git] / folly / Bits.cpp
1 /*
2  * Copyright 2014 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 #include "folly/Bits.h"
18
19 #include "folly/CpuId.h"
20 #include "folly/Portability.h"
21
22 // None of this is necessary if we're compiling for a target that supports
23 // popcnt
24 #ifndef __POPCNT__
25
26 namespace {
27
28 int popcount_builtin(unsigned int x) {
29   return __builtin_popcount(x);
30 }
31
32 int popcountll_builtin(unsigned long long x) {
33   return __builtin_popcountll(x);
34 }
35
36 #if FOLLY_HAVE_IFUNC && !defined(FOLLY_SANITIZE_ADDRESS)
37
38 // Strictly speaking, these versions of popcount are usable without ifunc
39 // support. However, we would have to check, via CpuId, if the processor
40 // implements the popcnt instruction first, which is what we use ifunc for.
41 int popcount_inst(unsigned int x) {
42   int n;
43   asm ("popcntl %1, %0" : "=r" (n) : "r" (x));
44   return n;
45 }
46
47 int popcountll_inst(unsigned long long x) {
48   unsigned long long n;
49   asm ("popcntq %1, %0" : "=r" (n) : "r" (x));
50   return n;
51 }
52
53 typedef decltype(popcount_builtin) Type_popcount;
54 typedef decltype(popcountll_builtin) Type_popcountll;
55
56 // This function is called on startup to resolve folly::detail::popcount
57 extern "C" Type_popcount* folly_popcount_ifunc() {
58   return folly::CpuId().popcnt() ?  popcount_inst : popcount_builtin;
59 }
60
61 // This function is called on startup to resolve folly::detail::popcountll
62 extern "C" Type_popcountll* folly_popcountll_ifunc() {
63   return folly::CpuId().popcnt() ?  popcountll_inst : popcountll_builtin;
64 }
65
66 #endif  // FOLLY_HAVE_IFUNC && !defined(FOLLY_SANITIZE_ADDRESS)
67
68 }  // namespace
69
70 namespace folly {
71 namespace detail {
72
73 // Call folly_popcount_ifunc on startup to resolve to either popcount_inst
74 // or popcount_builtin
75 int popcount(unsigned int x)
76 #if FOLLY_HAVE_IFUNC && !defined(FOLLY_SANITIZE_ADDRESS)
77   __attribute__((ifunc("folly_popcount_ifunc")));
78 #else
79 {  return popcount_builtin(x); }
80 #endif
81
82 // Call folly_popcount_ifunc on startup to resolve to either popcountll_inst
83 // or popcountll_builtin
84 int popcountll(unsigned long long x)
85 #if FOLLY_HAVE_IFUNC && !defined(FOLLY_SANITIZE_ADDRESS)
86   __attribute__((ifunc("folly_popcountll_ifunc")));
87 #else
88 {  return popcountll_builtin(x); }
89 #endif
90
91 }  // namespace detail
92 }  // namespace folly
93
94 #endif  /* !__POPCNT__ */