1/*****************************************************************************/
2/* */
3/* fp.h */
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** BEWARE: This code will currently only work on little endian systems!
45*/
46
47
48
49#ifndef FP_H
50#define FP_H
51
52
53
54#include <stddef.h>
55
56
57
58/*****************************************************************************/
59/* Data */
60/*****************************************************************************/
61
62
63typedef union Float Float;
64union Float {
65 float V;
66};
67
68
69
70typedef union Double Double;
71union Double {
72 double V;
73};
74
75
76
77/*****************************************************************************/
78/* Code */
79/*****************************************************************************/
80
81
82
83size_t FP_F_Size (void);
84/* Return the size of the data type float */
85
86unsigned char* FP_F_Data (Float Val);
87/* Return the raw data of a float in a malloc'ed buffer. Free after use. */
88
89Float FP_F_Make (float Val);
90/* Make a floating point variable from a float value */
91
92Float FP_F_FromInt (long Val);
93/* Convert an integer into a floating point variable */
94
95float FP_F_ToFloat (Float Val);
96/* Convert a Float into a native float */
97
98Float FP_F_Add (Float Left, Float Right);
99/* Add two floats */
100
101Float FP_F_Sub (Float Left, Float Right);
102/* Subtract two floats */
103
104Float FP_F_Mul (Float Left, Float Right);
105/* Multiplicate two floats */
106
107Float FP_F_Div (Float Left, Float Right);
108/* Divide two floats */
109
110size_t FP_D_Size (void);
111/* Return the size of the data type double */
112
113unsigned char* FP_D_Data (Double Val);
114/* Return the raw data of a double in a malloc'ed buffer. Free after use. */
115
116Double FP_D_Make (double Val);
117/* Make a floating point variable from a float value */
118
119Double FP_D_FromInt (long Val);
120/* Convert an integer into a floating point variable */
121
122double FP_D_ToFloat (Double Val);
123/* Convert a Double into a native double */
124
125Double FP_D_Add (Double Left, Double Right);
126/* Add two floats */
127
128Double FP_D_Sub (Double Left, Double Right);
129/* Subtract two floats */
130
131Double FP_D_Mul (Double Left, Double Right);
132/* Multiplicate two floats */
133
134Double FP_D_Div (Double Left, Double Right);
135/* Divide two floats */
136
137
138
139/* End of fp.h */
140
141#endif
142