-
Notifications
You must be signed in to change notification settings - Fork 0
/
FixedPoint.h
128 lines (102 loc) · 3.58 KB
/
FixedPoint.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
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
119
120
121
122
123
124
125
126
127
128
/*
* FixedPoint.h
*
* Created on: 24.05.2012
* Author: Mircea
*/
#ifndef FIXEDPOINT_H_
#define FIXEDPOINT_H_
/*structures*/
struct __float{
int mantissa :23;
int exp :8;
int sign :1;
} ;
typedef struct __float _float;
struct __int8_8
{
unsigned char q:8;
unsigned char p:8;
};
typedef struct __int8_8 int8_8;
struct __int16_16
{
unsigned short q:16;
unsigned short p:16;
};
typedef struct __int16_16 int16_16;
struct __int8_24
{
unsigned int q:24;
unsigned char p:8;
};
typedef struct __int8_24 int8_24;
struct __int24_8
{
unsigned char q:8;
unsigned int p:24;
};
typedef struct __int24_8 int24_8;
#define _int(X) ((int)(((X).p<128)?((*(int*)&X)&0x0000FFFF):((*(int*)&X)|0xFFFF0000)))
#define _int8_8(X) *(int8_8*)&X
#define _int16_16(X) *(int16_16*)&X
#define _int8_24(X) *(int8_24*)&X
#define _int24_8(X) *(int24_8*)&X
#define max_fract(X) ((X).q=0xFFFF)
#define size_fract(X) (max_fract(X)==0xFFFF?16:(max_fract(X)==0x00FF?8:4))
#define max_int_part(X) ((X).p=0xFFFF)
#define norm_int_part(X) (max_int_part(X)==0xFFFF?0:(max_int_part(X)==0x00FF?8:4))
#define __int(X) ((int)(((X).p << 16) + ((X).q << (16-size_fract(X)))))
#define print8_8(X) printf("%d : %d\n",(X).p,(X).q)
/*comparison macros*/
#define comp_gt(X,Y) ((_int(X)) > (_int(Y)))
#define comp_lt(X,Y) ((_int(X)) < (_int(Y)))
#define comp_eq(X,Y) ((_int(X)) == (_int(Y)))
#define comp_not_eq(X,Y) ((_int(X)) != (_int(Y)))
#define comp_gt_eq(X,Y) ((_int(X)) >= (_int(Y)))
#define comp_lt_eq(X,Y) ((_int(X)) <= (_int(Y)))
/*utility macros*/
#define floor(X) ((int)(_int(X)>>8))
/*ceil,trunc,round,fix*/
/*arithmetic macros*/
/*init and alloc macros*/
#define int8_8_alloc(X) if(1){ int __xx=0; X=(int8_8*)&__xx;}
__inline__ int8_8 int8_8_new(const float X);
__inline__ int8_8 add8_8(int8_8 x,int8_8 y);
__inline__ int8_8 sadd8_8(int8_8 x,int8_8 y);
__inline__ int8_8 sub8_8(int8_8 x,int8_8 y);
__inline__ int8_8 ssub8_8(int8_8 x,int8_8 y);
__inline__ int8_8 mul8_8(int8_8 x,int8_8 y);
__inline__ int8_8 smul8_8(int8_8 x,int8_8 y);
__inline__ int8_8 fract8_8(int8_8 x);
__inline__ int8_8 floor8_8(int8_8 x);
__inline__ int16_16 cast8_8_to_16_16(int8_8 x);
__inline__ int8_8 cast16_16_to_8_8(int16_16 x);
__inline__ int16_16 int16_16_new(const float X);
__inline__ int16_16 add16_16(int16_16 x,int16_16 y);
__inline__ int16_16 sadd16_16(int16_16 x,int16_16 y);
__inline__ int16_16 sub16_16(int16_16 x,int16_16 y);
__inline__ int16_16 ssub16_16(int16_16 x,int16_16 y);
__inline__ int16_16 mul16_16(int16_16 x,int16_16 y);
__inline__ int16_16 smul16_16(int16_16 x,int16_16 y);
__inline__ int16_16 fract16_16(int16_16 x);
__inline__ int16_16 floor16_16(int16_16 x);
__inline__ int8_24 int8_24_new(const float X);
__inline__ int8_24 add8_24(int8_24 x,int8_24 y);
__inline__ int8_24 sadd8_24(int8_24 x,int8_24 y);
__inline__ int8_24 sub8_24(int8_24 x,int8_24 y);
__inline__ int8_24 ssub8_24(int8_24 x,int8_24 y);
__inline__ int8_24 mul8_24(int8_24 x,int8_24 y);
__inline__ int8_24 smul8_24(int8_24 x,int8_24 y);
__inline__ int8_24 floor8_24(int8_24 x);
__inline__ int8_24 fract8_24(int8_24 x);
__inline__ int24_8 int24_8_new(const float X);
__inline__ int24_8 add24_8(int24_8 x,int24_8 y);
__inline__ int24_8 sadd24_8(int24_8 x,int24_8 y);
__inline__ int24_8 sub24_8(int24_8 x,int24_8 y);
__inline__ int24_8 ssub24_8(int24_8 x,int24_8 y);
__inline__ int24_8 mul24_8(int24_8 x,int24_8 y);
__inline__ int24_8 smul24_8(int24_8 x,int24_8 y);
__inline__ int24_8 floor24_8(int24_8 x);
__inline__ int24_8 fract24_8(int24_8 x);
#endif