forked from mrdudz/cc65-floatlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.h
161 lines (116 loc) · 4.05 KB
/
float.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef _FLOAT_H_
#define _FLOAT_H_
#ifdef __CC65__
#define BINARYFORMAT_CBM_UNPACKED 0
#define BINARYFORMAT_CBM_PACKED 1
#define BINARYFORMAT_IEEE754 2 // TODO
// BEWARE: also change in float.s
#define BINARYFORMAT BINARYFORMAT_CBM_PACKED
/*
format used in basic-variables
we dont use this format, although it saves one byte per variable, since that
removes the need of constantly converting between both formats. (someday
we may use an entire different (not cbm-specific), more accurate, format anyway)
*/
typedef struct {
unsigned char exponent;
unsigned char mantissa[4];
} FLOATBAS;
/*
format used in floating-point akku
this format can be directly used with most basic routines
*/
typedef struct {
unsigned char exponent;
unsigned char mantissa[4];
unsigned char sign;
} FLOATFAC;
#define float unsigned long
/* we dont wanna seriously use double precision eh? ;=P */
#define double float
/* integer convertion routines */
float __fastcall__ _ctof(char v);
float __fastcall__ _utof(unsigned char v);
float __fastcall__ _itof(int v);
float __fastcall__ _stof(unsigned short v);
int __fastcall__ _ftoi(float f);
/* arithmetic functions */
float __fastcall__ _fdiv(float f, float a);
float __fastcall__ _fmul(float f, float a);
float __fastcall__ _fadd(float f, float a);
float __fastcall__ _fsub(float f, float a);
float __fastcall__ _fpow(float f, float a);
float __fastcall__ _fneg(float f);
/* string convertion routines, these use the exponential form */
char * __fastcall__ _ftostr(char *d, float s); /* for vsprintf */
float __fastcall__ _strtof(char *d);
/* logical functions */
float __fastcall__ _fand(float f, float a);
float __fastcall__ _for(float f, float a);
float __fastcall__ _fnot(float f);
/* compare two floats, returns 0 if f = a, 1 if f < a, 255 if f > a */
unsigned char __fastcall__ _fcmp(float f, float a);
unsigned char __fastcall__ _ftestsgn(float f); /* fixme */
#define ctof(_s) _ctof((_s))
#define itof(_s) _itof((_s))
#define ltof(_s) _itof((_s)) /* TODO */
#define ftoi(_s) _ftoi((_s))
#define ftol(_s) _ftoi((_s)) /* TODO */
#define atof(_s) _strtof((_s))
char *ftoa(char *buf, float n);
#define fneg(_s) _fneg((_s))
#define fdiv(_f, _a) _fdiv((_f), (_a))
#define fmul(_f, _a) _fmul((_f), (_a))
#define fadd(_f, _a) _fadd((_f), (_a))
#define fsub(_f, _a) _fsub((_f), (_a))
#define fpow(_f, _a) _fpow((_f), (_a))
#define fcmp(_d, _s) _fcmp((_d), (_s))
#define fcmplt(_d, _s) (_fcmp((_d), (_s)) == 1)
#define fcmpgt(_d, _s) (_fcmp((_d), (_s)) == 255)
#define fcmpeq(_d, _s) (_fcmp((_d), (_s)) == 0)
/*
todo:
support for complex numbers
...stdlib...
char *ecvt( double value, int ndigit, int *decpt, int *sign )
char *fcvt( double value, int ndigit, int *decpt, int *sign )
char *gcvt( double value, int ndigit, char *buf )
*/
/* resets fp-libs in Turbo-C and M$-C */
#define _fpreset()
#else /* __CC65__ */
/* some macros to allow testing of programs using the lib in
a different compiler (with float support) */
#include <math.h>
#define fdiv(a,b) ((a)/(b))
#define fmul(a,b) ((a)*(b))
#define fadd(a,b) ((a)+(b))
#define fsub(a,b) ((a)-(b))
#define fpow(a,b) powf((a),(b))
#define fcmpgt(a,b) ((a)>(b))
#define fcmplt(a,b) ((a)<(b))
#define fcmpeq(a,b) ((a)==(b))
int fcmp(float a, float b)
{
if (a > b) return 255;
if (a < b) return 1;
return 0;
}
#define _fand(a,b) ((unsigned)(a) & (unsigned)(b))
#define _for(a,b) ((unsigned)(a) | (unsigned)(b))
#define _fneg(a) (-(float)(a))
#define fneg(a) (-(float)(a))
#define _ctof(a) ((float)(a))
#define _utof(a) ((float)(a))
#define _stof(a) ((float)(a))
#define ctof(a) ((float)(a))
#define itof(a) ((float)(a))
#define ftoi(a) ((int)(a))
char* _ftostr(char *b, float a)
{
sprintf(b, "%f", a);
return b;
}
#define ftoa(a, b) _ftostr((a), (b))
#endif /* __CC65__ */
#endif /* _FLOAT_H_ */