1 // Copyright 2017 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef PARTITION_ALLOC_PARTITION_ALLOC_BASE_NUMERICS_SAFE_CONVERSIONS_ARM_IMPL_H_ 6 #define PARTITION_ALLOC_PARTITION_ALLOC_BASE_NUMERICS_SAFE_CONVERSIONS_ARM_IMPL_H_ 7 8 #include <cassert> 9 #include <limits> 10 #include <type_traits> 11 12 #include "partition_alloc/partition_alloc_base/numerics/safe_conversions_impl.h" 13 14 namespace partition_alloc::internal::base::internal { 15 16 // Fast saturation to a destination type. 17 template <typename Dst, typename Src> 18 struct SaturateFastAsmOp { 19 static constexpr bool is_supported = 20 kEnableAsmCode && std::is_signed_v<Src> && std::is_integral_v<Dst> && 21 std::is_integral_v<Src> && 22 IntegerBitsPlusSign<Src>::value <= IntegerBitsPlusSign<int32_t>::value && 23 IntegerBitsPlusSign<Dst>::value <= IntegerBitsPlusSign<int32_t>::value && 24 !IsTypeInRangeForNumericType<Dst, Src>::value; 25 DoSaturateFastAsmOp26 __attribute__((always_inline)) static Dst Do(Src value) { 27 int32_t src = value; 28 typename std::conditional<std::is_signed_v<Dst>, int32_t, uint32_t>::type 29 result; 30 if (std::is_signed_v<Dst>) { 31 asm("ssat %[dst], %[shift], %[src]" 32 : [dst] "=r"(result) 33 : [src] "r"(src), [shift] "n"(IntegerBitsPlusSign<Dst>::value <= 32 34 ? IntegerBitsPlusSign<Dst>::value 35 : 32)); 36 } else { 37 asm("usat %[dst], %[shift], %[src]" 38 : [dst] "=r"(result) 39 : [src] "r"(src), [shift] "n"(IntegerBitsPlusSign<Dst>::value < 32 40 ? IntegerBitsPlusSign<Dst>::value 41 : 31)); 42 } 43 return static_cast<Dst>(result); 44 } 45 }; 46 47 } // namespace partition_alloc::internal::base::internal 48 49 #endif // PARTITION_ALLOC_PARTITION_ALLOC_BASE_NUMERICS_SAFE_CONVERSIONS_ARM_IMPL_H_ 50