-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert.cc
More file actions
55 lines (46 loc) · 1.6 KB
/
convert.cc
File metadata and controls
55 lines (46 loc) · 1.6 KB
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
#include <cstddef>
#include <fstream>
#include <limits>
template <typename QuanT>
static constexpr float kQuantFloat() {
return static_cast<float>(std::numeric_limits<QuanT>::max()) + 1.0f;
}
template <typename QuanT, typename QuanS>
static inline void ConvertQuantToQuant(const QuanT* in_buf, QuanS* out_buf,
size_t n_elems) {
for (size_t i = 0; i < n_elems; i++) {
float trans = static_cast<float>(in_buf[i]) / kQuantFloat<QuanT>() *
kQuantFloat<QuanS>();
if (trans > std::numeric_limits<QuanS>::max()) {
trans = std::numeric_limits<QuanS>::max();
}
if (trans < std::numeric_limits<QuanS>::min()) {
trans = std::numeric_limits<QuanS>::min();
}
out_buf[i] = static_cast<QuanS>(trans);
}
}
int main(int argc, char** argv) {
if (argc < 3) {
printf("Usage: %s <input_file> <output_file>\n", argv[0]);
return -1;
}
std::string input_file = argv[1];
std::string output_file = argv[2];
std::ifstream input(input_file, std::ios::binary);
input.seekg(0, std::ios::end);
std::streampos fileSize = input.tellg();
input.seekg(0, std::ios::beg);
const std::size_t n_elems = fileSize / sizeof(short);
short* h_recv = new short[n_elems];
input.read(reinterpret_cast<char*>(h_recv), fileSize);
input.close();
char* h_recv_char = new char[n_elems];
ConvertQuantToQuant(h_recv, h_recv_char, n_elems);
std::ofstream output(output_file, std::ios::binary);
output.write(reinterpret_cast<char*>(h_recv_char), n_elems * sizeof(char));
output.close();
delete[] h_recv;
delete[] h_recv_char;
return 0;
}