arith.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* arith.h Copyright (C) Codemist Ltd, 1990-2002 */
  2. /*
  3. * This code may be used and modified, and redistributed in binary
  4. * or source form, subject to the "CCL Public License", which should
  5. * accompany it. This license is a variant on the BSD license, and thus
  6. * permits use of code derived from this in either open and commercial
  7. * projects: but it does require that updates to this code be made
  8. * available back to the originators of the package.
  9. * Before merging other code in with this or linking this code
  10. * with other packages or libraries please check that the license terms
  11. * of the other material are compatible with those of this.
  12. */
  13. /* Signature: 236e0fe6 08-Apr-2002 */
  14. #ifndef header_arith_h
  15. #define header_arith_h 1
  16. #define TWO_32 4294967296.0 /* 2^32 */
  17. #define TWO_31 2147483648.0 /* 2^31 */
  18. #define TWO_24 16777216.0 /* 2^24 */
  19. #define TWO_22 4194304.0 /* 2^22 */
  20. #define TWO_21 2097152.0 /* 2^21 */
  21. #define TWO_20 1048576.0 /* 2^20 */
  22. #define M2_31_1 -2147483649.0 /* -(2^31 + 1) */
  23. #define _pi 3.14159265358979323846
  24. #define _half_pi 1.57079632679489661923
  25. #define boole_clr 0
  26. #define boole_and 1
  27. #define boole_andc2 2
  28. #define boole_1 3
  29. #define boole_andc1 4
  30. #define boole_2 5
  31. #define boole_xor 6
  32. #define boole_ior 7
  33. #define boole_nor 8
  34. #define boole_eqv 9
  35. #define boole_c2 10
  36. #define boole_orc2 11
  37. #define boole_c1 12
  38. #define boole_orc1 13
  39. #define boole_nand 14
  40. #define boole_set 15
  41. /*
  42. * Bignums are represented as vectors of digits, where each digit
  43. * uses 31 bits, and all but the most significant digit are unsigned
  44. * (and thus do not use the 0x80000000L bit). The most significant
  45. * digit of a bignum is a signed 2-s complement value in 31 bits that
  46. * has been sign extended into the 0x80000000L bit, and thus its top
  47. * two bits (in the 32 bit word) will be either '00' or '11'.
  48. * NOTE that even on a 64-bit machine I will work with 32-bit values
  49. * as digits in bignums.
  50. */
  51. #define top_bit_set(n) (((int32)(n)) < 0)
  52. #define top_bit(n) (((unsigned32)(n)) >> 31)
  53. #define set_top_bit(n) ((n) | (unsigned32)0x80000000)
  54. #define clear_top_bit(n) ((n) & 0x7fffffff)
  55. #define signed_overflow(n) top_bit_set((n) ^ (((int32)(n))<<1))
  56. #ifdef MULDIV64
  57. /*
  58. * Here I do some arithmetic in-line. In the following macros I need to
  59. * take care that the names used for local variables do not clash with
  60. * those used in the body of the code. Hence the names r64 and c64, which
  61. * I must agree not to use elsewhere. Note also the "do {} while (0)" idiom
  62. * to avoid nasty problems with C syntax and the need for semicolons.
  63. */
  64. #define IMULTIPLY 1 /* External function not needed */
  65. #define Dmultiply(hi, lo, a, b, c) \
  66. do { unsigned64 r64 = (unsigned64)(a) * (unsigned64)(b) + \
  67. (unsigned32)(c); \
  68. (lo) = 0x7fffffffu & (unsigned32)r64; \
  69. (hi) = (unsigned32)(r64 >> 31); } while (0)
  70. #define IDIVIDE 1
  71. #define Ddivide(r, q, a, b, c) \
  72. do { unsigned64 r64 = (((unsigned64)(a)) << 31) | (unsigned64)(b); \
  73. unsigned64 c64 = (unsigned64)(unsigned32)(c); \
  74. q = (unsigned32)(r64 / c64); \
  75. r = (unsigned32)(r64 % c64); } while (0)
  76. #define Ddiv10_9(r, q, a, b) Ddivide(r, q, a, b, 1000000000u)
  77. #else
  78. #define Dmultiply(hi, lo, a, b, c) ((hi) = Imultiply(&(lo), (a), (b), (c)))
  79. #define Ddivide(r, q, a, b, c) ((r) = Idivide(&(q), (a), (b), (c)))
  80. #define Ddiv10_9(r, q, a, b) ((r) = Idiv10_9(&(q), (a), (b)))
  81. #endif
  82. #define fix_mask (-0x08000000)
  83. #define fixnum_minusp(a) ((int32)(a) < 0)
  84. #define bignum_minusp(a) \
  85. ((int32)bignum_digits(a)[((bignum_length(a)-CELL)/4)-1]<0)
  86. extern Lisp_Object negateb(Lisp_Object);
  87. extern Lisp_Object copyb(Lisp_Object);
  88. extern Lisp_Object negate(Lisp_Object);
  89. extern Lisp_Object plus2(Lisp_Object a, Lisp_Object b);
  90. extern Lisp_Object difference2(Lisp_Object a, Lisp_Object b);
  91. extern Lisp_Object times2(Lisp_Object a, Lisp_Object b);
  92. extern Lisp_Object quot2(Lisp_Object a, Lisp_Object b);
  93. extern Lisp_Object CLquot2(Lisp_Object a, Lisp_Object b);
  94. extern Lisp_Object quotbn(Lisp_Object a, int32 n);
  95. extern Lisp_Object quotbn1(Lisp_Object a, int32 n);
  96. extern Lisp_Object quotbb(Lisp_Object a, Lisp_Object b);
  97. extern Lisp_Object Cremainder(Lisp_Object a, Lisp_Object b);
  98. extern Lisp_Object rembi(Lisp_Object a, Lisp_Object b);
  99. extern Lisp_Object rembb(Lisp_Object a, Lisp_Object b);
  100. extern Lisp_Object shrink_bignum(Lisp_Object a, int32 lena);
  101. extern Lisp_Object modulus(Lisp_Object a, Lisp_Object b);
  102. extern Lisp_Object rational(Lisp_Object a);
  103. extern Lisp_Object rationalize(Lisp_Object a);
  104. extern Lisp_Object lcm(Lisp_Object a, Lisp_Object b);
  105. extern Lisp_Object lengthen_by_one_bit(Lisp_Object a, int32 msd);
  106. extern CSLbool numeq2(Lisp_Object a, Lisp_Object b);
  107. extern CSLbool zerop(Lisp_Object a);
  108. extern CSLbool onep(Lisp_Object a);
  109. extern CSLbool minusp(Lisp_Object a);
  110. extern CSLbool plusp(Lisp_Object a);
  111. extern CSLbool lesspbd(Lisp_Object a, double b);
  112. extern CSLbool lessprd(Lisp_Object a, double b);
  113. extern CSLbool lesspdb(double a, Lisp_Object b);
  114. extern CSLbool lesspdr(double a, Lisp_Object b);
  115. extern Lisp_Object make_one_word_bignum(int32 n);
  116. extern Lisp_Object make_two_word_bignum(int32 a, unsigned32 b);
  117. extern Lisp_Object make_n_word_bignum(int32 a1, unsigned32 a2,
  118. unsigned32 a3, int32 n);
  119. extern Lisp_Object make_sfloat(double d);
  120. extern double float_of_integer(Lisp_Object a);
  121. extern Lisp_Object add1(Lisp_Object p);
  122. extern Lisp_Object sub1(Lisp_Object p);
  123. extern Lisp_Object integerp(Lisp_Object p);
  124. extern double float_of_number(Lisp_Object a);
  125. extern Lisp_Object make_boxfloat(double a, int32 type);
  126. extern Lisp_Object make_complex(Lisp_Object r, Lisp_Object i);
  127. extern Lisp_Object make_ratio(Lisp_Object p, Lisp_Object q);
  128. extern Lisp_Object ash(Lisp_Object a, Lisp_Object b);
  129. extern Lisp_Object lognot(Lisp_Object a);
  130. extern Lisp_Object logior2(Lisp_Object a, Lisp_Object b);
  131. extern Lisp_Object logxor2(Lisp_Object a, Lisp_Object b);
  132. extern Lisp_Object logand2(Lisp_Object a, Lisp_Object b);
  133. extern Lisp_Object logeqv2(Lisp_Object a, Lisp_Object b);
  134. extern Lisp_Object rationalf(double d);
  135. extern int _reduced_exp(double, double *);
  136. extern CSLbool lesspbi(Lisp_Object a, Lisp_Object b);
  137. extern CSLbool lesspib(Lisp_Object a, Lisp_Object b);
  138. #ifdef COMMON
  139. typedef struct Complex
  140. {
  141. double real;
  142. double imag;
  143. } Complex;
  144. extern Complex MS_CDECL Cln(Complex a);
  145. extern Complex MS_CDECL Ccos(Complex a);
  146. extern Complex MS_CDECL Cexp(Complex a);
  147. extern Complex MS_CDECL Cpow(Complex a, Complex b);
  148. extern double MS_CDECL Cabs(Complex a);
  149. #endif
  150. #endif /* header_arith_h */
  151. /* end of arith.h */