• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Slightly adapted for inclusion in V8.
6 // Copyright 2014 the V8 project authors. All rights reserved.
7 // List of adaptations:
8 // - include guard names
9 // - wrap in v8 namespace
10 // - include paths
11 
12 #ifndef V8_BASE_SAFE_CONVERSIONS_ARM_IMPL_H_
13 #define V8_BASE_SAFE_CONVERSIONS_ARM_IMPL_H_
14 
15 #include <cassert>
16 #include <limits>
17 #include <type_traits>
18 
19 #include "src/base/safe_conversions_impl.h"
20 
21 namespace v8 {
22 namespace base {
23 namespace internal {
24 
25 // Fast saturation to a destination type.
26 template <typename Dst, typename Src>
27 struct SaturateFastAsmOp {
28   static constexpr bool is_supported =
29       std::is_signed<Src>::value && std::is_integral<Dst>::value &&
30       std::is_integral<Src>::value &&
31       IntegerBitsPlusSign<Src>::value <= IntegerBitsPlusSign<int32_t>::value &&
32       IntegerBitsPlusSign<Dst>::value <= IntegerBitsPlusSign<int32_t>::value &&
33       !IsTypeInRangeForNumericType<Dst, Src>::value;
34 
DoSaturateFastAsmOp35   __attribute__((always_inline)) static Dst Do(Src value) {
36     int32_t src = value;
37     typename std::conditional<std::is_signed<Dst>::value, int32_t,
38                               uint32_t>::type result;
39     if (std::is_signed<Dst>::value) {
40       asm("ssat %[dst], %[shift], %[src]"
41           : [dst] "=r"(result)
42           : [src] "r"(src), [shift] "n"(IntegerBitsPlusSign<Dst>::value <= 32
43                                             ? IntegerBitsPlusSign<Dst>::value
44                                             : 32));
45     } else {
46       asm("usat %[dst], %[shift], %[src]"
47           : [dst] "=r"(result)
48           : [src] "r"(src), [shift] "n"(IntegerBitsPlusSign<Dst>::value < 32
49                                             ? IntegerBitsPlusSign<Dst>::value
50                                             : 31));
51     }
52     return static_cast<Dst>(result);
53   }
54 };
55 
56 }  // namespace internal
57 }  // namespace base
58 }  // namespace v8
59 
60 #endif  // V8_BASE_SAFE_CONVERSIONS_ARM_IMPL_H_
61