2 * Copyright 2017 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.
21 #include <folly/Portability.h>
26 namespace portability {
28 void call_flush_instruction_cache_self_pid(void* begin, size_t size);
33 FOLLY_ALWAYS_INLINE void __builtin___clear_cache(char* begin, char* end) {
34 if (folly::kIsArchAmd64) {
35 // x86_64 doesn't require the instruction cache to be flushed after
38 // Default to flushing it for everything else, such as ARM.
39 folly::portability::detail::call_flush_instruction_cache_self_pid(
40 static_cast<void*>(begin), static_cast<size_t>(end - begin));
44 FOLLY_ALWAYS_INLINE int __builtin_clz(unsigned int x) {
46 return int(_BitScanReverse(&index, (unsigned long)x) ? 31 - index : 32);
49 FOLLY_ALWAYS_INLINE int __builtin_clzl(unsigned long x) {
50 return __builtin_clz((unsigned int)x);
53 FOLLY_ALWAYS_INLINE int __builtin_clzll(unsigned long long x) {
55 return int(_BitScanReverse64(&index, x) ? 63 - index : 64);
58 FOLLY_ALWAYS_INLINE int __builtin_ctzll(unsigned long long x) {
60 return int(_BitScanForward64(&index, x) ? index : 64);
63 FOLLY_ALWAYS_INLINE int __builtin_ffs(int x) {
65 return int(_BitScanForward(&index, (unsigned long)x) ? index + 1 : 0);
68 FOLLY_ALWAYS_INLINE int __builtin_ffsl(long x) {
69 return __builtin_ffs(int(x));
72 FOLLY_ALWAYS_INLINE int __builtin_ffsll(long long x) {
74 return int(_BitScanForward64(&index, (unsigned long long)x) ? index + 1 : 0);
77 FOLLY_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) {
78 return int(__popcnt64(x));
81 FOLLY_ALWAYS_INLINE void* __builtin_return_address(unsigned int frame) {
82 // I really hope frame is zero...
85 return _ReturnAddress();