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 #include "src/__support/architectures.h" 13 14 // Cache line sizes for ARM: These values are not strictly correct since 15 // cache line sizes depend on implementations, not architectures. There 16 // are even implementations with cache line sizes configurable at boot 17 // time. 18 #if defined(LLVM_LIBC_ARCH_AARCH64) || defined(LLVM_LIBC_ARCH_X86) 19 #define LLVM_LIBC_CACHELINE_SIZE 64 20 #elif defined(LLVM_LIBC_ARCH_ARM) 21 #define LLVM_LIBC_CACHELINE_SIZE 32 22 #else 23 #error "Unsupported platform for memory functions." 24 #endif 25 26 #include <stddef.h> // size_t 27 #include <stdint.h> // intptr_t / uintptr_t 28 29 namespace __llvm_libc { 30 31 // Return whether `value` is zero or a power of two. is_power2_or_zero(size_t value)32static constexpr bool is_power2_or_zero(size_t value) { 33 return (value & (value - 1U)) == 0; 34 } 35 36 // Return whether `value` is a power of two. is_power2(size_t value)37static constexpr bool is_power2(size_t value) { 38 return value && is_power2_or_zero(value); 39 } 40 41 // Compile time version of log2 that handles 0. log2(size_t value)42static constexpr size_t log2(size_t value) { 43 return (value == 0 || value == 1) ? 0 : 1 + log2(value / 2); 44 } 45 46 // Returns the first power of two preceding value or value if it is already a 47 // power of two (or 0 when value is 0). le_power2(size_t value)48static constexpr size_t le_power2(size_t value) { 49 return value == 0 ? value : 1ULL << log2(value); 50 } 51 52 // Returns the first power of two following value or value if it is already a 53 // power of two (or 0 when value is 0). ge_power2(size_t value)54static constexpr size_t ge_power2(size_t value) { 55 return is_power2_or_zero(value) ? value : 1ULL << (log2(value) + 1); 56 } 57 offset_from_last_aligned(const void * ptr)58template <size_t alignment> intptr_t offset_from_last_aligned(const void *ptr) { 59 static_assert(is_power2(alignment), "alignment must be a power of 2"); 60 return reinterpret_cast<uintptr_t>(ptr) & (alignment - 1U); 61 } 62 offset_to_next_aligned(const void * ptr)63template <size_t alignment> intptr_t offset_to_next_aligned(const void *ptr) { 64 static_assert(is_power2(alignment), "alignment must be a power of 2"); 65 // The logic is not straightforward and involves unsigned modulo arithmetic 66 // but the generated code is as fast as it can be. 67 return -reinterpret_cast<uintptr_t>(ptr) & (alignment - 1U); 68 } 69 70 // Returns the offset from `ptr` to the next cache line. offset_to_next_cache_line(const void * ptr)71static inline intptr_t offset_to_next_cache_line(const void *ptr) { 72 return offset_to_next_aligned<LLVM_LIBC_CACHELINE_SIZE>(ptr); 73 } 74 assume_aligned(T * ptr)75template <size_t alignment, typename T> static T *assume_aligned(T *ptr) { 76 return reinterpret_cast<T *>(__builtin_assume_aligned(ptr, alignment)); 77 } 78 79 } // namespace __llvm_libc 80 81 #endif // LLVM_LIBC_SRC_MEMORY_UTILS_UTILS_H 82