2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <folly/Portability.h>
24 FOLLY_ALWAYS_INLINE int __builtin_clz(unsigned int x) {
26 return (int)(_BitScanReverse(&index, (unsigned long)x) ? 31 - index : 32);
29 FOLLY_ALWAYS_INLINE int __builtin_clzl(unsigned long x) {
30 return __builtin_clz((unsigned int)x);
33 FOLLY_ALWAYS_INLINE int __builtin_clzll(unsigned long long x) {
35 return (int)(_BitScanReverse64(&index, x) ? 63 - index : 64);
38 FOLLY_ALWAYS_INLINE int __builtin_ctzll(unsigned long long x) {
40 return (int)(_BitScanForward64(&index, x) ? index : 64);
43 FOLLY_ALWAYS_INLINE int __builtin_ffs(int x) {
45 return (int)(_BitScanForward(&index, (unsigned long)x) ? index : 0);
48 FOLLY_ALWAYS_INLINE int __builtin_ffsl(long x) { return __builtin_ffs((int)x); }
50 FOLLY_ALWAYS_INLINE int __builtin_ffsll(long long x) {
52 return (int)(_BitScanForward64(&index, (unsigned long long)x) ? index : 0);
55 FOLLY_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) {
56 return (int)__popcnt64(x);
59 FOLLY_ALWAYS_INLINE void* __builtin_return_address(unsigned int frame) {
60 // I really hope frame is zero...
62 return _ReturnAddress();