-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfloat_type.h
48 lines (41 loc) · 1.57 KB
/
float_type.h
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
#ifndef __FLOAT_TYPE_H__
#define __FLOAT_TYPE_H__
#ifdef PARSING
float float_shift(float x, int shift);
unsigned float_to_uint(float a);
float uint_to_float(unsigned a);
#else
#ifndef __PIPELINEC__
union fp_tlayout { float f; unsigned i; struct { unsigned mantissa:23; unsigned exp:8; unsigned sign:1; }; };
inline unsigned float_to_uint(float a) { fp_tlayout conv; conv.f = a; return conv.i; }
inline float uint_to_float(unsigned a) { fp_tlayout conv; conv.i = a; return conv.f; }
inline float float_shift(float x, int shift) { return shift > 0 ? x * (1<<shift) : x / ((1<<-shift)); }
//correct float_shift implementation in hardware just adds the shift to exponent
//inline float float_shift(float x, int shift) { fp_tlayout conv; conv.f = x; conv.exp+=shift; return conv.f; }
//main overloaded float operators
#define float_mul(a,b) ((a)*(b))
#define float_add(a,b) ((a)+(b))
#define float_sub(a,b) ((a)-(b))
#define float_div(a,b) ((a)/(b))
#else
#include "pipelinec_float_type.h"
#endif
#endif
//misc overloaded float operators
#define float_lt(a, b) ((a)<(b))
#define float_gt(a, b) ((a)>(b))
#define float_eq(a, b) ((a)==(b))
#define float_neq(a, b) ((a)!=(b))
#define float_mul_double(a, b) float_mul(a, b)
#define float_sub_double(a, b) float_sub(a, b)
#define float_neq_double(a, b) float_neq(a, b)
#define float_lt_double(a, b) float_lt(a, b)
#define float_gt_double(a, b) float_gt(a, b)
#define double_div_float(a, b) float_div(a, b)
#include "custom_float.h" //may redefine float
#ifndef __PIPELINEC__
typedef float float_type;
#else
#define float_type float
#endif
#endif //__FLOAT_TYPE_H__