1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* fp.c */ |
4 | /* */ |
5 | /* Floating point support */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 2008 Ullrich von Bassewitz */ |
10 | /* Roemerstrasse 52 */ |
11 | /* D-70794 Filderstadt */ |
12 | /* EMail: uz@cc65.org */ |
13 | /* */ |
14 | /* */ |
15 | /* This software is provided 'as-is', without any expressed or implied */ |
16 | /* warranty. In no event will the authors be held liable for any damages */ |
17 | /* arising from the use of this software. */ |
18 | /* */ |
19 | /* Permission is granted to anyone to use this software for any purpose, */ |
20 | /* including commercial applications, and to alter it and redistribute it */ |
21 | /* freely, subject to the following restrictions: */ |
22 | /* */ |
23 | /* 1. The origin of this software must not be misrepresented; you must not */ |
24 | /* claim that you wrote the original software. If you use this software */ |
25 | /* in a product, an acknowledgment in the product documentation would be */ |
26 | /* appreciated but is not required. */ |
27 | /* 2. Altered source versions must be plainly marked as such, and must not */ |
28 | /* be misrepresented as being the original software. */ |
29 | /* 3. This notice may not be removed or altered from any source */ |
30 | /* distribution. */ |
31 | /* */ |
32 | /*****************************************************************************/ |
33 | |
34 | |
35 | |
36 | /* The compiler must use the same floating point arithmetic as the target |
37 | ** platform, otherwise expressions will yield a different result when |
38 | ** evaluated in the compiler or on the target platform. Since writing a target |
39 | ** and source library is almost double the work, we will at least add the |
40 | ** hooks here, and define functions for a plug in library that may be added |
41 | ** at a later time. Currently we use the builtin data types of the compiler |
42 | ** that translates cc65. |
43 | */ |
44 | |
45 | |
46 | |
47 | #include <string.h> |
48 | |
49 | /* common */ |
50 | #include "fp.h" |
51 | #include "xmalloc.h" |
52 | |
53 | |
54 | |
55 | /*****************************************************************************/ |
56 | /* Data */ |
57 | /*****************************************************************************/ |
58 | |
59 | |
60 | |
61 | #define F_SIZE sizeof(float) |
62 | #define D_SIZE sizeof(float) /* NOT double! */ |
63 | |
64 | |
65 | |
66 | /*****************************************************************************/ |
67 | /* Code */ |
68 | /*****************************************************************************/ |
69 | |
70 | |
71 | |
72 | size_t FP_F_Size (void) |
73 | /* Return the size of the data type float */ |
74 | { |
75 | return F_SIZE; |
76 | } |
77 | |
78 | |
79 | |
80 | unsigned char* FP_F_Data (Float Val) |
81 | /* Return the raw data of a float in a malloc'ed buffer. Free after use. */ |
82 | { |
83 | return memcpy (xmalloc (F_SIZE), &Val.V, F_SIZE); |
84 | } |
85 | |
86 | |
87 | |
88 | Float FP_F_Make (float Val) |
89 | /* Make a floating point variable from a float value */ |
90 | { |
91 | Float D; |
92 | D.V = Val; |
93 | return D; |
94 | } |
95 | |
96 | |
97 | |
98 | Float FP_F_FromInt (long Val) |
99 | /* Convert an integer into a floating point variable */ |
100 | { |
101 | Float D; |
102 | D.V = (float) Val; |
103 | return D; |
104 | } |
105 | |
106 | |
107 | |
108 | float FP_F_ToFloat (Float Val) |
109 | /* Convert a Float into a native float */ |
110 | { |
111 | return Val.V; |
112 | } |
113 | |
114 | |
115 | |
116 | Float FP_F_Add (Float Left, Float Right) |
117 | /* Add two floats */ |
118 | { |
119 | Float D; |
120 | D.V = Left.V + Right.V; |
121 | return D; |
122 | } |
123 | |
124 | |
125 | |
126 | Float FP_F_Sub (Float Left, Float Right) |
127 | /* Subtract two floats */ |
128 | { |
129 | Float D; |
130 | D.V = Left.V - Right.V; |
131 | return D; |
132 | } |
133 | |
134 | |
135 | |
136 | Float FP_F_Mul (Float Left, Float Right) |
137 | /* Multiplicate two floats */ |
138 | { |
139 | Float D; |
140 | D.V = Left.V * Right.V; |
141 | return D; |
142 | } |
143 | |
144 | |
145 | |
146 | Float FP_F_Div (Float Left, Float Right) |
147 | /* Divide two floats */ |
148 | { |
149 | Float D; |
150 | D.V = Left.V / Right.V; |
151 | return D; |
152 | } |
153 | |
154 | |
155 | |
156 | size_t FP_D_Size (void) |
157 | /* Return the size of the data type double */ |
158 | { |
159 | return D_SIZE; |
160 | } |
161 | |
162 | |
163 | |
164 | unsigned char* FP_D_Data (Double Val) |
165 | /* Return the raw data of a double in a malloc'ed buffer. Free after use. */ |
166 | { |
167 | float F = (float) Val.V; |
168 | return memcpy (xmalloc (F_SIZE), &F, F_SIZE); |
169 | } |
170 | |
171 | |
172 | |
173 | Double FP_D_Make (double Val) |
174 | /* Make a floating point variable from a float value */ |
175 | { |
176 | Double D; |
177 | D.V = Val; |
178 | return D; |
179 | } |
180 | |
181 | |
182 | |
183 | |
184 | Double FP_D_FromInt (long Val) |
185 | /* Convert an integer into a floating point variable */ |
186 | { |
187 | Double D; |
188 | D.V = Val; |
189 | return D; |
190 | } |
191 | |
192 | |
193 | |
194 | double FP_D_ToFloat (Double Val) |
195 | /* Convert a Double into a native double */ |
196 | { |
197 | return Val.V; |
198 | } |
199 | |
200 | |
201 | |
202 | Double FP_D_Add (Double Left, Double Right) |
203 | /* Add two floats */ |
204 | { |
205 | Double D; |
206 | D.V = Left.V + Right.V; |
207 | return D; |
208 | } |
209 | |
210 | |
211 | |
212 | Double FP_D_Sub (Double Left, Double Right) |
213 | /* Subtract two floats */ |
214 | { |
215 | Double D; |
216 | D.V = Left.V - Right.V; |
217 | return D; |
218 | } |
219 | |
220 | |
221 | |
222 | Double FP_D_Mul (Double Left, Double Right) |
223 | /* Multiplicate two floats */ |
224 | { |
225 | Double D; |
226 | D.V = Left.V * Right.V; |
227 | return D; |
228 | } |
229 | |
230 | |
231 | |
232 | Double FP_D_Div (Double Left, Double Right) |
233 | /* Divide two floats */ |
234 | { |
235 | Double D; |
236 | D.V = Left.V / Right.V; |
237 | return D; |
238 | } |
239 | |