blob: c64bc178576ff797563035e17d342fda45b0d9ee [file] [log] [blame]
Eric Kunze2364dcd2021-04-26 11:06:57 -07001
2// Copyright (c) 2020-2021, ARM Limited.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef _TOSA_NUMPY_UTILS_H
17#define _TOSA_NUMPY_UTILS_H
18
19#include <cassert>
20#include <cctype>
21#include <cstdint>
22#include <cstdio>
23#include <cstdlib>
24#include <cstring>
25#include <vector>
26
27class NumpyUtilities
28{
29public:
30 enum NPError
31 {
32 NO_ERROR = 0,
33 FILE_NOT_FOUND,
34 FILE_IO_ERROR,
35 FILE_TYPE_MISMATCH,
36 HEADER_PARSE_ERROR,
37 BUFFER_SIZE_MISMATCH,
38 };
39
40 static NPError readFromNpyFile(const char* filename, const uint32_t elems, float* databuf);
41
42 static NPError readFromNpyFile(const char* filename, const uint32_t elems, int32_t* databuf);
43
44 static NPError readFromNpyFile(const char* filename, const uint32_t elems, int64_t* databuf);
45
46 static NPError readFromNpyFile(const char* filename, const uint32_t elems, bool* databuf);
47
48 static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const bool* databuf);
49
50 static NPError writeToNpyFile(const char* filename, const uint32_t elems, const bool* databuf);
51
52 static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const int32_t* databuf);
53
54 static NPError writeToNpyFile(const char* filename, const uint32_t elems, const int32_t* databuf);
55
56 static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const int64_t* databuf);
57
58 static NPError writeToNpyFile(const char* filename, const uint32_t elems, const int64_t* databuf);
59
60 static NPError writeToNpyFile(const char* filename, const std::vector<int32_t>& shape, const float* databuf);
61
62 static NPError writeToNpyFile(const char* filename, const uint32_t elems, const float* databuf);
63
64private:
65 static NPError writeToNpyFileCommon(const char* filename,
66 const char* dtype_str,
67 const size_t elementsize,
68 const std::vector<int32_t>& shape,
69 const void* databuf,
70 bool bool_translate);
71 static NPError readFromNpyFileCommon(const char* filename,
72 const char* dtype_str,
73 const size_t elementsize,
74 const uint32_t elems,
75 void* databuf,
76 bool bool_translate);
77 static NPError checkNpyHeader(FILE* infile, const uint32_t elems, const char* dtype_str);
78 static NPError writeNpyHeader(FILE* outfile, const std::vector<int32_t>& shape, const char* dtype_str);
79};
80
81#endif // _TOSA_NUMPY_UTILS_H