1 #include "hhutil.h"
2
3 /////////////////////////////////////////////////////////////////////////////////////
4 // Errors
5 /////////////////////////////////////////////////////////////////////////////////////
FormatError(const char infile[],const char * file,const int line,const char * func,const char details[])6 int FormatError(const char infile[], const char* file, const int line, const char* func, const char details[]) {
7 HH_LOG(ERROR) << "In " << file << ":" << line << ": " << func << ":" << std::endl;
8 HH_LOG(ERROR) << "\twrong format while reading file \'"<<infile<<". "<<details<<"\n";
9 exit(1);
10 }
11
OpenFileError(const char outfile[],const char * file,const int line,const char * func)12 int OpenFileError(const char outfile[], const char* file, const int line, const char* func) {
13 HH_LOG(ERROR) << "In " << file << ":" << line << ": " << func << ":" << std::endl;
14 HH_LOG(ERROR) << "\tcould not open file \'" << outfile << "\'\n";
15 exit(2);
16 }
17
MemoryError(const char arrayname[],const char * file,const int line,const char * func)18 int MemoryError(const char arrayname[], const char* file, const int line, const char* func) {
19 HH_LOG(ERROR) << "In " << file << ":" << line << ": " << func << ":" << std::endl;
20 HH_LOG(ERROR) << "\tCould not allocate memory for \'"<<arrayname<<"\'." << std::endl;
21 HH_LOG(ERROR) << "\tDo you have >=4GB of RAM per core on your machine? " <<
22 "Are your max memory size and stack sizes sufficient? " <<
23 "(Check using '$ ulimit -a' under Linux and best set to 'unlimited')" << std::endl;
24 exit(3);
25 }
26
SyntaxError(const char * file,const int line,const char * func,const char details[])27 int SyntaxError(const char* file, const int line, const char* func, const char details[]) {
28 HH_LOG(ERROR) << "In " << file << ":" << line << ": " << func << ":" << std::endl;
29 HH_LOG(ERROR) << "\ton command line: "<<details << std::endl;
30 exit(4);
31 }
32
InternalError(const char errstr[],const char * file,const int line,const char * func)33 int InternalError(const char errstr[], const char* file, const int line, const char* func) {
34 HH_LOG(ERROR) << "In " << file << ":" << line << ": " << func << ":" << std::endl;
35 HH_LOG(ERROR) << "\t" << errstr << ". Please report this bug to the developers" << std::endl;
36 exit(6);
37 }
38
39 /////////////////////////////////////////////////////////////////////////////////////
40 // Count number of lines in <file>
41 /////////////////////////////////////////////////////////////////////////////////////
CountLinesInFile(const char * file)42 int CountLinesInFile(const char* file)
43 {
44 char line[LINELEN]=""; // input line
45 int numlines=0;
46 char tmp_file[NAMELEN];
47 strcpy(tmp_file, file);
48 strcat(tmp_file, ".sizes");
49 FILE* fin = fopen(tmp_file, "r");
50 if (fin)
51 {
52 char* ptr=fgets(line, LINELEN, fin);
53 numlines = strint(ptr);
54 fclose(fin);
55 }
56 else
57 {
58 fin = fopen(file, "r");
59 if (!fin) OpenFileError(file, __FILE__, __LINE__, __func__);
60 while (fgets(line,LINELEN,fin)) numlines++;
61 fclose(fin);
62 }
63 return numlines;
64 }
65
66
67
float_to_8_bit(float input,unsigned char & result)68 void float_to_8_bit(float input, unsigned char& result) {
69 const unsigned int normalization_111 = 939524096;
70 const unsigned int complete_exp_mask = 2139095040;
71 const unsigned int exp_mask = 125829120;
72 const unsigned int mant_mask = 7864320;
73
74 const unsigned int exp_shift = 19;
75 const unsigned int mant_shift = 19;
76
77 unsigned int in = *((unsigned int*)(&input));
78
79 unsigned int e = in & complete_exp_mask;
80 e -= normalization_111;
81 e = e & exp_mask;
82 e = e >> exp_shift;
83
84 unsigned int m = in & mant_mask;
85 m = m >> mant_shift;
86
87 result = e | m;
88 }
89
bit_8_to_float(unsigned char input,float & result)90 void bit_8_to_float(unsigned char input, float& result) {
91 const unsigned int normalization_111 = 939524096;
92 const unsigned int exp_shift = 19;
93 const unsigned int mant_shift = 19;
94
95 unsigned int m = input & 15;
96 m = m << mant_shift;
97
98 unsigned int e = input & 240;
99 e = e << exp_shift;
100 e += normalization_111;
101
102 unsigned int res = e | m;
103
104 result = *((float*)(&res));
105 }
106
float_to_16_bit(float input,unsigned short int & result)107 void float_to_16_bit(float input, unsigned short int& result) {
108 const unsigned int normalization_63 = 536870912;
109 const unsigned int complete_exp_mask = 2139095040;
110 const unsigned int exp_mask = 528482304;
111 const unsigned int mant_mask = 8380416;
112
113 const unsigned int exp_shift = 13;
114 const unsigned int mant_shift = 13;
115
116 unsigned int in = *((unsigned int*)(&input));
117
118 unsigned int e = in & complete_exp_mask;
119 e -= normalization_63;
120 e = e & exp_mask;
121 e = e >> exp_shift;
122
123 unsigned int m = in & mant_mask;
124 m = m >> mant_shift;
125
126 result = e | m;
127 }
128
bit_16_to_float(unsigned short int input,float & result)129 void bit_16_to_float(unsigned short int input, float& result) {
130 const unsigned int normalization_63 = 536870912;
131
132 const unsigned int exp_shift = 13;
133 const unsigned int mant_shift = 13;
134
135 unsigned int m = input & 2046;
136 m = m << mant_shift;
137
138 unsigned int e = input & 129024;
139 e = e << exp_shift;
140 e += normalization_63;
141 unsigned int res = e | m;
142
143 result = *((float*)(&res));
144 }
145
writeU16(std::ostream & file,unsigned short int val)146 void writeU16(std::ostream& file, unsigned short int val) {
147 unsigned char bytes[2];
148
149 // extract the individual bytes from our value
150 bytes[1] = (val) & 0xFF; // low byte
151 bytes[0] = (val >> 8) & 0xFF; // high byte
152
153 // write those bytes to the file
154 file.write( (char*)bytes, 2 );
155 }
156
writeS16(std::ostream & file,signed short int val)157 void writeS16(std::ostream& file, signed short int val) {
158 signed char bytes[2];
159
160 // extract the individual bytes from our value
161 bytes[1] = (val) & 0xFF; // low byte
162 bytes[0] = (val >> 8) & 0xFF; // high byte
163
164 // write those bytes to the file
165 file.write( (char*)bytes, 2 );
166 }
167
168