aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/backends/RefWorkloads/Division.cpp
blob: cc7f7c9fe47a55a78384d12cf2a5ed5117a60d8e (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "Division.hpp"
#include "Broadcast.hpp"

#include <functional>

#include <cmath>

namespace
{

void ElementwiseDivision(unsigned int numElements,
                         const float* inData0,
                         const float* inData1,
                         float* outData)
{
    for (unsigned int i = 0; i < numElements; ++i)
    {
        if (inData1[i] != 0.0f)
        {
            outData[i] = inData0[i] / inData1[i];
        }
        else if (inData0[i] == 0.0f)
        {
            if (!std::signbit(inData1[i]))
            {
                outData[i]= NAN;
            }
            else
            {
                outData[i]= -NAN;
            }
        }
        else if (inData0[i] < 0.0f)
        {
            if (!std::signbit(inData1[i]))
            {
                outData[i] = -INFINITY;
            }
            else
            {
                outData[i] = INFINITY;
            }
        }
        else
        {
            if (!std::signbit(inData1[i]))
            {
                outData[i] = INFINITY;
            }
            else
            {
                outData[i] = -INFINITY;
            }
        }
    }
}

} // namespace

namespace armnn
{

void Division(const TensorShape& inShape0,
              const TensorShape& inShape1,
              const TensorShape& outShape,
              const float* inData0,
              const float* inData1,
              float* outData)
{
    if (inShape0 == inShape1)
    {
        ElementwiseDivision(inShape0.GetNumElements(), inData0, inData1, outData);
    }
    else
    {
        BroadcastLoop(inShape0, inShape1, outShape).Unroll(std::divides<float>(),
                                                           0,
                                                           inData0,
                                                           inData1,
                                                           outData);
    }
}

} //namespace armnn