integers.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #ifndef SCM_INTEGERS_H
  2. #define SCM_INTEGERS_H
  3. /* Copyright 2021, 2022 Free Software Foundation, Inc.
  4. This file is part of Guile.
  5. Guile is free software: you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Guile is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with Guile. If not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #include "libguile/numbers.h"
  17. struct scm_bignum;
  18. static inline struct scm_bignum *
  19. scm_bignum (SCM x)
  20. {
  21. if (!SCM_BIGP (x)) abort ();
  22. return (struct scm_bignum *) SCM_UNPACK (x);
  23. }
  24. SCM_INTERNAL SCM scm_integer_from_mpz (const mpz_t n);
  25. SCM_INTERNAL void scm_integer_set_mpz_z (struct scm_bignum *z, mpz_t n);
  26. SCM_INTERNAL void scm_integer_init_set_mpz_z (struct scm_bignum *z, mpz_t n);
  27. SCM_INTERNAL int scm_is_integer_odd_i (scm_t_inum i);
  28. SCM_INTERNAL int scm_is_integer_odd_z (struct scm_bignum *z);
  29. SCM_INTERNAL SCM scm_integer_abs_i (scm_t_inum i);
  30. SCM_INTERNAL SCM scm_integer_abs_z (struct scm_bignum *z);
  31. #define DECLARE_QUOTIENT_OPERATORS(stem) \
  32. SCM_INTERNAL SCM scm_integer_##stem##_quotient_ii (scm_t_inum x, \
  33. scm_t_inum y); \
  34. SCM_INTERNAL SCM scm_integer_##stem##_quotient_iz (scm_t_inum x, \
  35. struct scm_bignum *y); \
  36. SCM_INTERNAL SCM scm_integer_##stem##_quotient_zi (struct scm_bignum *x, \
  37. scm_t_inum y); \
  38. SCM_INTERNAL SCM scm_integer_##stem##_quotient_zz (struct scm_bignum *x, \
  39. struct scm_bignum *y);
  40. #define DECLARE_REMAINDER_OPERATORS(stem) \
  41. SCM_INTERNAL SCM scm_integer_##stem##_remainder_ii (scm_t_inum x, \
  42. scm_t_inum y); \
  43. SCM_INTERNAL SCM scm_integer_##stem##_remainder_iz (scm_t_inum x, \
  44. struct scm_bignum *y); \
  45. SCM_INTERNAL SCM scm_integer_##stem##_remainder_zi (struct scm_bignum *x, \
  46. scm_t_inum y); \
  47. SCM_INTERNAL SCM scm_integer_##stem##_remainder_zz (struct scm_bignum *x, \
  48. struct scm_bignum *y);
  49. #define DECLARE_DIVIDE_OPERATORS(stem) \
  50. SCM_INTERNAL void scm_integer_##stem##_divide_ii (scm_t_inum x, \
  51. scm_t_inum y, \
  52. SCM *qp, SCM *rp); \
  53. SCM_INTERNAL void scm_integer_##stem##_divide_iz (scm_t_inum x, \
  54. struct scm_bignum *y, \
  55. SCM *qp, SCM *rp); \
  56. SCM_INTERNAL void scm_integer_##stem##_divide_zi (struct scm_bignum *x, \
  57. scm_t_inum y, \
  58. SCM *qp, SCM *rp); \
  59. SCM_INTERNAL void scm_integer_##stem##_divide_zz (struct scm_bignum *x, \
  60. struct scm_bignum *y, \
  61. SCM *qp, SCM *rp);
  62. #define DECLARE_DIVISION_OPERATORS(stem) \
  63. DECLARE_QUOTIENT_OPERATORS(stem); \
  64. DECLARE_REMAINDER_OPERATORS(stem); \
  65. DECLARE_DIVIDE_OPERATORS(stem)
  66. DECLARE_DIVISION_OPERATORS(floor);
  67. DECLARE_DIVISION_OPERATORS(ceiling);
  68. DECLARE_DIVISION_OPERATORS(truncate);
  69. DECLARE_DIVISION_OPERATORS(centered);
  70. DECLARE_DIVISION_OPERATORS(round);
  71. SCM_INTERNAL SCM scm_integer_gcd_ii (scm_t_inum x, scm_t_inum y);
  72. SCM_INTERNAL SCM scm_integer_gcd_zi (struct scm_bignum *x, scm_t_inum y);
  73. SCM_INTERNAL SCM scm_integer_gcd_zz (struct scm_bignum *x,
  74. struct scm_bignum *y);
  75. SCM_INTERNAL SCM scm_integer_lcm_ii (scm_t_inum x, scm_t_inum y);
  76. SCM_INTERNAL SCM scm_integer_lcm_zi (struct scm_bignum *x, scm_t_inum y);
  77. SCM_INTERNAL SCM scm_integer_lcm_zz (struct scm_bignum *x,
  78. struct scm_bignum *y);
  79. SCM_INTERNAL SCM scm_integer_logand_ii (scm_t_inum x, scm_t_inum y);
  80. SCM_INTERNAL SCM scm_integer_logand_zi (struct scm_bignum *x, scm_t_inum y);
  81. SCM_INTERNAL SCM scm_integer_logand_zz (struct scm_bignum *x,
  82. struct scm_bignum *y);
  83. SCM_INTERNAL SCM scm_integer_logior_ii (scm_t_inum x, scm_t_inum y);
  84. SCM_INTERNAL SCM scm_integer_logior_zi (struct scm_bignum *x, scm_t_inum y);
  85. SCM_INTERNAL SCM scm_integer_logior_zz (struct scm_bignum *x,
  86. struct scm_bignum *y);
  87. SCM_INTERNAL SCM scm_integer_logxor_ii (scm_t_inum x, scm_t_inum y);
  88. SCM_INTERNAL SCM scm_integer_logxor_zi (struct scm_bignum *x, scm_t_inum y);
  89. SCM_INTERNAL SCM scm_integer_logxor_zz (struct scm_bignum *x,
  90. struct scm_bignum *y);
  91. SCM_INTERNAL int scm_integer_logtest_ii (scm_t_inum x, scm_t_inum y);
  92. SCM_INTERNAL int scm_integer_logtest_zi (struct scm_bignum *x, scm_t_inum y);
  93. SCM_INTERNAL int scm_integer_logtest_zz (struct scm_bignum *x,
  94. struct scm_bignum *y);
  95. SCM_INTERNAL int scm_integer_logbit_ui (unsigned long bit, scm_t_inum n);
  96. SCM_INTERNAL int scm_integer_logbit_uz (unsigned long bit,
  97. struct scm_bignum *n);
  98. SCM_INTERNAL SCM scm_integer_lognot_i (scm_t_inum n);
  99. SCM_INTERNAL SCM scm_integer_lognot_z (struct scm_bignum *n);
  100. SCM_INTERNAL SCM scm_integer_expt_ii (scm_t_inum n, scm_t_inum k);
  101. SCM_INTERNAL SCM scm_integer_expt_zi (struct scm_bignum *n, scm_t_inum k);
  102. SCM_INTERNAL SCM scm_integer_modulo_expt_nnn (SCM n, SCM k, SCM m);
  103. SCM_INTERNAL SCM scm_integer_lsh_iu (scm_t_inum n, unsigned long count);
  104. SCM_INTERNAL SCM scm_integer_lsh_zu (struct scm_bignum *n,
  105. unsigned long count);
  106. SCM_INTERNAL SCM scm_integer_floor_rsh_iu (scm_t_inum n, unsigned long count);
  107. SCM_INTERNAL SCM scm_integer_floor_rsh_zu (struct scm_bignum *n,
  108. unsigned long count);
  109. SCM_INTERNAL SCM scm_integer_round_rsh_iu (scm_t_inum n, unsigned long count);
  110. SCM_INTERNAL SCM scm_integer_round_rsh_zu (struct scm_bignum *n,
  111. unsigned long count);
  112. SCM_INTERNAL SCM scm_integer_bit_extract_i (scm_t_inum n, unsigned long start,
  113. unsigned long bits);
  114. SCM_INTERNAL SCM scm_integer_bit_extract_z (struct scm_bignum *n,
  115. unsigned long start,
  116. unsigned long bits);
  117. SCM_INTERNAL SCM scm_integer_logcount_i (scm_t_inum n);
  118. SCM_INTERNAL SCM scm_integer_logcount_z (struct scm_bignum *n);
  119. SCM_INTERNAL SCM scm_integer_length_i (scm_t_inum n);
  120. SCM_INTERNAL SCM scm_integer_length_z (struct scm_bignum *n);
  121. SCM_INTERNAL SCM scm_integer_to_string_i (scm_t_inum n, int base);
  122. SCM_INTERNAL SCM scm_integer_to_string_z (struct scm_bignum *n, int base);
  123. SCM_INTERNAL int scm_is_integer_equal_ir (scm_t_inum x, double y);
  124. SCM_INTERNAL int scm_is_integer_equal_ic (scm_t_inum x,
  125. double real, double imag);
  126. SCM_INTERNAL int scm_is_integer_equal_zz (struct scm_bignum *x,
  127. struct scm_bignum *y);
  128. SCM_INTERNAL int scm_is_integer_equal_zr (struct scm_bignum *x, double y);
  129. SCM_INTERNAL int scm_is_integer_equal_zc (struct scm_bignum *x,
  130. double real, double imag);
  131. SCM_INTERNAL int scm_is_integer_less_than_ir (scm_t_inum x, double y);
  132. SCM_INTERNAL int scm_is_integer_less_than_ri (double x, scm_t_inum y);
  133. SCM_INTERNAL int scm_is_integer_less_than_zz (struct scm_bignum *x,
  134. struct scm_bignum *y);
  135. SCM_INTERNAL int scm_is_integer_less_than_zr (struct scm_bignum *x, double y);
  136. SCM_INTERNAL int scm_is_integer_less_than_rz (double y, struct scm_bignum *x);
  137. SCM_INTERNAL int scm_is_integer_positive_z (struct scm_bignum *x);
  138. SCM_INTERNAL int scm_is_integer_negative_z (struct scm_bignum *x);
  139. SCM_INTERNAL double scm_integer_frexp_z (struct scm_bignum *x, long *exp);
  140. SCM_INTERNAL double scm_integer_to_double_z (struct scm_bignum *x);
  141. SCM_INTERNAL SCM scm_integer_from_double (double val);
  142. SCM_INTERNAL SCM scm_integer_add_ii (scm_t_inum x, scm_t_inum y);
  143. SCM_INTERNAL SCM scm_integer_add_zi (struct scm_bignum *x, scm_t_inum y);
  144. SCM_INTERNAL SCM scm_integer_add_zz (struct scm_bignum *x, struct scm_bignum *y);
  145. SCM_INTERNAL SCM scm_integer_negate_i (scm_t_inum x);
  146. SCM_INTERNAL SCM scm_integer_negate_z (struct scm_bignum *x);
  147. SCM_INTERNAL SCM scm_integer_sub_ii (scm_t_inum x, scm_t_inum y);
  148. SCM_INTERNAL SCM scm_integer_sub_iz (scm_t_inum x, struct scm_bignum *y);
  149. SCM_INTERNAL SCM scm_integer_sub_zi (struct scm_bignum *x, scm_t_inum y);
  150. SCM_INTERNAL SCM scm_integer_sub_zz (struct scm_bignum *x, struct scm_bignum *y);
  151. SCM_INTERNAL SCM scm_integer_mul_ii (scm_t_inum x, scm_t_inum y);
  152. SCM_INTERNAL SCM scm_integer_mul_zi (struct scm_bignum *x, scm_t_inum y);
  153. SCM_INTERNAL SCM scm_integer_mul_zz (struct scm_bignum *x, struct scm_bignum *y);
  154. SCM_INTERNAL int scm_is_integer_divisible_ii (scm_t_inum x, scm_t_inum y);
  155. SCM_INTERNAL int scm_is_integer_divisible_zi (struct scm_bignum *x,
  156. scm_t_inum y);
  157. SCM_INTERNAL int scm_is_integer_divisible_zz (struct scm_bignum *x,
  158. struct scm_bignum *y);
  159. SCM_INTERNAL SCM scm_integer_exact_quotient_ii (scm_t_inum n, scm_t_inum d);
  160. SCM_INTERNAL SCM scm_integer_exact_quotient_iz (scm_t_inum n,
  161. struct scm_bignum *d);
  162. SCM_INTERNAL SCM scm_integer_exact_quotient_zi (struct scm_bignum *n,
  163. scm_t_inum d);
  164. SCM_INTERNAL SCM scm_integer_exact_quotient_zz (struct scm_bignum *n,
  165. struct scm_bignum *d);
  166. #if SCM_SIZEOF_LONG == 4
  167. SCM_INTERNAL SCM scm_integer_from_int32 (int32_t n);
  168. SCM_INTERNAL SCM scm_integer_from_uint32 (uint32_t n);
  169. SCM_INTERNAL int scm_integer_to_int32_z (struct scm_bignum *z, int32_t *val);
  170. SCM_INTERNAL int scm_integer_to_uint32_z (struct scm_bignum *z, uint32_t *val);
  171. #endif
  172. SCM_INTERNAL int scm_integer_to_int64_z (struct scm_bignum *z, int64_t *val);
  173. SCM_INTERNAL int scm_integer_to_uint64_z (struct scm_bignum *z, uint64_t *val);
  174. SCM_INTERNAL SCM scm_integer_from_int64 (int64_t n);
  175. SCM_INTERNAL SCM scm_integer_from_uint64 (uint64_t n);
  176. SCM_INTERNAL void scm_integer_exact_sqrt_i (scm_t_inum k, SCM *s, SCM *r);
  177. SCM_INTERNAL void scm_integer_exact_sqrt_z (struct scm_bignum *k,
  178. SCM *s, SCM *r);
  179. SCM_INTERNAL int scm_is_integer_perfect_square_i (scm_t_inum k);
  180. SCM_INTERNAL int scm_is_integer_perfect_square_z (struct scm_bignum *k);
  181. SCM_INTERNAL SCM scm_integer_floor_sqrt_i (scm_t_inum k);
  182. SCM_INTERNAL SCM scm_integer_floor_sqrt_z (struct scm_bignum *k);
  183. SCM_INTERNAL double scm_integer_inexact_sqrt_i (scm_t_inum k);
  184. SCM_INTERNAL double scm_integer_inexact_sqrt_z (struct scm_bignum *k);
  185. SCM_INTERNAL SCM scm_integer_scan1_i (scm_t_inum n);
  186. SCM_INTERNAL SCM scm_integer_scan1_z (struct scm_bignum *n);
  187. #endif /* SCM_INTEGERS_H */