You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
8.7 KiB

25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
  1. // Basic definitions of numbers
  2. #ifndef _CL_NUMBER_H
  3. #define _CL_NUMBER_H
  4. #include "cln/object.h"
  5. #include "cln/malloc.h"
  6. // Type hierachy:
  7. // Number (N) =
  8. // Real (R) =
  9. // Float (F) =
  10. // Short float (SF)
  11. // Single float (FF)
  12. // Double float (DF)
  13. // Long float (LF)
  14. // Rational (RA) =
  15. // Integer (I) =
  16. // Fixnum (FN)
  17. // Bignum (BN)
  18. // Ratio (RT)
  19. // Complex (C)
  20. // Constructors and assignment operators from C numeric types.
  21. #define CL_DEFINE_INT_CONSTRUCTOR(_class_,_type_) \
  22. inline _class_::_class_ (const _type_ wert) \
  23. { \
  24. word = cl_combine(cl_FN_tag,wert); \
  25. }
  26. #define CL_DEFINE_INT_CONSTRUCTORS(_class_) \
  27. CL_DEFINE_INT_CONSTRUCTOR(_class_, int) \
  28. CL_DEFINE_INT_CONSTRUCTOR(_class_, unsigned int)
  29. #define CL_DEFINE_INT_ASSIGNMENT_OPERATOR(_class_,_type_) \
  30. inline _class_& _class_::operator= (const _type_ wert) \
  31. { \
  32. cl_dec_refcount(*this); \
  33. word = cl_combine(cl_FN_tag,wert); \
  34. return *this; \
  35. }
  36. #define CL_DEFINE_INT_ASSIGNMENT_OPERATORS(_class_) \
  37. CL_DEFINE_INT_ASSIGNMENT_OPERATOR(_class_, int) \
  38. CL_DEFINE_INT_ASSIGNMENT_OPERATOR(_class_, unsigned int)
  39. #if (long_bitsize==32)
  40. // `long' == `sintL', `unsigned long' == `uintL'.
  41. #define CL_DEFINE_LONG_CONSTRUCTORS(_class_) \
  42. inline _class_::_class_ (const long wert) \
  43. { \
  44. extern cl_private_thing cl_I_constructor_from_L (sint32 wert); \
  45. pointer = cl_I_constructor_from_L(wert); \
  46. } \
  47. inline _class_::_class_ (const unsigned long wert) \
  48. { \
  49. extern cl_private_thing cl_I_constructor_from_UL (uint32 wert); \
  50. pointer = cl_I_constructor_from_UL(wert); \
  51. }
  52. #elif (long_bitsize==64)
  53. // `long' == `sintQ', `unsigned long' == `uintQ'.
  54. #define CL_DEFINE_LONG_CONSTRUCTORS(_class_) \
  55. inline _class_::_class_ (const long wert) \
  56. { \
  57. extern cl_private_thing cl_I_constructor_from_Q (sint64 wert); \
  58. pointer = cl_I_constructor_from_Q(wert); \
  59. } \
  60. inline _class_::_class_ (const unsigned long wert) \
  61. { \
  62. extern cl_private_thing cl_I_constructor_from_UQ (uint64 wert); \
  63. pointer = cl_I_constructor_from_UQ(wert); \
  64. }
  65. #endif
  66. #if (long_bitsize==32)
  67. // `long' == `sintL', `unsigned long' == `uintL'.
  68. #define CL_DEFINE_LONG_ASSIGNMENT_OPERATORS(_class_) \
  69. inline _class_& _class_::operator= (const long wert) \
  70. { \
  71. extern cl_private_thing cl_I_constructor_from_L (sint32 wert); \
  72. cl_dec_refcount(*this); \
  73. pointer = cl_I_constructor_from_L(wert); \
  74. return *this; \
  75. } \
  76. inline _class_& _class_::operator= (const unsigned long wert) \
  77. { \
  78. extern cl_private_thing cl_I_constructor_from_UL (uint32 wert); \
  79. cl_dec_refcount(*this); \
  80. pointer = cl_I_constructor_from_UL(wert); \
  81. return *this; \
  82. }
  83. #elif (long_bitsize==64)
  84. // `long' == `sintQ', `unsigned long' == `uintQ'.
  85. #define CL_DEFINE_LONG_ASSIGNMENT_OPERATORS(_class_) \
  86. inline _class_& _class_::operator= (const long wert) \
  87. { \
  88. extern cl_private_thing cl_I_constructor_from_Q (sint64 wert); \
  89. cl_dec_refcount(*this); \
  90. pointer = cl_I_constructor_from_Q(wert); \
  91. return *this; \
  92. } \
  93. inline _class_& _class_::operator= (const unsigned long wert) \
  94. { \
  95. extern cl_private_thing cl_I_constructor_from_UQ (uint64 wert); \
  96. cl_dec_refcount(*this); \
  97. pointer = cl_I_constructor_from_UQ(wert); \
  98. return *this; \
  99. }
  100. #endif
  101. namespace cln {
  102. // Constructors and assignment operators from C numeric types.
  103. // from `float':
  104. union ffloatjanus;
  105. extern cl_private_thing cl_float_to_FF_pointer (const union ffloatjanus& val);
  106. #define CL_DEFINE_FLOAT_CONSTRUCTOR(_class_) \
  107. inline _class_ :: _class_ (const float x) \
  108. { \
  109. pointer = cl_float_to_FF_pointer(*(const union ffloatjanus *)&x); \
  110. } \
  111. inline _class_& _class_::operator= (const float x) \
  112. { \
  113. cl_dec_refcount(*this); \
  114. pointer = cl_float_to_FF_pointer(*(const union ffloatjanus *)&x); \
  115. return *this; \
  116. }
  117. // from `double':
  118. union dfloatjanus;
  119. extern struct cl_heap_dfloat * cl_double_to_DF_pointer (const union dfloatjanus& val);
  120. #define CL_DEFINE_DOUBLE_CONSTRUCTOR(_class_) \
  121. inline _class_::_class_ (const double x) \
  122. { \
  123. pointer = cl_double_to_DF_pointer(*(const union dfloatjanus *)&x); \
  124. } \
  125. inline _class_& _class_::operator= (const double x) \
  126. { \
  127. cl_dec_refcount(*this); \
  128. pointer = cl_double_to_DF_pointer(*(const union dfloatjanus *)&x); \
  129. return *this; \
  130. }
  131. // Abstract class of all numbers.
  132. class cl_number : public cl_gcobject {
  133. public:
  134. // Default constructor. (Used for objects with no initializer.)
  135. cl_number ();
  136. // Copy constructor. (Used for function argument passing and function
  137. // return value, and of course for objects with initializers of the same type.)
  138. cl_number (const cl_number& x);
  139. // Converters. (Used for function argument passing and function return values.)
  140. // Assignment operators. (Used for assignments.)
  141. cl_number& operator= (const cl_number&);
  142. // Constructors and assignment operators from C numeric types.
  143. cl_number (const int); // |argument| must be < 2^29
  144. cl_number (const unsigned int); // argument must be < 2^29
  145. cl_number (const long);
  146. cl_number (const unsigned long);
  147. cl_number (const float);
  148. cl_number (const double);
  149. cl_number& operator= (const int); // |argument| must be < 2^29
  150. cl_number& operator= (const unsigned int); // argument must be < 2^29
  151. cl_number& operator= (const long);
  152. cl_number& operator= (const unsigned long);
  153. cl_number& operator= (const float);
  154. cl_number& operator= (const double);
  155. // Other constructors.
  156. // cl_number (const char *);
  157. // Private pointer manipulations.
  158. cl_number (cl_private_thing);
  159. };
  160. // Private constructors.
  161. inline cl_number::cl_number (cl_private_thing ptr) : cl_gcobject (ptr) {}
  162. // The assignment operators:
  163. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_number, cl_number)
  164. // The default constructors.
  165. inline cl_number::cl_number ()
  166. : cl_gcobject ((cl_private_thing) cl_combine(cl_FN_tag,0)) {}
  167. // The copy constructors.
  168. CL_DEFINE_COPY_CONSTRUCTOR2(cl_number,cl_gcobject)
  169. // Constructors and assignment operators from C numeric types.
  170. CL_DEFINE_INT_CONSTRUCTORS(cl_number)
  171. CL_DEFINE_INT_ASSIGNMENT_OPERATORS(cl_number)
  172. CL_DEFINE_LONG_CONSTRUCTORS(cl_number)
  173. CL_DEFINE_LONG_ASSIGNMENT_OPERATORS(cl_number)
  174. CL_DEFINE_FLOAT_CONSTRUCTOR(cl_number)
  175. CL_DEFINE_DOUBLE_CONSTRUCTOR(cl_number)
  176. // Conversion:
  177. //inline cl_N& cl_as_N (const cl_number& x)
  178. //{ return *(cl_N*)(&x); }
  179. // Hack section.
  180. // Conversions to subtypes without checking, template version:
  181. // the<cl_I>(x) converts x to a cl_I, without change of representation.
  182. template<class type>
  183. inline const type& the(const cl_number& x)
  184. {
  185. // check that sizeof(type)==sizeof(cl_number)
  186. typedef int assertion1 [1 - 2 * (sizeof(type) != sizeof(cl_number))];
  187. return *(const type *) &x;
  188. }
  189. // Conversions to subtypes without checking, macro version:
  190. // The(cl_I)(x) converts x to a cl_I, without change of representation.
  191. #define The(type) *(const type *) & cl_identity
  192. // This inline function is for type checking purposes only.
  193. inline const cl_number& cl_identity (const cl_number& x) { return x; }
  194. } // namespace cln
  195. // Conversions to subtypes:
  196. // As(cl_I)(x) returns x as a cl_I. It first checks that x is a cl_I
  197. // and then returns it without change of representation.
  198. #if 0 // no debug information
  199. #define As(type) as_##type
  200. #define CL_DEFINE_AS_CONVERSION(_class_) \
  201. extern const _class_& as_##_class_ (const cl_number& x); \
  202. inline const _class_& as_##_class_ (const _class_& x) { return x; }
  203. #else // Line number information for ease of debugging.
  204. #define As(type) as_##type cl_as_aux
  205. #define cl_as_aux(expr) (expr,__FILE__,__LINE__)
  206. #define CL_DEFINE_AS_CONVERSION(_class_) \
  207. extern const _class_& as_##_class_ (const cl_number& x, const char * filename, int line); \
  208. inline const _class_& as_##_class_ (const _class_& x, const char * filename, int line) { (void)filename; (void)line; return x; }
  209. #endif
  210. // Mutable(type,x);
  211. // x should be a variable `const type x' or `const type& x'.
  212. // This macro introduces a new variable `type& x' whose value can be
  213. // modified. Useful for modifying the argument of a function which takes
  214. // a `const type &x'.
  215. // Warning: To apply this to a function's formal parameter, a block { ... }
  216. // must be inserted.
  217. #define Mutable(type,x) \
  218. type __copied_##x = x; \
  219. type& x = __copied_##x;
  220. // DeclareType(type,x);
  221. // x should be a variable of some subtype of `cl_number'. type should be
  222. // a subtype of `cl_number'. A new variable of the given type is declared,
  223. // with name x and which refers to x (by reference, with const attribute).
  224. #define DeclareType(type,x) \
  225. const type& __tmp_##x = *(const type*) &x; \
  226. const type& x = __tmp_##x;
  227. #endif /* _CL_NUMBER_H */