PageRenderTime 75ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/cln-1.3.2/include/cln/ffloat.h

#
C Header | 329 lines | 183 code | 73 blank | 73 comment | 0 complexity | a8f625d7608558b7677160dd56f4dc63 MD5 | raw file
Possible License(s): GPL-2.0
  1. // Public single float operations.
  2. #ifndef _CL_FFLOAT_H
  3. #define _CL_FFLOAT_H
  4. #include "cln/number.h"
  5. #include "cln/ffloat_class.h"
  6. #include "cln/integer_class.h"
  7. #include "cln/float.h"
  8. namespace cln {
  9. CL_DEFINE_AS_CONVERSION(cl_FF)
  10. // Liefert zu einem Single-Float x : (- x), ein FF.
  11. extern const cl_FF operator- (const cl_FF& x);
  12. // compare(x,y) vergleicht zwei Single-Floats x und y.
  13. // Ergebnis: 0 falls x=y, +1 falls x>y, -1 falls x<y.
  14. extern cl_signean compare (const cl_FF& x, const cl_FF& y);
  15. // equal_hashcode(x) liefert einen equal-invarianten Hashcode f?r x.
  16. extern uint32 equal_hashcode (const cl_FF& x);
  17. inline bool operator== (const cl_FF& x, const cl_FF& y)
  18. { return compare(x,y)==0; }
  19. inline bool operator!= (const cl_FF& x, const cl_FF& y)
  20. { return compare(x,y)!=0; }
  21. inline bool operator<= (const cl_FF& x, const cl_FF& y)
  22. { return compare(x,y)<=0; }
  23. inline bool operator< (const cl_FF& x, const cl_FF& y)
  24. { return compare(x,y)<0; }
  25. inline bool operator>= (const cl_FF& x, const cl_FF& y)
  26. { return compare(x,y)>=0; }
  27. inline bool operator> (const cl_FF& x, const cl_FF& y)
  28. { return compare(x,y)>0; }
  29. // minusp(x) == (< x 0)
  30. extern bool minusp (const cl_FF& x);
  31. // zerop(x) stellt fest, ob ein Single-Float x = 0.0 ist.
  32. extern bool zerop (const cl_FF& x);
  33. // plusp(x) == (> x 0)
  34. extern bool plusp (const cl_FF& x);
  35. // Liefert zu zwei Single-Float x und y : (+ x y), ein FF.
  36. extern const cl_FF operator+ (const cl_FF& x, const cl_FF& y);
  37. // The C++ compiler may hesitate to do these conversions of its own:
  38. inline const cl_FF operator+ (const cl_FF& x, const float y)
  39. { return x + cl_FF(y); }
  40. inline const cl_FF operator+ (const float x, const cl_FF& y)
  41. { return cl_FF(x) + y; }
  42. // Liefert zu zwei Single-Float x und y : (- x y), ein FF.
  43. extern const cl_FF operator- (const cl_FF& x, const cl_FF& y);
  44. // The C++ compiler may hesitate to do these conversions of its own:
  45. inline const cl_FF operator- (const cl_FF& x, const float y)
  46. { return x - cl_FF(y); }
  47. inline const cl_FF operator- (const float x, const cl_FF& y)
  48. { return cl_FF(x) - y; }
  49. // Liefert zu zwei Single-Float x und y : (* x y), ein FF.
  50. extern const cl_FF operator* (const cl_FF& x, const cl_FF& y);
  51. // The C++ compiler may hesitate to do these conversions of its own:
  52. inline const cl_FF operator* (const cl_FF& x, const float y)
  53. { return x * cl_FF(y); }
  54. inline const cl_FF operator* (const float x, const cl_FF& y)
  55. { return cl_FF(x) * y; }
  56. // Liefert zu einem Single-Float x : (* x x), ein FF.
  57. inline const cl_FF square (const cl_FF& x) { return x*x; }
  58. // Liefert zu zwei Single-Float x und y : (/ x y), ein FF.
  59. extern const cl_FF operator/ (const cl_FF& x, const cl_FF& y);
  60. // The C++ compiler may hesitate to do these conversions of its own:
  61. inline const cl_FF operator/ (const cl_FF& x, const float y)
  62. { return x / cl_FF(y); }
  63. inline const cl_FF operator/ (const float x, const cl_FF& y)
  64. { return cl_FF(x) / y; }
  65. // Liefert zu einem Single-Float x>=0 : (sqrt x), ein FF.
  66. extern const cl_FF sqrt (const cl_FF& x);
  67. // recip(x) liefert (/ x), wo x ein Single-Float ist.
  68. extern const cl_FF recip (const cl_FF& x);
  69. // abs(x) liefert (abs x), wo x ein Single-Float ist.
  70. extern const cl_FF abs (const cl_FF& x);
  71. // (1+ x), wo x ein Single-Float ist.
  72. inline const cl_FF plus1 (const cl_FF& x)
  73. {
  74. extern const cl_FF cl_I_to_FF (const cl_I&);
  75. return x + cl_I_to_FF(cl_I(1));
  76. }
  77. // (1- x), wo x ein Single-Float ist.
  78. inline const cl_FF minus1 (const cl_FF& x)
  79. {
  80. extern const cl_FF cl_I_to_FF (const cl_I&);
  81. return x + cl_I_to_FF(cl_I(-1));
  82. }
  83. // ffloor(x) liefert (ffloor x), wo x ein FF ist.
  84. extern const cl_FF ffloor (const cl_FF& x);
  85. // fceiling(x) liefert (fceiling x), wo x ein FF ist.
  86. extern const cl_FF fceiling (const cl_FF& x);
  87. // ftruncate(x) liefert (ftruncate x), wo x ein FF ist.
  88. extern const cl_FF ftruncate (const cl_FF& x);
  89. // fround(x) liefert (fround x), wo x ein FF ist.
  90. extern const cl_FF fround (const cl_FF& x);
  91. // Return type for frounding operators.
  92. // x / y --> (q,r) with x = y*q+r.
  93. struct cl_FF_fdiv_t {
  94. cl_FF quotient;
  95. cl_FF remainder;
  96. // Constructor.
  97. cl_FF_fdiv_t () {}
  98. cl_FF_fdiv_t (const cl_FF& q, const cl_FF& r) : quotient(q), remainder(r) {}
  99. };
  100. // ffloor2(x) liefert (ffloor x), wo x ein FF ist.
  101. inline const cl_FF_fdiv_t ffloor2 (const cl_FF& x)
  102. { cl_FF q = ffloor(x); return cl_FF_fdiv_t(q,x-q); }
  103. // fceiling2(x) liefert (fceiling x), wo x ein FF ist.
  104. inline const cl_FF_fdiv_t fceiling2 (const cl_FF& x)
  105. { cl_FF q = fceiling(x); return cl_FF_fdiv_t(q,x-q); }
  106. // ftruncate2(x) liefert (ftruncate x), wo x ein FF ist.
  107. inline const cl_FF_fdiv_t ftruncate2 (const cl_FF& x)
  108. { cl_FF q = ftruncate(x); return cl_FF_fdiv_t(q,x-q); }
  109. // fround2(x) liefert (fround x), wo x ein FF ist.
  110. inline const cl_FF_fdiv_t fround2 (const cl_FF& x)
  111. { cl_FF q = fround(x); return cl_FF_fdiv_t(q,x-q); }
  112. // Return type for rounding operators.
  113. // x / y --> (q,r) with x = y*q+r.
  114. struct cl_FF_div_t {
  115. cl_I quotient;
  116. cl_FF remainder;
  117. // Constructor.
  118. cl_FF_div_t () {}
  119. cl_FF_div_t (const cl_I& q, const cl_FF& r) : quotient(q), remainder(r) {}
  120. };
  121. // floor2(x) liefert (floor x), wo x ein FF ist.
  122. inline const cl_FF_div_t floor2 (const cl_FF& x)
  123. {
  124. extern const cl_I cl_FF_to_I (const cl_FF& x);
  125. cl_FF q = ffloor(x);
  126. return cl_FF_div_t(cl_FF_to_I(q),x-q);
  127. }
  128. inline const cl_I floor1 (const cl_FF& x)
  129. {
  130. extern const cl_I cl_FF_to_I (const cl_FF& x);
  131. return cl_FF_to_I(ffloor(x));
  132. }
  133. // ceiling2(x) liefert (ceiling x), wo x ein FF ist.
  134. inline const cl_FF_div_t ceiling2 (const cl_FF& x)
  135. {
  136. extern const cl_I cl_FF_to_I (const cl_FF& x);
  137. cl_FF q = fceiling(x);
  138. return cl_FF_div_t(cl_FF_to_I(q),x-q);
  139. }
  140. inline const cl_I ceiling1 (const cl_FF& x)
  141. {
  142. extern const cl_I cl_FF_to_I (const cl_FF& x);
  143. return cl_FF_to_I(fceiling(x));
  144. }
  145. // truncate2(x) liefert (truncate x), wo x ein FF ist.
  146. inline const cl_FF_div_t truncate2 (const cl_FF& x)
  147. {
  148. extern const cl_I cl_FF_to_I (const cl_FF& x);
  149. cl_FF q = ftruncate(x);
  150. return cl_FF_div_t(cl_FF_to_I(q),x-q);
  151. }
  152. inline const cl_I truncate1 (const cl_FF& x)
  153. {
  154. extern const cl_I cl_FF_to_I (const cl_FF& x);
  155. return cl_FF_to_I(ftruncate(x));
  156. }
  157. // round2(x) liefert (round x), wo x ein FF ist.
  158. inline const cl_FF_div_t round2 (const cl_FF& x)
  159. {
  160. extern const cl_I cl_FF_to_I (const cl_FF& x);
  161. cl_FF q = fround(x);
  162. return cl_FF_div_t(cl_FF_to_I(q),x-q);
  163. }
  164. inline const cl_I round1 (const cl_FF& x)
  165. {
  166. extern const cl_I cl_FF_to_I (const cl_FF& x);
  167. return cl_FF_to_I(fround(x));
  168. }
  169. // floor2(x,y) liefert (floor x y).
  170. extern const cl_FF_div_t floor2 (const cl_FF& x, const cl_FF& y);
  171. inline const cl_I floor1 (const cl_FF& x, const cl_FF& y) { return floor1(x/y); }
  172. // ceiling2(x,y) liefert (ceiling x y).
  173. extern const cl_FF_div_t ceiling2 (const cl_FF& x, const cl_FF& y);
  174. inline const cl_I ceiling1 (const cl_FF& x, const cl_FF& y) { return ceiling1(x/y); }
  175. // truncate2(x,y) liefert (truncate x y).
  176. extern const cl_FF_div_t truncate2 (const cl_FF& x, const cl_FF& y);
  177. inline const cl_I truncate1 (const cl_FF& x, const cl_FF& y) { return truncate1(x/y); }
  178. // round2(x,y) liefert (round x y).
  179. extern const cl_FF_div_t round2 (const cl_FF& x, const cl_FF& y);
  180. inline const cl_I round1 (const cl_FF& x, const cl_FF& y) { return round1(x/y); }
  181. // Return type for decode_float:
  182. struct decoded_ffloat {
  183. cl_FF mantissa;
  184. cl_I exponent;
  185. cl_FF sign;
  186. // Constructor.
  187. decoded_ffloat () {}
  188. decoded_ffloat (const cl_FF& m, const cl_I& e, const cl_FF& s) : mantissa(m), exponent(e), sign(s) {}
  189. };
  190. // decode_float(x) liefert zu einem Float x: (decode-float x).
  191. // x = 0.0 liefert (0.0, 0, 1.0).
  192. // x = (-1)^s * 2^e * m liefert ((-1)^0 * 2^0 * m, e als Integer, (-1)^s).
  193. extern const decoded_ffloat decode_float (const cl_FF& x);
  194. // float_exponent(x) liefert zu einem Float x:
  195. // den Exponenten von (decode-float x).
  196. // x = 0.0 liefert 0.
  197. // x = (-1)^s * 2^e * m liefert e.
  198. extern sintE float_exponent (const cl_FF& x);
  199. // float_radix(x) liefert (float-radix x), wo x ein Float ist.
  200. inline sintL float_radix (const cl_FF& x)
  201. {
  202. (void)x; // unused x
  203. return 2;
  204. }
  205. // float_sign(x) liefert (float-sign x), wo x ein Float ist.
  206. extern const cl_FF float_sign (const cl_FF& x);
  207. // float_digits(x) liefert (float-digits x), wo x ein Float ist.
  208. // < ergebnis: ein uintC >0
  209. extern uintC float_digits (const cl_FF& x);
  210. // float_precision(x) liefert (float-precision x), wo x ein Float ist.
  211. // < ergebnis: ein uintC >=0
  212. extern uintC float_precision (const cl_FF& x);
  213. // integer_decode_float(x) liefert zu einem Float x: (integer-decode-float x).
  214. // x = 0.0 liefert (0, 0, 1).
  215. // x = (-1)^s * 2^e * m bei Float-Precision p liefert
  216. // (Mantisse 2^p * m als Integer, e-p als Integer, (-1)^s als Fixnum).
  217. extern const cl_idecoded_float integer_decode_float (const cl_FF& x);
  218. // scale_float(x,delta) liefert x*2^delta, wo x ein FF ist.
  219. extern const cl_FF scale_float (const cl_FF& x, sintC delta);
  220. extern const cl_FF scale_float (const cl_FF& x, const cl_I& delta);
  221. // max(x,y) liefert (max x y), wo x und y Floats sind.
  222. extern const cl_FF max (const cl_FF& x, const cl_FF& y);
  223. // min(x,y) liefert (min x y), wo x und y Floats sind.
  224. extern const cl_FF min (const cl_FF& x, const cl_FF& y);
  225. // signum(x) liefert (signum x), wo x ein Float ist.
  226. extern const cl_FF signum (const cl_FF& x);
  227. // Konversion zu einem C "float".
  228. extern float float_approx (const cl_FF& x);
  229. // Konversion zu einem C "double".
  230. extern double double_approx (const cl_FF& x);
  231. // This could be optimized to use in-place operations.
  232. inline cl_FF& operator+= (cl_FF& x, const cl_FF& y) { return x = x + y; }
  233. inline cl_FF& operator+= (cl_FF& x, const float y) { return x = x + y; }
  234. inline cl_FF& operator++ /* prefix */ (cl_FF& x) { return x = plus1(x); }
  235. inline void operator++ /* postfix */ (cl_FF& x, int dummy) { (void)dummy; x = plus1(x); }
  236. inline cl_FF& operator-= (cl_FF& x, const cl_FF& y) { return x = x - y; }
  237. inline cl_FF& operator-= (cl_FF& x, const float y) { return x = x - y; }
  238. inline cl_FF& operator-- /* prefix */ (cl_FF& x) { return x = minus1(x); }
  239. inline void operator-- /* postfix */ (cl_FF& x, int dummy) { (void)dummy; x = minus1(x); }
  240. inline cl_FF& operator*= (cl_FF& x, const cl_FF& y) { return x = x * y; }
  241. inline cl_FF& operator*= (cl_FF& x, const float y) { return x = x * y; }
  242. inline cl_FF& operator/= (cl_FF& x, const cl_FF& y) { return x = x / y; }
  243. inline cl_FF& operator/= (cl_FF& x, const float y) { return x = x / y; }
  244. /* */
  245. // Runtime typing support.
  246. extern cl_class cl_class_ffloat;
  247. #ifdef CL_WIDE_POINTERS
  248. CL_FORCE_LINK(cl_FF_classes_dummy, cl_class_ffloat)
  249. #endif
  250. // Debugging support.
  251. #ifdef CL_DEBUG
  252. extern int cl_FF_debug_module;
  253. CL_FORCE_LINK(cl_FF_debug_dummy, cl_FF_debug_module)
  254. #endif
  255. } // namespace cln
  256. #endif /* _CL_FFLOAT_H */