ArmNN
 22.08
EthosnRefConvImpl.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "EthosnRefConvImpl.hpp"
7 
9 
10 #include <cmath>
11 #include <limits>
12 
13 namespace armnn
14 {
15 
17 {
18  ARMNN_ASSERT(multiplier >= 0.0f && multiplier < 1.0f);
19  if (multiplier == 0.0f)
20  {
21  m_Multiplier = 0;
22  m_RightShift = 0;
23  }
24  else
25  {
26  const double q = std::frexp(multiplier, &m_RightShift);
27  m_RightShift = -m_RightShift;
28  int64_t qFixed = static_cast<int64_t>(std::round(q * (1ll << 16)));
29  ARMNN_ASSERT(qFixed <= (1ll << 16));
30  if (qFixed == (1ll << 16))
31  {
32  qFixed /= 2;
33  --m_RightShift;
34  }
35  ARMNN_ASSERT(m_RightShift >= 0);
36  ARMNN_ASSERT(qFixed <= std::numeric_limits<int32_t>::max());
37  m_Multiplier = static_cast<int32_t>(qFixed);
38  }
39 }
40 
41 #define SATURATE_VAL_S_32S(WIDTH, VAL) \
42  (static_cast<int32_t>(VAL > ((static_cast<int64_t>(1) << (WIDTH - 1)) - 1) \
43  ? ((static_cast<int64_t>(1) << (WIDTH - 1)) - 1) \
44  : VAL < (-1 * (static_cast<int64_t>(1) << (WIDTH - 1))) \
45  ? (-1 * (static_cast<int64_t>(1) << (WIDTH - 1))) \
46  : VAL))
47 
49 {
50  // Align with the implementation in NPU model
51  // See function WinogradFinalTransform::postscale_uint8 in winograd_final_transform.cpp
52  int64_t a_64(rhs);
53  int64_t b_64(m_Multiplier);
54  int64_t ab_64 = a_64 * b_64;
55 
56  ab_64 = ab_64 >> 15;
57  int32_t roundBit = (ab_64 >> m_RightShift) & 0x1;
58  int64_t resultShifted = (ab_64 >> (m_RightShift + 1)) + roundBit;
59 
60  return SATURATE_VAL_S_32S(9, resultShifted);
61 }
62 
64 {
65  // Align with the implementation in NPU model
66  // See function WinogradFinalTransform::postscale_uint8 in winograd_final_transform.cpp
67  int64_t a_64(rhs);
68  int64_t b_64(m_Multiplier);
69  int64_t ab_64 = a_64 * b_64;
70 
71  ab_64 = ab_64 >> 15;
72  int32_t roundBit = (ab_64 >> m_RightShift) & 0x1;
73  int64_t resultShifted = (ab_64 >> (m_RightShift + 1)) + roundBit;
74 
75  return SATURATE_VAL_S_32S(16, resultShifted);
76 }
77 
78 } //namespace armnn
int32_t operator*(int32_t rhs) const
The implementation of this function is adapted from Android NN&#39;s MultiplyByEthosnRefQuantizedMultipli...
#define SATURATE_VAL_S_32S(WIDTH, VAL)
Copyright (c) 2021 ARM Limited and Contributors.
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
EthosnRefQuantizedMultiplierSmallerThanOne(float multiplier)
Constructs a EthosnRefQuantizedMultiplierSmallerThanOne which will multiply by the given multiplier...