6 #include "cln/number.h"
12 #define underflow_allowed() (! cl_inhibit_floating_point_underflow)
15 // For all floating-point formats:
16 // Sign s, Exponent e, Mantissa mk-1,...,m0
17 // represents the number (-1)^s * 2^(e-_EXP_MID) * [0 . 1 mk-1 ... m0]
18 // e=0 represents the number 0, always with sign s=0 (and mantissa =0).
19 // _exp_low and _exp_high are (inclusive) bounds for e.
20 // Bits for Sign s Exponent e Mantissa m (= k)
24 // LF 1 32 or 64 intDsize*n >= 53
27 // Konversionen ohne Rundung:
29 // cl_SF_to_FF(x) wandelt ein Short-Float x in ein Single-Float um.
30 extern const cl_FF cl_SF_to_FF (const cl_SF& x);
32 // cl_SF_to_DF(x) wandelt ein Short-Float x in ein Double-Float um.
33 extern const cl_DF cl_SF_to_DF (const cl_SF& x);
35 // cl_SF_to_LF(x,len) wandelt ein Short-Float x in ein Long-Float mit len Digits um.
36 // > uintC len: gewünschte Anzahl Digits, >=LF_minlen
37 extern const cl_LF cl_SF_to_LF (const cl_SF& x, uintC len);
39 // cl_FF_to_DF(x) wandelt ein Single-Float x in ein Double-Float um.
40 extern const cl_DF cl_FF_to_DF (const cl_FF& x);
42 // cl_FF_to_LF(x,len) wandelt ein Single-Float x in ein Long-Float mit len Digits um.
43 // > uintC len: gewünschte Anzahl Digits, >=LF_minlen
44 extern const cl_LF cl_FF_to_LF (const cl_FF& x, uintC len);
46 // cl_DF_to_LF(x,len) wandelt ein Double-Float x in ein Long-Float mit len Digits um.
47 // > uintC len: gewünschte Anzahl Digits, >=LF_minlen
48 extern const cl_LF cl_DF_to_LF (const cl_DF& x, uintC len);
51 // Konversionen mit Rundung:
53 // cl_FF_to_SF(x) wandelt ein Single-Float x in ein Short-Float um.
54 extern const cl_SF cl_FF_to_SF (const cl_FF& x);
56 // cl_DF_to_SF(x) wandelt ein Double-Float x in ein Short-Float um.
57 extern const cl_SF cl_DF_to_SF (const cl_DF& x);
59 // cl_LF_to_SF(x) wandelt ein Long-Float x in ein Short-Float um.
60 extern const cl_SF cl_LF_to_SF (const cl_LF& x);
62 // cl_DF_to_FF(x) wandelt ein Double-Float x in ein Single-Float um.
63 extern const cl_FF cl_DF_to_FF (const cl_DF& x);
65 // cl_LF_to_FF(x) wandelt ein Long-Float x in ein Single-Float um.
66 extern const cl_FF cl_LF_to_FF (const cl_LF& x);
68 // cl_LF_to_DF(x) wandelt ein Long-Float x in ein Double-Float um.
69 extern const cl_DF cl_LF_to_DF (const cl_LF& x);
72 // Runtime typing support.
73 extern cl_class cl_class_ffloat;
74 extern cl_class cl_class_dfloat;
75 extern cl_class cl_class_lfloat;
78 inline bool longfloatp (const cl_F& x)
81 if (x.pointer_type() == &cl_class_lfloat)
86 // Macro: verteilt je nach Float-Typ eines Floats x auf 4 Statements.
87 // floattypecase(x, SF_statement,FF_statement,DF_statement,LF_statement);
88 // x sollte eine Variable sein.
89 #ifdef CL_WIDE_POINTERS
90 #define floattypecase(x, SF_statement,FF_statement,DF_statement,LF_statement) \
91 if (!(x).pointer_p()) \
92 switch ((x).nonpointer_tag()) \
93 { case cl_SF_tag: { SF_statement } break; \
94 case cl_FF_tag: { FF_statement } break; \
98 if ((x).pointer_type() == &cl_class_dfloat) { DF_statement } \
99 else if ((x).pointer_type() == &cl_class_lfloat) { LF_statement } \
103 #define floattypecase(x, SF_statement,FF_statement,DF_statement,LF_statement) \
104 if (!(x).pointer_p()) \
105 switch ((x).nonpointer_tag()) \
106 { case cl_SF_tag: { SF_statement } break; \
107 default: NOTREACHED \
110 if ((x).pointer_type() == &cl_class_ffloat) { FF_statement } \
111 else if ((x).pointer_type() == &cl_class_dfloat) { DF_statement } \
112 else if ((x).pointer_type() == &cl_class_lfloat) { LF_statement } \
117 // Macro: verteilt je nach Float-Typ eines Floats x auf 4 Statements,
118 // die x vom jeweiligen Float-Typ benutzen dürfen.
119 // floatcase(x, SF_statement,FF_statement,DF_statement,LF_statement);
120 // x sollte eine Variable sein.
121 #define floatcase(x, SF_statement,FF_statement,DF_statement,LF_statement) \
123 , var cl_SF& __tmp = *(cl_SF*)&x; var cl_SF& x = __tmp; SF_statement \
124 , var cl_FF& __tmp = *(cl_FF*)&x; var cl_FF& x = __tmp; FF_statement \
125 , var cl_DF& __tmp = *(cl_DF*)&x; var cl_DF& x = __tmp; DF_statement \
126 , var cl_LF& __tmp = *(cl_LF*)&x; var cl_LF& x = __tmp; LF_statement \
130 // GEN_F_OP1(arg1,F_OP,ergebnis_zuweisung)
131 // generates the body of a float operation with one argument.
132 // LF_OP is executed once the argument has been converted to its exact
134 #define GEN_F_OP1(arg1,F_OP,ergebnis_zuweisung) \
137 , /* SF */ ergebnis_zuweisung F_OP(arg1); \
138 , /* FF */ ergebnis_zuweisung F_OP(arg1); \
139 , /* DF */ ergebnis_zuweisung F_OP(arg1); \
140 , /* LF */ ergebnis_zuweisung F_OP(arg1); \
145 // GEN_F_OP2(arg1,arg2,F_OP,r,s,ergebnis_zuweisung)
146 // generates the body of a float operation with two arguments.
147 // F_OP is executed once both arguments have been converted to the same
148 // float format (the longer one of arg1 and arg2). The r results are then
149 // converted the shorter of the two float formats. (r = 0,1,2.)
150 // s = 0,1. s=0 means the LF operation needs two long-floats of the same size.
151 // s=1 means they may be of different sizes.
152 #define GEN_F_OP2(arg1,arg2,F_OP,r,s,ergebnis_zuweisung) \
158 ergebnis_zuweisung CONCAT(NOMAP,r)(SF, \
161 ergebnis_zuweisung CONCAT(MAP,r)(FF,cl_FF_to_SF,\
162 F_OP(cl_SF_to_FF(arg1),arg2) ); \
164 ergebnis_zuweisung CONCAT(MAP,r)(DF,cl_DF_to_SF,\
165 F_OP(cl_SF_to_DF(arg1),arg2) ); \
167 ergebnis_zuweisung CONCAT(MAP,r)(LF,cl_LF_to_SF,\
168 F_OP(cl_SF_to_LF(arg1,CONCAT(LFlen,s)(arg2)),arg2) ); \
173 ergebnis_zuweisung CONCAT(MAP,r)(FF,cl_FF_to_SF,\
174 F_OP(arg1,cl_SF_to_FF(arg2)) ); \
176 ergebnis_zuweisung CONCAT(NOMAP,r)(FF, \
179 ergebnis_zuweisung CONCAT(MAP,r)(DF,cl_DF_to_FF,\
180 F_OP(cl_FF_to_DF(arg1),arg2) ); \
182 ergebnis_zuweisung CONCAT(MAP,r)(LF,cl_LF_to_FF,\
183 F_OP(cl_FF_to_LF(arg1,CONCAT(LFlen,s)(arg2)),arg2) ); \
188 ergebnis_zuweisung CONCAT(MAP,r)(DF,cl_DF_to_SF,\
189 F_OP(arg1,cl_SF_to_DF(arg2)) ); \
191 ergebnis_zuweisung CONCAT(MAP,r)(DF,cl_DF_to_FF,\
192 F_OP(arg1,cl_FF_to_DF(arg2)) ); \
194 ergebnis_zuweisung CONCAT(NOMAP,r)(DF, \
197 ergebnis_zuweisung CONCAT(MAP,r)(LF,cl_LF_to_DF,\
198 F_OP(cl_DF_to_LF(arg1,CONCAT(LFlen,s)(arg2)),arg2) ); \
203 ergebnis_zuweisung CONCAT(MAP,r)(LF,cl_LF_to_SF,\
204 F_OP(arg1,cl_SF_to_LF(arg2,CONCAT(LFlen,s)(arg1))) ); \
206 ergebnis_zuweisung CONCAT(MAP,r)(LF,cl_LF_to_FF,\
207 F_OP(arg1,cl_FF_to_LF(arg2,CONCAT(LFlen,s)(arg1))) ); \
209 ergebnis_zuweisung CONCAT(MAP,r)(LF,cl_LF_to_DF,\
210 F_OP(arg1,cl_DF_to_LF(arg2,CONCAT(LFlen,s)(arg1))) ); \
212 GEN_LF_OP2_AUX(arg1,arg2,F_OP,r,s,ergebnis_zuweisung) \
216 #define GEN_LF_OP2_AUX(arg1,arg2,F_OP,r,s,ergebnis_zuweisung) \
217 CONCAT(GEN_LF_OP2_AUX,s)(arg1,arg2,F_OP,r,ergebnis_zuweisung)
218 #define GEN_LF_OP2_AUX0(arg1,arg2,F_OP,r,ergebnis_zuweisung) \
219 var uintC len1 = TheLfloat(arg1)->len; \
220 var uintC len2 = TheLfloat(arg2)->len; \
221 if (len1 == len2) /* gleich -> direkt ausführen */ \
222 { ergebnis_zuweisung CONCAT(NOMAP,r) (LF, F_OP(arg1,arg2)); } \
223 elif (len1 > len2) /* -> arg2 auf die Länge von arg1 bringen */ \
224 { ergebnis_zuweisung CONCAT(MAP,r) (LF, LF_shorten_len2, \
225 F_OP(arg1,extend(arg2,len1)) ); \
227 else /* (len1 < len2) -> arg1 auf die Länge von arg2 bringen */ \
228 { ergebnis_zuweisung CONCAT(MAP,r) (LF, LF_shorten_len1, \
229 F_OP(extend(arg1,len2),arg2) ); \
231 #define LF_shorten_len1(arg) shorten(arg,len1)
232 #define LF_shorten_len2(arg) shorten(arg,len2)
233 #define GEN_LF_OP2_AUX1(arg1,arg2,F_OP,r,ergebnis_zuweisung) \
234 ergebnis_zuweisung CONCAT(NOMAP,r) (LF, F_OP(arg1,arg2));
236 #define NOMAP0(F,EXPR) EXPR
237 #define NOMAP1(F,EXPR) EXPR
238 #define MAP0(F,FN,EXPR) EXPR
239 #define MAP1(F,FN,EXPR) FN(EXPR)
241 #define LFlen0(arg) TheLfloat(arg)->len
242 #define LFlen1(arg) LF_minlen
245 // cl_F_extendsqrt(x) erweitert die Genauigkeit eines Floats x um eine Stufe
246 // SF -> FF -> DF -> LF(4) -> LF(5) -> LF(6) -> ...
247 // Ein Float mit d Mantissenbits wird so zu einem Float mit
248 // mindestens d+sqrt(d)+2 Mantissenbits.
249 extern const cl_F cl_F_extendsqrt (const cl_F& x);
251 // cl_F_extendsqrtx(x) erweitert die Genauigkeit eines Floats x um eine Stufe
252 // SF -> FF -> DF -> LF(4) -> LF(5) -> LF(6) -> ...
253 // Ein Float mit d Mantissenbits und l Exponentenbits wird so zu einem Float
254 // mit mindestens d+sqrt(d)+2+(l-1) Mantissenbits.
255 extern const cl_F cl_F_extendsqrtx (const cl_F& x);
257 // cl_F_shortenrelative(x,y) tries to reduce the size of x, such that one
258 // wouldn't notice it when adding x to y. y must be /= 0. More precisely,
259 // this returns a float approximation of x, such that 1 ulp(x) < 1 ulp(y).
260 extern const cl_F cl_F_shortenrelative (const cl_F& x, const cl_F& y);
263 // Macro: dispatches according to a float_format_t value.
264 // floatformatcase(value, SF_statement,FF_statement,DF_statement,LF_statement)
265 // LF_statement darf auf `len' zugreifen, die zu `value' korrespondierende
266 // Mantissenlänge (gemessen in Digits).
267 #define floatformatcase(value, SF_statement,FF_statement,DF_statement,LF_statement) \
268 { if ((value) <= float_format_sfloat) { SF_statement } \
269 elif ((value) <= float_format_ffloat) { FF_statement } \
270 elif ((value) <= float_format_dfloat) { DF_statement } \
271 else { var uintC len = ceiling((uintC)(value),intDsize); LF_statement } \