-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSource.cpp
More file actions
118 lines (106 loc) · 2.43 KB
/
Source.cpp
File metadata and controls
118 lines (106 loc) · 2.43 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
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
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdint.h>
#include <string>
//#include <math.h>
#include "fp16_t.h"
using namespace std;
typedef unsigned long long ULL;
template <typename T>
class Number {
//http://blog.csdn.net/qq_20480611/article/details/52564530
private:
T t;
public:
Number(T t = 0) :t(t) {}
Number(string& bitstr)
{
t = parseNumber(bitstr);
}
Number(const char* bitstr)
{
t = parseNumber(bitstr);
}
Number& operator = (string& bitstr)
{
t = parseNumber(bitstr);
return *this;
}
string toString()
{
int pos = 8 * sizeof(T);
char* bitsequence = (char*)malloc(pos + 1);
memset(bitsequence, 0, pos + 1);
bitsequence[pos] = 0;
ULL n = *(ULL*)&t;
while (pos--) {
bitsequence[pos] = (n & 1) + '0';
n >>= 1;
}
return bitsequence;
}
T parseNumber(const string& bitstr)
{
ULL n = bitstr[0] - '0';
for (unsigned i = 1; i < bitstr.length(); i++) {
n <<= 1;
n |= bitstr[i] - '0';
}
return *(T*)&n;
}
T getVal()
{
return t;
}
};
//#define COMMON
void fToFp(float f){
fp16_t fp;
fp = f;
Number<uint16_t> ui = fp.val;
cout << "float :" << f << " -> fp16 (uint16_t):" << fp.val<<" binary:"<< ui.toString()<< endl;
}
void fpToF(int val){
fp16_t fp;
fp.val = val;
float f = fp;
cout << "fp16(uint16_t value):" << val << " convert to float is:" << f << endl;
}
void main(int argc, char* argv[]){
if (argc == 2){
float f = atof(argv[1]);
fToFp(f);
}
else if (argc == 3){
int type = atoi(argv[1]);
if (type){
float f = atof(argv[2]);
fToFp(f);
}
else{
int input = atoi(argv[2]);
fpToF(input);
}
}
else if(argc==1){
int tp;
while (true){
cout << "��0�� fp16->float , otherwise float->fp16\nconvert type:";
cin >> tp;
if (tp){
cout << "input a float number:";
float f;
cin >> f;
fToFp(f);
}
else{
cout << "input a fp16 (uint16_t) number:";
int input;
cin >> input;
fpToF(input);
}
}
}
return;
}