1 /*****************************************************************************/
5 /* Floating point support */
9 /* (C) 2008 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
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.
55 /*****************************************************************************/
57 /*****************************************************************************/
61 #define F_SIZE sizeof(float)
62 #define D_SIZE sizeof(float) /* NOT double! */
66 /*****************************************************************************/
68 /*****************************************************************************/
72 size_t FP_F_Size (void)
73 /* Return the size of the data type float */
80 unsigned char* FP_F_Data (Float Val)
81 /* Return the raw data of a float in a malloc'ed buffer. Free after use. */
83 return memcpy (xmalloc (F_SIZE), &Val.V, F_SIZE);
88 Float FP_F_Make (float Val)
89 /* Make a floating point variable from a float value */
98 Float FP_F_FromInt (long Val)
99 /* Convert an integer into a floating point variable */
108 float FP_F_ToFloat (Float Val)
109 /* Convert a Float into a native float */
116 Float FP_F_Add (Float Left, Float Right)
120 D.V = Left.V + Right.V;
126 Float FP_F_Sub (Float Left, Float Right)
127 /* Subtract two floats */
130 D.V = Left.V - Right.V;
136 Float FP_F_Mul (Float Left, Float Right)
137 /* Multiplicate two floats */
140 D.V = Left.V * Right.V;
146 Float FP_F_Div (Float Left, Float Right)
147 /* Divide two floats */
150 D.V = Left.V / Right.V;
156 size_t FP_D_Size (void)
157 /* Return the size of the data type double */
164 unsigned char* FP_D_Data (Double Val)
165 /* Return the raw data of a double in a malloc'ed buffer. Free after use. */
167 float F = (float) Val.V;
168 return memcpy (xmalloc (F_SIZE), &F, F_SIZE);
173 Double FP_D_Make (double Val)
174 /* Make a floating point variable from a float value */
184 Double FP_D_FromInt (long Val)
185 /* Convert an integer into a floating point variable */
194 double FP_D_ToFloat (Double Val)
195 /* Convert a Double into a native double */
202 Double FP_D_Add (Double Left, Double Right)
206 D.V = Left.V + Right.V;
212 Double FP_D_Sub (Double Left, Double Right)
213 /* Subtract two floats */
216 D.V = Left.V - Right.V;
222 Double FP_D_Mul (Double Left, Double Right)
223 /* Multiplicate two floats */
226 D.V = Left.V * Right.V;
232 Double FP_D_Div (Double Left, Double Right)
233 /* Divide two floats */
236 D.V = Left.V / Right.V;