1 //===-- Memory utils --------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIBC_SRC_MEMORY_UTILS_UTILS_H 10 #define LLVM_LIBC_SRC_MEMORY_UTILS_UTILS_H 11 12 // Cache line sizes for ARM: These values are not strictly correct since 13 // cache line sizes depend on implementations, not architectures. There 14 // are even implementations with cache line sizes configurable at boot 15 // time. 16 #if defined(__aarch64__) 17 #define LLVM_LIBC_CACHELINE_SIZE 64 18 #elif defined(__ARM_ARCH_5T__) 19 #define LLVM_LIBC_CACHELINE_SIZE 32 20 #elif defined(__ARM_ARCH_7A__) 21 #define LLVM_LIBC_CACHELINE_SIZE 64 22 #elif defined(__PPC64__) 23 #define LLVM_LIBC_CACHELINE_SIZE 128 24 #elif defined(__i386__) || defined(__x86_64__) 25 #define LLVM_LIBC_CACHELINE_SIZE 64 26 #else 27 #error "Unsupported platform for memory functions." 28 #endif 29 30 #include <stddef.h> // size_t 31 #include <stdint.h> // intptr_t / uintptr_t 32 33 namespace __llvm_libc { 34 35 // Return whether `value` is zero or a power of two. is_power2_or_zero(size_t value)36static constexpr bool is_power2_or_zero(size_t value) { 37 return (value & (value - 1U)) == 0; 38 } 39 40 // Return whether `value` is a power of two. is_power2(size_t value)41static constexpr bool is_power2(size_t value) { 42 return value && is_power2_or_zero(value); 43 } 44 45 // Compile time version of log2 that handles 0. log2(size_t value)46static constexpr size_t log2(size_t value) { 47 return (value == 0 || value == 1) ? 0 : 1 + log2(value / 2); 48 } 49 50 // Returns the first power of two preceding value or value if it is already a 51 // power of two (or 0 when value is 0). le_power2(size_t value)52static constexpr size_t le_power2(size_t value) { 53 return value == 0 ? value : 1ULL << log2(value); 54 } 55 56 // Returns the first power of two following value or value if it is already a 57 // power of two (or 0 when value is 0). ge_power2(size_t value)58static constexpr size_t ge_power2(size_t value) { 59 return is_power2_or_zero(value) ? value : 1ULL << (log2(value) + 1); 60 } 61 offset_from_last_aligned(const void * ptr)62template <size_t alignment> intptr_t offset_from_last_aligned(const void *ptr) { 63 static_assert(is_power2(alignment), "alignment must be a power of 2"); 64 return reinterpret_cast<uintptr_t>(ptr) & (alignment - 1U); 65 } 66 offset_to_next_aligned(const void * ptr)67template <size_t alignment> intptr_t offset_to_next_aligned(const void *ptr) { 68 static_assert(is_power2(alignment), "alignment must be a power of 2"); 69 // The logic is not straightforward and involves unsigned modulo arithmetic 70 // but the generated code is as fast as it can be. 71 return -reinterpret_cast<uintptr_t>(ptr) & (alignment - 1U); 72 } 73 74 // Returns the offset from `ptr` to the next cache line. offset_to_next_cache_line(const void * ptr)75static inline intptr_t offset_to_next_cache_line(const void *ptr) { 76 return offset_to_next_aligned<LLVM_LIBC_CACHELINE_SIZE>(ptr); 77 } 78 assume_aligned(T * ptr)79template <size_t alignment, typename T> static T *assume_aligned(T *ptr) { 80 return reinterpret_cast<T *>(__builtin_assume_aligned(ptr, alignment)); 81 } 82 83 } // namespace __llvm_libc 84 85 #endif // LLVM_LIBC_SRC_MEMORY_UTILS_UTILS_H 86