summaryrefslogtreecommitdiffstats
path: root/src/import/generic/memory/lib/utils/endian_utils.H
blob: 20bfd7caa1e44f7905e54ada4629f64001c05f46 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/generic/memory/lib/utils/endian_utils.H $          */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2018                             */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* 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.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

///
/// @file endian_utils.H
/// @brief Util functions to help with endianess
///

// *HWP HWP Owner: Ben Gass <bgass@us.ibm.com>
// *HWP HWP Backup: Christian Geddes <crgeddes@us.ibm.com>
// *HWP Team:
// *HWP Level: 2
// *HWP Consumed by: HB:FSP

#ifndef _ENDIAN_UTILS_H_
#define _ENDIAN_UTILS_H_

#include <cstdint>
#include <vector>
#include <generic/memory/lib/utils/shared/mss_generic_consts.H>

namespace mss
{

///
/// @brief Forces native data into LE order
/// @tparam T the data type to process
/// @param[in] i_input inputted data to process
/// @param[in,out] io_data vector to append data to
///
template < typename T >
void forceLE(const T& i_input, std::vector<uint8_t>& io_data)
{
    // Temporary variable to process - we'll be doing bit shifts below
    T l_temp = i_input;

    for(size_t i = 0; i < sizeof(i_input); i++)
    {
        // Grab the lowest order byte and add it to the back of the vector
        const uint8_t l_byte = l_temp & 0xFF;
        io_data.push_back(l_byte);

        // Shift higher byte value into lowest no matter existing endianness
        l_temp >>= BITS_PER_BYTE;
    }
}

///
/// @brief Forces native data into LE order for an array
/// @tparam T the data type to process
/// @param[in] i_input inputted data to process
/// @param[in] i_size size of the array
/// @param[in,out] io_data vector to append data to
///
template < typename T >
inline void forceLEArray(const T* i_input, const uint64_t i_size, std::vector<uint8_t>& io_data)
{
    for(size_t i = 0; i < i_size; i++)
    {
        forceLE(i_input[i], io_data);
    }
}

///
/// @brief Forces native data into BE order
/// @tparam T the data type to process
/// @param[in] i_input inputted data to process
/// @param[in,out] io_data vector to append data to
///
template < typename T >
void forceBE(const T& i_input, std::vector<uint8_t>& io_data)
{
    // Temporary variable to process - we'll be doing bit shifts below
    T l_temp = i_input;

    std::vector<uint8_t> l_tempBuffer;

    // This loop will put i_input into l_tempBuffer in BE order
    for(size_t i = 0; i < sizeof(i_input); i++)
    {
        // Grab the lowest order byte and add it to the front of the vector
        const uint8_t l_byte = l_temp & 0xFF;
        l_tempBuffer.insert(l_tempBuffer.begin(), l_byte);

        // Shift higher byte value into lowest no matter existing endianness
        l_temp >>= BITS_PER_BYTE;
    }

    // Put the new BE formatted data at the end of the input buffer
    io_data.insert(io_data.end(), l_tempBuffer.begin(), l_tempBuffer.end());
}

///
/// @brief Forces native data into BE order for an array
/// @tparam T the data type to process
/// @param[in] i_input inputted data to process
/// @param[in] i_size size of the array
/// @param[in,out] io_data vector to append data to
///
template < typename T >
inline void forceBEArray(const T* i_input, const uint64_t i_size, std::vector<uint8_t>& io_data)
{
    for(size_t i = 0; i < i_size; i++)
    {
        forceBE(i_input[i], io_data);
    }
}

///
/// @brief Converts LE data into native order
/// @tparam T the data type to output to
/// @param[in] i_input inputted data to process
/// @param[in,out] io_idx current index
/// @param[out] o_data data that has been converted into native endianness
/// @return bool true if passing false if failing
/// @note Real FFDC will be handled outside
///
template < typename T >
bool readLE(const std::vector<uint8_t>& i_input, uint32_t& io_idx, T& o_data)
{
    const uint32_t l_sz = static_cast<uint32_t>(sizeof(o_data));
    io_idx = l_sz + io_idx;

    // Checks that our final index is within the data range
    // Note: we decrement the index prior, so equal to is ok
    if(io_idx > i_input.size())
    {
        return false;
    }

    uint64_t l_idx = io_idx;

    o_data = 0;

    for(uint64_t i = 0; i < l_sz; i++)
    {
        l_idx--;
        uint8_t v = i_input[l_idx];
        o_data <<= BITS_PER_BYTE;
        o_data |= v;
    }

    return true;
}

///
/// @brief Converts LE data into native order
/// @tparam T the data type to output to
/// @param[in] i_input inputted data to process
/// @param[in] i_size size of the array
/// @param[in,out] io_idx current index
/// @param[out] o_data data that has been converted into native endianness
/// @return bool true if passing false if failing
/// @note Real FFDC will be handled outside
///
template < typename T >
bool readLEArray(const std::vector<uint8_t>& i_input, const uint32_t i_size, uint32_t& io_idx, T* o_data)
{
    // Loop while the readLE is still passing and we haven't looped through the array's boundaries
    bool l_passing = true;

    for(uint32_t i = 0; i < i_size && l_passing; ++i)
    {
        l_passing = readLE(i_input, io_idx, o_data[i]);
    }

    return l_passing;
}

///
/// @brief Converts BE data into native order
/// @tparam T the data type to output to
/// @param[in] i_input inputted data to process
/// @param[in,out] io_idx current index
/// @param[out] o_data data that has been converted into native endianness
/// @return bool true if passing false if failing
/// @note Real FFDC will be handled outside
///
template < typename T >
bool readBE(const std::vector<uint8_t>& i_input, uint32_t& io_idx, T& o_data)
{
    const uint32_t l_sz = static_cast<uint32_t>(sizeof(o_data));
    uint64_t l_idx = io_idx;
    io_idx = l_sz + io_idx;

    // Checks that our final index is within the data range
    // Note: we decrement the index prior, so equal to is ok
    if(io_idx > i_input.size())
    {
        return false;
    }

    o_data = 0;

    for(uint64_t i = 0; i < l_sz; i++)
    {
        uint8_t v = i_input[l_idx];
        o_data <<= BITS_PER_BYTE;
        o_data |= v;
        l_idx++;
    }

    return true;
}

///
/// @brief Converts BE data into native order
/// @tparam T the data type to output to
/// @param[in] i_input inputted data to process
/// @param[in] i_size size of the array
/// @param[in,out] io_idx current index
/// @param[out] o_data data that has been converted into native endianness
/// @return bool true if passing false if failing
/// @note Real FFDC will be handled outside
///
template < typename T >
bool readBEArray(const std::vector<uint8_t>& i_input, const uint32_t i_size, uint32_t& io_idx, T* o_data)
{
    // Loop while the readLE is still passing and we haven't looped through the array's boundaries
    bool l_passing = true;

    for(uint32_t i = 0; i < i_size && l_passing; ++i)
    {
        l_passing = readBE(i_input, io_idx, o_data[i]);
    }

    return l_passing;
}

}

#endif
OpenPOWER on IntegriCloud