aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src/generate/generate_dot_product_states.cc
blob: cd9ffbacf6310aeb594d44068666e64afcf71c25 (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
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2023, ARM Limited.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#include "generate_dot_product.h"
#include "generate_utils.h"

#include <cstdint>

namespace
{

// Input index global variables
inline constexpr uint32_t P0 = 0;
inline constexpr uint32_t P1 = 1;

// Unused helper function
template <typename... Args>
inline void unused(Args&&...)
{}

// Primitive generator class
//
// Yields a new value on function operator access and increases the
// index by one
class PrimitiveGenerator
{
public:
    PrimitiveGenerator(uint32_t S)
        : _S(S)
        , _m(0)
        , _r(0)
        , _index(0)
    {
        _m = (8 * _S + 1) * 0x705A5E75;
        _r = _m + 1;
    }

    [[nodiscard]] float operator()()
    {
        _r           = _r * _m + 1;
        float sign   = (_r >> 31) == 0 ? +1 : -1;
        float pseudo = sign * (float)(_r & 0x7FFFFFFF) / (float)(0x7FFFFFFF);
        ++_index;

        return pseudo;
    }

    uint32_t index()
    {
        return _index;
    }

private:
    uint32_t _S;
    uint32_t _m;
    uint32_t _r;
    uint32_t _index;
};

//----------------------------------------------------------------------------//
// State generators
//----------------------------------------------------------------------------//

// S0 generator
class GeneratorS0 : public TosaReference::IDotProductGenerator
{
public:
    GeneratorS0(uint32_t p)
        : _p(p)
        , _s0(0)    // set_data(2*S)
        , _s1(1)    // set_data(2*S+1)
    {}
    float operator()(uint32_t k) override
    {
        unused(k);
        const float s0 = _s0();
        const float s1 = _s1();
        if (_p == P0)
            return s0 < 0.f ? 0.f : s1;
        else
            return s0 < 0.f ? s1 : 0.f;
    }

private:
    uint32_t _p;
    PrimitiveGenerator _s0;
    PrimitiveGenerator _s1;
};

}    // namespace

namespace TosaReference
{

std::unique_ptr<IDotProductGenerator> pickDotProductGenerator(const GenerateConfig& cfg)
{
    const DotProductInfo& dpinfo = cfg.dotProductInfo;
    switch (dpinfo.s)
    {
        case 0:
            return std::make_unique<GeneratorS0>(cfg.inputPos);
        default:
            return nullptr;
    }
    return nullptr;
}

}    // namespace TosaReference