summaryrefslogtreecommitdiff
path: root/chromium/base/numerics/saturated_arithmetic_arm.h
blob: 732f5f2c1f5f5668387649f0e1b2b0fd6b4a51ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_NUMERICS_SATURATED_ARITHMETIC_ARM_H_
#define BASE_NUMERICS_SATURATED_ARITHMETIC_ARM_H_

#include <limits>

namespace base {

inline int32_t SaturatedAddition(int32_t a, int32_t b) {
  int32_t result;

  asm("qadd %[output],%[first],%[second]"
      : [output] "=r"(result)
      : [first] "r"(a), [second] "r"(b));

  return result;
}

inline int32_t SaturatedSubtraction(int32_t a, int32_t b) {
  int32_t result;

  asm("qsub %[output],%[first],%[second]"
      : [output] "=r"(result)
      : [first] "r"(a), [second] "r"(b));

  return result;
}

inline int32_t SaturatedNegative(int32_t a) {
  return SaturatedSubtraction(0, a);
}

inline int32_t SaturatedAbsolute(int32_t a) {
  if (a >= 0)
    return a;
  return SaturatedNegative(a);
}

inline int GetMaxSaturatedSetResultForTesting(int fractional_shift) {
  // For ARM Asm version the set function maxes out to the biggest
  // possible integer part with the fractional part zero'd out.
  // e.g. 0x7fffffc0.
  return std::numeric_limits<int>::max() & ~((1 << fractional_shift) - 1);
}

inline int GetMinSaturatedSetResultForTesting(int fractional_shift) {
  return std::numeric_limits<int>::min();
}

template <int fractional_shift>
inline int SaturatedSet(int value) {
  // Figure out how many bits are left for storing the integer part of
  // the fixed point number, and saturate our input to that
  enum { Saturate = 32 - fractional_shift };

  int result;

  // The following ARM code will Saturate the passed value to the number of
  // bits used for the whole part of the fixed point representation, then
  // shift it up into place. This will result in the low <FractionShift> bits
  // all being 0's. When the value saturates this gives a different result
  // to from the C++ case; in the C++ code a saturated value has all the low
  // bits set to 1 (for a +ve number at least). This cannot be done rapidly
  // in ARM ... we live with the difference, for the sake of speed.

  asm("ssat %[output],%[saturate],%[value]\n\t"
      "lsl  %[output],%[shift]"
      : [output] "=r"(result)
      : [value] "r"(value), [saturate] "n"(Saturate),
        [shift] "n"(fractional_shift));

  return result;
}

template <int fractional_shift>
inline int SaturatedSet(unsigned value) {
  // Here we are being passed an unsigned value to saturate,
  // even though the result is returned as a signed integer. The ARM
  // instruction for unsigned saturation therefore needs to be given one
  // less bit (i.e. the sign bit) for the saturation to work correctly; hence
  // the '31' below.
  enum { Saturate = 31 - fractional_shift };

  // The following ARM code will Saturate the passed value to the number of
  // bits used for the whole part of the fixed point representation, then
  // shift it up into place. This will result in the low <FractionShift> bits
  // all being 0's. When the value saturates this gives a different result
  // to from the C++ case; in the C++ code a saturated value has all the low
  // bits set to 1. This cannot be done rapidly in ARM, so we live with the
  // difference, for the sake of speed.

  int result;

  asm("usat %[output],%[saturate],%[value]\n\t"
      "lsl  %[output],%[shift]"
      : [output] "=r"(result)
      : [value] "r"(value), [saturate] "n"(Saturate),
        [shift] "n"(fractional_shift));

  return result;
}

}  // namespace base

#endif  // BASE_NUMERICS_SATURATED_ARITHMETIC_ARM_H_