1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 03:40:34 +02:00
guile/libguile/integers.h
Jan (janneke) Nieuwenhuizen 76950b4281 Support for x86_64-w64-mingw32.
On x86-64-MinGW the size of long is 4.  As long is used for
SCM_FIXNUM_BIT, that would mean incompatible .go files, and waste of
cell space.  So we would like to use long long, but the GMP interface
uses long.

To get around this, the x86-64-MinGW port now requires the use of
mini-GMP.  Mini-GMP has been changed to use intptr_t and uintptr_t.

Likewise, "integers.{h,c}" and "numbers.{h,c}" now use intptr_t instead
of scm_t_inum or long, and uintptr_t instead of unsigned long.

* configure.ac: When x86_64-w64-mingw32, require mini-GMP.
* libguile/mini-gmp.h: Use intptr_t instead of long, uintptr_t instead
of unsigned long throughout.
* libguile/mini-gmp.c: Likewise.
* libguile/scm.h (SCM_INTPTR_T_BIT): New define.
* libguile/numbers.h (SCM_FIXNUM_BIT): Use it.
* libguile/numbers.c (L1, UL1): New macros.  Use them thoughout instead
of 1L, 1UL.
(verify): Use SCM_INTPTR_T_BIT.
(verify): Use SCM_INTPTR_T_MAX and SCM_INTPTR_T_MIN.
(scm_from_inum): Remove macro.
Use intptr_t and uintptr_t instead of scm_t_inum or long, and unsigned
long.
* libguile/numbers.h (scm_from_intptr, scm_from_uintptr, scm_to_intptr,
scm_to_uintptr): New defines.
* libguile/integers.h: Use intptr_t and uintptr_t instead of scm_t_inum
and unsigned long.
* libguile/integers.c (L1) : New macro.  Use it thoughout instead of 1L.
Use intptr_t and uintptr_t instead of long and unsigned long.
(long_magnitude): Rename to...
(intptr_t_magnitude): ...this.  Use intptr_t, uintptr_t.
(negative_long): Rename to...
(negative_t_intptr): ...this.  Use uintptr_t, INTPTR_MIN.
(inum_magnitude): Use intptr_t.
(ulong_to_bignum): Rename to...
(uintptr_t_to_bignum): ...this.  Use uintptr_t.
(long_to_bignum): Rename to...
(intptr_t_to_bignum): ...this.  Use intptr_t.
(long_to_scm): Rename to...
(intptr_t_to_scm): ...this.  Use intptr_to_bignum.
(ulong_to_scm): Rename to...
(uintptr_t_to_scm): ...this.  Use uintptr_to_bignum.
(long_sign): Rename to..
(intptr_t_sign): ...this.  Use SCM_SIZEOF_INTPTR_T.
(bignum_cmp_long): Rename to...
(bignum_cmp_intptr_t): ...this.  Use uintptr_t.
* libguile/array-map.c (array_compare): Use uintptr_t instead of
unsigned long and intptr_t instead of long.
* libguile/arrays.c (make-shared-array): Use ssize_t instead of long.
* libguile/bytevectors.c (is_signed_int32, is_unsigned_int32)
[MINGW32 && __x86_64__]: Use ULL.
(twos_complement): Use uintptr_t instead of unsigned long.
* libguile/hash.c (JENKINS_LOOKUP3_HASHWORD2): Likewise.
(narrow_string_hash, wide_string_hash, scm_i_string_hash,
scm_i_locale_string_hash, scm_i_latin1_string_hash,
scm_i_utf8_string_hash, scm_i_struct_hash, scm_raw_ihashq,
scm_raw_ihash): Use and return uintptr_t instead of unsigned long.
(scm_hashv, scm_hash): Use SCM_UINTPTR_T_MAX.
* libguile/hash.h (scm_i_locale_string_hash, scm_i_latin1_string_hash,
scm_i_utf8_string_hash): update prototypes.
* libguile/scmsigs.c (sigaction): Use intptr_t instead of long.
* libguile/strings.c (scm_i_make_symbol, (scm_i_c_make_symbol): Use
uintptr_t instead of unsigned long.
* libguile/strings.h (scm_i_make_symbol, (scm_i_c_make_symbol): Update
declacations.
* libguile/srfi-60.c: Use scm_to_uintptr, scm_from_intptr and variants
throughout.
* libguile/symbols.c (symbol-hash): Use scm_from_uintptr.

Co-authored-by: Mike Gran <spk121@yahoo.com>
Co-authored-by: Andy Wingo <wingo@pobox.com>
2022-11-10 10:33:49 -08:00

238 lines
12 KiB
C
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef SCM_INTEGERS_H
#define SCM_INTEGERS_H
/* Copyright 2021, 2022 Free Software Foundation, Inc.
This file is part of Guile.
Guile is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Guile is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Guile. If not, see
<https://www.gnu.org/licenses/>. */
#include "libguile/numbers.h"
struct scm_bignum;
static inline struct scm_bignum *
scm_bignum (SCM x)
{
if (!SCM_BIGP (x)) abort ();
return (struct scm_bignum *) SCM_UNPACK (x);
}
SCM_INTERNAL SCM scm_integer_from_mpz (const mpz_t n);
SCM_INTERNAL void scm_integer_set_mpz_z (struct scm_bignum *z, mpz_t n);
SCM_INTERNAL void scm_integer_init_set_mpz_z (struct scm_bignum *z, mpz_t n);
SCM_INTERNAL int scm_is_integer_odd_i (intptr_t i);
SCM_INTERNAL int scm_is_integer_odd_z (struct scm_bignum *z);
SCM_INTERNAL SCM scm_integer_abs_i (intptr_t i);
SCM_INTERNAL SCM scm_integer_abs_z (struct scm_bignum *z);
#define DECLARE_QUOTIENT_OPERATORS(stem) \
SCM_INTERNAL SCM scm_integer_##stem##_quotient_ii (intptr_t x, \
intptr_t y); \
SCM_INTERNAL SCM scm_integer_##stem##_quotient_iz (intptr_t x, \
struct scm_bignum *y); \
SCM_INTERNAL SCM scm_integer_##stem##_quotient_zi (struct scm_bignum *x, \
intptr_t y); \
SCM_INTERNAL SCM scm_integer_##stem##_quotient_zz (struct scm_bignum *x, \
struct scm_bignum *y);
#define DECLARE_REMAINDER_OPERATORS(stem) \
SCM_INTERNAL SCM scm_integer_##stem##_remainder_ii (intptr_t x, \
intptr_t y); \
SCM_INTERNAL SCM scm_integer_##stem##_remainder_iz (intptr_t x, \
struct scm_bignum *y); \
SCM_INTERNAL SCM scm_integer_##stem##_remainder_zi (struct scm_bignum *x, \
intptr_t y); \
SCM_INTERNAL SCM scm_integer_##stem##_remainder_zz (struct scm_bignum *x, \
struct scm_bignum *y);
#define DECLARE_DIVIDE_OPERATORS(stem) \
SCM_INTERNAL void scm_integer_##stem##_divide_ii (intptr_t x, \
intptr_t y, \
SCM *qp, SCM *rp); \
SCM_INTERNAL void scm_integer_##stem##_divide_iz (intptr_t x, \
struct scm_bignum *y, \
SCM *qp, SCM *rp); \
SCM_INTERNAL void scm_integer_##stem##_divide_zi (struct scm_bignum *x, \
intptr_t y, \
SCM *qp, SCM *rp); \
SCM_INTERNAL void scm_integer_##stem##_divide_zz (struct scm_bignum *x, \
struct scm_bignum *y, \
SCM *qp, SCM *rp);
#define DECLARE_DIVISION_OPERATORS(stem) \
DECLARE_QUOTIENT_OPERATORS(stem); \
DECLARE_REMAINDER_OPERATORS(stem); \
DECLARE_DIVIDE_OPERATORS(stem)
DECLARE_DIVISION_OPERATORS(floor);
DECLARE_DIVISION_OPERATORS(ceiling);
DECLARE_DIVISION_OPERATORS(truncate);
DECLARE_DIVISION_OPERATORS(centered);
DECLARE_DIVISION_OPERATORS(round);
SCM_INTERNAL SCM scm_integer_gcd_ii (intptr_t x, intptr_t y);
SCM_INTERNAL SCM scm_integer_gcd_zi (struct scm_bignum *x, intptr_t y);
SCM_INTERNAL SCM scm_integer_gcd_zz (struct scm_bignum *x,
struct scm_bignum *y);
SCM_INTERNAL SCM scm_integer_lcm_ii (intptr_t x, intptr_t y);
SCM_INTERNAL SCM scm_integer_lcm_zi (struct scm_bignum *x, intptr_t y);
SCM_INTERNAL SCM scm_integer_lcm_zz (struct scm_bignum *x,
struct scm_bignum *y);
SCM_INTERNAL SCM scm_integer_logand_ii (intptr_t x, intptr_t y);
SCM_INTERNAL SCM scm_integer_logand_zi (struct scm_bignum *x, intptr_t y);
SCM_INTERNAL SCM scm_integer_logand_zz (struct scm_bignum *x,
struct scm_bignum *y);
SCM_INTERNAL SCM scm_integer_logior_ii (intptr_t x, intptr_t y);
SCM_INTERNAL SCM scm_integer_logior_zi (struct scm_bignum *x, intptr_t y);
SCM_INTERNAL SCM scm_integer_logior_zz (struct scm_bignum *x,
struct scm_bignum *y);
SCM_INTERNAL SCM scm_integer_logxor_ii (intptr_t x, intptr_t y);
SCM_INTERNAL SCM scm_integer_logxor_zi (struct scm_bignum *x, intptr_t y);
SCM_INTERNAL SCM scm_integer_logxor_zz (struct scm_bignum *x,
struct scm_bignum *y);
SCM_INTERNAL int scm_integer_logtest_ii (intptr_t x, intptr_t y);
SCM_INTERNAL int scm_integer_logtest_zi (struct scm_bignum *x, intptr_t y);
SCM_INTERNAL int scm_integer_logtest_zz (struct scm_bignum *x,
struct scm_bignum *y);
SCM_INTERNAL int scm_integer_logbit_ui (uintptr_t bit, intptr_t n);
SCM_INTERNAL int scm_integer_logbit_uz (uintptr_t bit,
struct scm_bignum *n);
SCM_INTERNAL SCM scm_integer_lognot_i (intptr_t n);
SCM_INTERNAL SCM scm_integer_lognot_z (struct scm_bignum *n);
SCM_INTERNAL SCM scm_integer_expt_ii (intptr_t n, intptr_t k);
SCM_INTERNAL SCM scm_integer_expt_zi (struct scm_bignum *n, intptr_t k);
SCM_INTERNAL SCM scm_integer_modulo_expt_nnn (SCM n, SCM k, SCM m);
SCM_INTERNAL SCM scm_integer_lsh_iu (intptr_t n, uintptr_t count);
SCM_INTERNAL SCM scm_integer_lsh_zu (struct scm_bignum *n,
uintptr_t count);
SCM_INTERNAL SCM scm_integer_floor_rsh_iu (intptr_t n, uintptr_t count);
SCM_INTERNAL SCM scm_integer_floor_rsh_zu (struct scm_bignum *n,
uintptr_t count);
SCM_INTERNAL SCM scm_integer_round_rsh_iu (intptr_t n, uintptr_t count);
SCM_INTERNAL SCM scm_integer_round_rsh_zu (struct scm_bignum *n,
uintptr_t count);
SCM_INTERNAL SCM scm_integer_bit_extract_i (intptr_t n, uintptr_t start,
uintptr_t bits);
SCM_INTERNAL SCM scm_integer_bit_extract_z (struct scm_bignum *n,
uintptr_t start,
uintptr_t bits);
SCM_INTERNAL SCM scm_integer_logcount_i (intptr_t n);
SCM_INTERNAL SCM scm_integer_logcount_z (struct scm_bignum *n);
SCM_INTERNAL SCM scm_integer_length_i (intptr_t n);
SCM_INTERNAL SCM scm_integer_length_z (struct scm_bignum *n);
SCM_INTERNAL SCM scm_integer_to_string_i (intptr_t n, int base);
SCM_INTERNAL SCM scm_integer_to_string_z (struct scm_bignum *n, int base);
SCM_INTERNAL int scm_is_integer_equal_ir (intptr_t x, double y);
SCM_INTERNAL int scm_is_integer_equal_ic (intptr_t x,
double real, double imag);
SCM_INTERNAL int scm_is_integer_equal_zz (struct scm_bignum *x,
struct scm_bignum *y);
SCM_INTERNAL int scm_is_integer_equal_zr (struct scm_bignum *x, double y);
SCM_INTERNAL int scm_is_integer_equal_zc (struct scm_bignum *x,
double real, double imag);
SCM_INTERNAL int scm_is_integer_less_than_ir (intptr_t x, double y);
SCM_INTERNAL int scm_is_integer_less_than_ri (double x, intptr_t y);
SCM_INTERNAL int scm_is_integer_less_than_zz (struct scm_bignum *x,
struct scm_bignum *y);
SCM_INTERNAL int scm_is_integer_less_than_zr (struct scm_bignum *x, double y);
SCM_INTERNAL int scm_is_integer_less_than_rz (double y, struct scm_bignum *x);
SCM_INTERNAL int scm_is_integer_positive_z (struct scm_bignum *x);
SCM_INTERNAL int scm_is_integer_negative_z (struct scm_bignum *x);
SCM_INTERNAL double scm_integer_frexp_z (struct scm_bignum *x, intptr_t *exp);
SCM_INTERNAL double scm_integer_to_double_z (struct scm_bignum *x);
SCM_INTERNAL SCM scm_integer_from_double (double val);
SCM_INTERNAL SCM scm_integer_add_ii (intptr_t x, intptr_t y);
SCM_INTERNAL SCM scm_integer_add_zi (struct scm_bignum *x, intptr_t y);
SCM_INTERNAL SCM scm_integer_add_zz (struct scm_bignum *x, struct scm_bignum *y);
SCM_INTERNAL SCM scm_integer_negate_i (intptr_t x);
SCM_INTERNAL SCM scm_integer_negate_z (struct scm_bignum *x);
SCM_INTERNAL SCM scm_integer_sub_ii (intptr_t x, intptr_t y);
SCM_INTERNAL SCM scm_integer_sub_iz (intptr_t x, struct scm_bignum *y);
SCM_INTERNAL SCM scm_integer_sub_zi (struct scm_bignum *x, intptr_t y);
SCM_INTERNAL SCM scm_integer_sub_zz (struct scm_bignum *x, struct scm_bignum *y);
SCM_INTERNAL SCM scm_integer_mul_ii (intptr_t x, intptr_t y);
SCM_INTERNAL SCM scm_integer_mul_zi (struct scm_bignum *x, intptr_t y);
SCM_INTERNAL SCM scm_integer_mul_zz (struct scm_bignum *x, struct scm_bignum *y);
SCM_INTERNAL int scm_is_integer_divisible_ii (intptr_t x, intptr_t y);
SCM_INTERNAL int scm_is_integer_divisible_zi (struct scm_bignum *x,
intptr_t y);
SCM_INTERNAL int scm_is_integer_divisible_zz (struct scm_bignum *x,
struct scm_bignum *y);
SCM_INTERNAL SCM scm_integer_exact_quotient_ii (intptr_t n, intptr_t d);
SCM_INTERNAL SCM scm_integer_exact_quotient_iz (intptr_t n,
struct scm_bignum *d);
SCM_INTERNAL SCM scm_integer_exact_quotient_zi (struct scm_bignum *n,
intptr_t d);
SCM_INTERNAL SCM scm_integer_exact_quotient_zz (struct scm_bignum *n,
struct scm_bignum *d);
#if SCM_SIZEOF_INTPTR_T == 4
SCM_INTERNAL SCM scm_integer_from_int32 (int32_t n);
SCM_INTERNAL SCM scm_integer_from_uint32 (uint32_t n);
SCM_INTERNAL int scm_integer_to_int32_z (struct scm_bignum *z, int32_t *val);
SCM_INTERNAL int scm_integer_to_uint32_z (struct scm_bignum *z, uint32_t *val);
#endif
SCM_INTERNAL int scm_integer_to_int64_z (struct scm_bignum *z, int64_t *val);
SCM_INTERNAL int scm_integer_to_uint64_z (struct scm_bignum *z, uint64_t *val);
SCM_INTERNAL SCM scm_integer_from_int64 (int64_t n);
SCM_INTERNAL SCM scm_integer_from_uint64 (uint64_t n);
SCM_INTERNAL void scm_integer_exact_sqrt_i (intptr_t k, SCM *s, SCM *r);
SCM_INTERNAL void scm_integer_exact_sqrt_z (struct scm_bignum *k,
SCM *s, SCM *r);
SCM_INTERNAL int scm_is_integer_perfect_square_i (intptr_t k);
SCM_INTERNAL int scm_is_integer_perfect_square_z (struct scm_bignum *k);
SCM_INTERNAL SCM scm_integer_floor_sqrt_i (intptr_t k);
SCM_INTERNAL SCM scm_integer_floor_sqrt_z (struct scm_bignum *k);
SCM_INTERNAL double scm_integer_inexact_sqrt_i (intptr_t k);
SCM_INTERNAL double scm_integer_inexact_sqrt_z (struct scm_bignum *k);
SCM_INTERNAL SCM scm_integer_scan1_i (intptr_t n);
SCM_INTERNAL SCM scm_integer_scan1_z (struct scm_bignum *n);
#endif /* SCM_INTEGERS_H */