1 // Public single float operations.
6 #include "cln/number.h"
7 #include "cln/ffloat_class.h"
8 #include "cln/integer_class.h"
13 CL_DEFINE_AS_CONVERSION(cl_FF)
16 // Liefert zu einem Single-Float x : (- x), ein FF.
17 extern const cl_FF operator- (const cl_FF& x);
19 // compare(x,y) vergleicht zwei Single-Floats x und y.
20 // Ergebnis: 0 falls x=y, +1 falls x>y, -1 falls x<y.
21 extern cl_signean compare (const cl_FF& x, const cl_FF& y);
23 // equal_hashcode(x) liefert einen equal-invarianten Hashcode für x.
24 extern uint32 equal_hashcode (const cl_FF& x);
26 inline bool operator== (const cl_FF& x, const cl_FF& y)
27 { return compare(x,y)==0; }
28 inline bool operator!= (const cl_FF& x, const cl_FF& y)
29 { return compare(x,y)!=0; }
30 inline bool operator<= (const cl_FF& x, const cl_FF& y)
31 { return compare(x,y)<=0; }
32 inline bool operator< (const cl_FF& x, const cl_FF& y)
33 { return compare(x,y)<0; }
34 inline bool operator>= (const cl_FF& x, const cl_FF& y)
35 { return compare(x,y)>=0; }
36 inline bool operator> (const cl_FF& x, const cl_FF& y)
37 { return compare(x,y)>0; }
39 // minusp(x) == (< x 0)
40 extern bool minusp (const cl_FF& x);
42 // zerop(x) stellt fest, ob ein Single-Float x = 0.0 ist.
43 extern bool zerop (const cl_FF& x);
45 // plusp(x) == (> x 0)
46 extern bool plusp (const cl_FF& x);
48 // Liefert zu zwei Single-Float x und y : (+ x y), ein FF.
49 extern const cl_FF operator+ (const cl_FF& x, const cl_FF& y);
50 // The C++ compiler may hesitate to do these conversions of its own:
51 inline const cl_FF operator+ (const cl_FF& x, const float y)
52 { return x + cl_FF(y); }
53 inline const cl_FF operator+ (const float x, const cl_FF& y)
54 { return cl_FF(x) + y; }
56 // Liefert zu zwei Single-Float x und y : (- x y), ein FF.
57 extern const cl_FF operator- (const cl_FF& x, const cl_FF& y);
58 // The C++ compiler may hesitate to do these conversions of its own:
59 inline const cl_FF operator- (const cl_FF& x, const float y)
60 { return x - cl_FF(y); }
61 inline const cl_FF operator- (const float x, const cl_FF& y)
62 { return cl_FF(x) - y; }
64 // Liefert zu zwei Single-Float x und y : (* x y), ein FF.
65 extern const cl_FF operator* (const cl_FF& x, const cl_FF& y);
66 // The C++ compiler may hesitate to do these conversions of its own:
67 inline const cl_FF operator* (const cl_FF& x, const float y)
68 { return x * cl_FF(y); }
69 inline const cl_FF operator* (const float x, const cl_FF& y)
70 { return cl_FF(x) * y; }
72 // Liefert zu einem Single-Float x : (* x x), ein FF.
73 inline const cl_FF square (const cl_FF& x) { return x*x; }
75 // Liefert zu zwei Single-Float x und y : (/ x y), ein FF.
76 extern const cl_FF operator/ (const cl_FF& x, const cl_FF& y);
77 // The C++ compiler may hesitate to do these conversions of its own:
78 inline const cl_FF operator/ (const cl_FF& x, const float y)
79 { return x / cl_FF(y); }
80 inline const cl_FF operator/ (const float x, const cl_FF& y)
81 { return cl_FF(x) / y; }
83 // Liefert zu einem Single-Float x>=0 : (sqrt x), ein FF.
84 extern const cl_FF sqrt (const cl_FF& x);
86 // recip(x) liefert (/ x), wo x ein Single-Float ist.
87 extern const cl_FF recip (const cl_FF& x);
89 // abs(x) liefert (abs x), wo x ein Single-Float ist.
90 extern const cl_FF abs (const cl_FF& x);
93 // (1+ x), wo x ein Single-Float ist.
94 inline const cl_FF plus1 (const cl_FF& x)
96 extern const cl_FF cl_I_to_FF (const cl_I&);
97 return x + cl_I_to_FF(cl_I(1));
100 // (1- x), wo x ein Single-Float ist.
101 inline const cl_FF minus1 (const cl_FF& x)
103 extern const cl_FF cl_I_to_FF (const cl_I&);
104 return x + cl_I_to_FF(cl_I(-1));
108 // ffloor(x) liefert (ffloor x), wo x ein FF ist.
109 extern const cl_FF ffloor (const cl_FF& x);
111 // fceiling(x) liefert (fceiling x), wo x ein FF ist.
112 extern const cl_FF fceiling (const cl_FF& x);
114 // ftruncate(x) liefert (ftruncate x), wo x ein FF ist.
115 extern const cl_FF ftruncate (const cl_FF& x);
117 // fround(x) liefert (fround x), wo x ein FF ist.
118 extern const cl_FF fround (const cl_FF& x);
121 // Return type for frounding operators.
122 // x / y --> (q,r) with x = y*q+r.
123 struct cl_FF_fdiv_t {
128 cl_FF_fdiv_t (const cl_FF& q, const cl_FF& r) : quotient(q), remainder(r) {}
131 // ffloor2(x) liefert (ffloor x), wo x ein FF ist.
132 inline const cl_FF_fdiv_t ffloor2 (const cl_FF& x)
133 { cl_FF q = ffloor(x); return cl_FF_fdiv_t(q,x-q); }
135 // fceiling2(x) liefert (fceiling x), wo x ein FF ist.
136 inline const cl_FF_fdiv_t fceiling2 (const cl_FF& x)
137 { cl_FF q = fceiling(x); return cl_FF_fdiv_t(q,x-q); }
139 // ftruncate2(x) liefert (ftruncate x), wo x ein FF ist.
140 inline const cl_FF_fdiv_t ftruncate2 (const cl_FF& x)
141 { cl_FF q = ftruncate(x); return cl_FF_fdiv_t(q,x-q); }
143 // fround2(x) liefert (fround x), wo x ein FF ist.
144 inline const cl_FF_fdiv_t fround2 (const cl_FF& x)
145 { cl_FF q = fround(x); return cl_FF_fdiv_t(q,x-q); }
148 // Return type for rounding operators.
149 // x / y --> (q,r) with x = y*q+r.
155 cl_FF_div_t (const cl_I& q, const cl_FF& r) : quotient(q), remainder(r) {}
158 // floor2(x) liefert (floor x), wo x ein FF ist.
159 inline const cl_FF_div_t floor2 (const cl_FF& x)
161 extern const cl_I cl_FF_to_I (const cl_FF& x);
163 return cl_FF_div_t(cl_FF_to_I(q),x-q);
165 inline const cl_I floor1 (const cl_FF& x)
167 extern const cl_I cl_FF_to_I (const cl_FF& x);
168 return cl_FF_to_I(ffloor(x));
171 // ceiling2(x) liefert (ceiling x), wo x ein FF ist.
172 inline const cl_FF_div_t ceiling2 (const cl_FF& x)
174 extern const cl_I cl_FF_to_I (const cl_FF& x);
175 cl_FF q = fceiling(x);
176 return cl_FF_div_t(cl_FF_to_I(q),x-q);
178 inline const cl_I ceiling1 (const cl_FF& x)
180 extern const cl_I cl_FF_to_I (const cl_FF& x);
181 return cl_FF_to_I(fceiling(x));
184 // truncate2(x) liefert (truncate x), wo x ein FF ist.
185 inline const cl_FF_div_t truncate2 (const cl_FF& x)
187 extern const cl_I cl_FF_to_I (const cl_FF& x);
188 cl_FF q = ftruncate(x);
189 return cl_FF_div_t(cl_FF_to_I(q),x-q);
191 inline const cl_I truncate1 (const cl_FF& x)
193 extern const cl_I cl_FF_to_I (const cl_FF& x);
194 return cl_FF_to_I(ftruncate(x));
197 // round2(x) liefert (round x), wo x ein FF ist.
198 inline const cl_FF_div_t round2 (const cl_FF& x)
200 extern const cl_I cl_FF_to_I (const cl_FF& x);
202 return cl_FF_div_t(cl_FF_to_I(q),x-q);
204 inline const cl_I round1 (const cl_FF& x)
206 extern const cl_I cl_FF_to_I (const cl_FF& x);
207 return cl_FF_to_I(fround(x));
210 // floor2(x,y) liefert (floor x y).
211 extern const cl_FF_div_t floor2 (const cl_FF& x, const cl_FF& y);
212 inline const cl_I floor1 (const cl_FF& x, const cl_FF& y) { return floor1(x/y); }
214 // ceiling2(x,y) liefert (ceiling x y).
215 extern const cl_FF_div_t ceiling2 (const cl_FF& x, const cl_FF& y);
216 inline const cl_I ceiling1 (const cl_FF& x, const cl_FF& y) { return ceiling1(x/y); }
218 // truncate2(x,y) liefert (truncate x y).
219 extern const cl_FF_div_t truncate2 (const cl_FF& x, const cl_FF& y);
220 inline const cl_I truncate1 (const cl_FF& x, const cl_FF& y) { return truncate1(x/y); }
222 // round2(x,y) liefert (round x y).
223 extern const cl_FF_div_t round2 (const cl_FF& x, const cl_FF& y);
224 inline const cl_I round1 (const cl_FF& x, const cl_FF& y) { return round1(x/y); }
227 // Return type for decode_float:
228 struct decoded_ffloat {
234 decoded_ffloat (const cl_FF& m, const cl_I& e, const cl_FF& s) : mantissa(m), exponent(e), sign(s) {}
237 // decode_float(x) liefert zu einem Float x: (decode-float x).
238 // x = 0.0 liefert (0.0, 0, 1.0).
239 // x = (-1)^s * 2^e * m liefert ((-1)^0 * 2^0 * m, e als Integer, (-1)^s).
240 extern const decoded_ffloat decode_float (const cl_FF& x);
242 // float_exponent(x) liefert zu einem Float x:
243 // den Exponenten von (decode-float x).
244 // x = 0.0 liefert 0.
245 // x = (-1)^s * 2^e * m liefert e.
246 extern sintE float_exponent (const cl_FF& x);
248 // float_radix(x) liefert (float-radix x), wo x ein Float ist.
249 inline sintL float_radix (const cl_FF& x)
255 // float_sign(x) liefert (float-sign x), wo x ein Float ist.
256 extern const cl_FF float_sign (const cl_FF& x);
258 // float_digits(x) liefert (float-digits x), wo x ein Float ist.
259 // < ergebnis: ein uintC >0
260 extern uintC float_digits (const cl_FF& x);
262 // float_precision(x) liefert (float-precision x), wo x ein Float ist.
263 // < ergebnis: ein uintC >=0
264 extern uintC float_precision (const cl_FF& x);
267 // integer_decode_float(x) liefert zu einem Float x: (integer-decode-float x).
268 // x = 0.0 liefert (0, 0, 1).
269 // x = (-1)^s * 2^e * m bei Float-Precision p liefert
270 // (Mantisse 2^p * m als Integer, e-p als Integer, (-1)^s als Fixnum).
271 extern const cl_idecoded_float integer_decode_float (const cl_FF& x);
274 // scale_float(x,delta) liefert x*2^delta, wo x ein FF ist.
275 extern const cl_FF scale_float (const cl_FF& x, sintC delta);
276 extern const cl_FF scale_float (const cl_FF& x, const cl_I& delta);
279 // max(x,y) liefert (max x y), wo x und y Floats sind.
280 extern const cl_FF max (const cl_FF& x, const cl_FF& y);
282 // min(x,y) liefert (min x y), wo x und y Floats sind.
283 extern const cl_FF min (const cl_FF& x, const cl_FF& y);
285 // signum(x) liefert (signum x), wo x ein Float ist.
286 extern const cl_FF signum (const cl_FF& x);
289 // Konversion zu einem C "float".
290 extern float float_approx (const cl_FF& x);
292 // Konversion zu einem C "double".
293 extern double double_approx (const cl_FF& x);
296 // This could be optimized to use in-place operations.
297 inline cl_FF& operator+= (cl_FF& x, const cl_FF& y) { return x = x + y; }
298 inline cl_FF& operator+= (cl_FF& x, const float y) { return x = x + y; }
299 inline cl_FF& operator++ /* prefix */ (cl_FF& x) { return x = plus1(x); }
300 inline void operator++ /* postfix */ (cl_FF& x, int dummy) { (void)dummy; x = plus1(x); }
301 inline cl_FF& operator-= (cl_FF& x, const cl_FF& y) { return x = x - y; }
302 inline cl_FF& operator-= (cl_FF& x, const float y) { return x = x - y; }
303 inline cl_FF& operator-- /* prefix */ (cl_FF& x) { return x = minus1(x); }
304 inline void operator-- /* postfix */ (cl_FF& x, int dummy) { (void)dummy; x = minus1(x); }
305 inline cl_FF& operator*= (cl_FF& x, const cl_FF& y) { return x = x * y; }
306 inline cl_FF& operator*= (cl_FF& x, const float y) { return x = x * y; }
307 inline cl_FF& operator/= (cl_FF& x, const cl_FF& y) { return x = x / y; }
308 inline cl_FF& operator/= (cl_FF& x, const float y) { return x = x / y; }
314 // Runtime typing support.
315 extern cl_class cl_class_ffloat;
316 #ifdef CL_WIDE_POINTERS
317 CL_FORCE_LINK(cl_FF_classes_dummy, cl_class_ffloat)
321 // Debugging support.
323 extern int cl_FF_debug_module;
324 CL_FORCE_LINK(cl_FF_debug_dummy, cl_FF_debug_module)
329 #endif /* _CL_FFLOAT_H */