1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 03:40:34 +02:00
Commit graph

68 commits

Author SHA1 Message Date
Michael Gran
9a29293a88 More long integer fixes for x86_64-w64-mingw32
* libguile/gc.c (scm_object_address): use uintptr_t instead of ulong
* libguile/gen-scmconfig.c (main): handle different sizes of scm_off_t
* libguile/integers.c (scm_integer_length_z): the mask size is bitcnt_max,
    not UINTPTR_MAX
* libguile/ports.c (scm_set_port_line_x): test that line is an intptr_t,
    not a long
* libguile/srfi-60.c (scm_srfi60_rotate_bit_fields): use correct size of
    integer for bit-shifting
  (scm_srfi60_reverse_bit_field): use correct integer size for bit-shifting
* libguile/symbols.h (scm_i_symbol_hash): cast to uintptr_t, not unsigned long
2022-11-10 10:33:57 -08:00
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
Michael Gran
1ddc4eb964 Fixes arg type for scm_integer_from_mpz
The definition and the declaration for scm_integer_from_mpz
do not match

* libguile/integers.c (scm_integer_from_mpz): takes const mpz_t arg
  No callers need to be changed.
2022-10-15 10:35:54 -07:00
Andy Wingo
62fc44e6ee Slight bignum-fixnum multiplication optimization
* libguile/integers.c (scm_integer_mul_zi): Use mpn_mul_1.
2022-02-01 19:57:14 +01:00
Andy Wingo
222b320868 Remove dead code in scm_integer_inexact_sqrt_z
* libguile/integers.c (scm_integer_inexact_sqrt_z): Remove some dead
code.
2022-01-13 09:37:17 +01:00
Andy Wingo
9e5aa173c7 Optimize logand against a positive inum
* libguile/integers.c (scm_integer_logand_zi): Optimize.
2022-01-13 09:37:17 +01:00
Andy Wingo
6fe43301aa Optimize integer-expt for fixnums
* libguile/integers.c (scm_integer_expt_ii): Add some optimizations for
cases in which we can avoid allocating an mpz.
2022-01-13 09:37:17 +01:00
Andy Wingo
2cf80ca238 Optimize scm_integer_mul_ii
* libguile/integers.c (make_bignum_2): Always include.
(scm_integer_mul_ii): Avoid making a temporary allocation.
2022-01-13 09:37:17 +01:00
Andy Wingo
210ab8ff76 Optimize bignum subtraction
* libguile/integers.c (scm_integer_sub_iz):
(scm_integer_sub_zi):
(scm_integer_sub_zz): Optimize to avoid temporary allocations.
2022-01-13 09:37:17 +01:00
Andy Wingo
cc52f01aba Optimize bignum add to avoid temporary allocations
* libguile/integers.c (do_add_1, do_add, do_sub_1, do_sub, do_cmp): New
helpers.
(scm_integer_add_zi):
(scm_integer_add_zz): Use new helpers.
2022-01-13 09:37:17 +01:00
Andy Wingo
95350aae8c Avoid bignum clone in scm_integer_sub_zz
* libguile/integers.c (scm_integer_abs_z): Just call
scm_integer_negate_z if negation is needed.
(scm_integer_sub_zz): Use mpz_sub instead of cloning bignum.
2022-01-13 09:37:17 +01:00
Andy Wingo
cae8b30163 Start to optimize scm_integer_sub_iz
* libguile/integers.c (scm_integer_sub_iz): Avoid cloning bignum.
2022-01-13 09:37:17 +01:00
Andy Wingo
2723513712 Less pessimal scm_integer_sub_zi
* libguile/integers.c (scm_integer_sub_zi): Only delegate to
scm_integer_add_ii if y is negative.
2022-01-13 09:37:17 +01:00
Andy Wingo
ad6811a12b Optimize scm_integer_mul_zz.
* libguile/integers.c (scm_integer_mul_zz): Optimize to avoid temporary
allocations.
2022-01-13 09:37:17 +01:00
Andy Wingo
d89f75c535 Optimize scm_integer_mul_zi
* libguile/integers.c (bignum_trim1):
(bignum_negate_if): New helpers.
(scm_integer_mul_zi): Use the mpn API to avoid temporary allocation.
2022-01-13 09:37:17 +01:00
Andy Wingo
5481222670 Re-rewrite integer-expt in C
Calling out to Scheme was a performance regression.

* libguile/integers.h:
* libguile/integers.c (scm_integer_expt_ii, scm_integer_expt_zi): New
internal functions.
* libguile/numbers.c (scm_integer_expt): Go back to C.  But, include
fast cases for inums and doubles.
* module/ice-9/boot-9.scm: Revert addition of integer-expt.
2022-01-13 09:37:17 +01:00
Andy Wingo
eac47c3e45 Fix bug when making mpz from 0
* libguile/integers.c (make_bignum_0): New helper.
(make_bignum_from_uint64):
(ulong_to_bignum):
(long_to_bignum): Call new helper as appropriate.
2022-01-13 09:37:17 +01:00
Andy Wingo
443d239828 take_mpz optimization
* libguile/integers.c (take_mpz): Avoid making a bignum if the value is
fixable.
2022-01-13 09:37:17 +01:00
Andy Wingo
399d0c8745 Start porting srfi-60 off the bad bignum interfaces
* libguile/integers.h:
* libguile/integers.c (scm_integer_scan1_i):
(scm_integer_scan1_z): New internal functions.
* libguile/srfi-60.c (scm_srfi60_log2_binary_factors): Use scan1
functions.
(scm_srfi60_copy_bit): Use integers lib.
2022-01-13 09:37:17 +01:00
Andy Wingo
a4524da8c1 Simplify scm_exact_integer_quotient
* libguile/integers.h:
* libguile/integers.c (scm_integer_exact_quotient_iz): New internal
function.
* libguile/numbers.c (scm_i_make_ratio): Simplify and enforce
invariants.
(scm_exact_integer_quotient): Use integer lib.
2022-01-13 09:37:17 +01:00
Andy Wingo
0754dbf3e8 divide2double refactor
* libguile/integers.c (scm_integer_set_mpz_z): New internal function.
(scm_integer_init_set_mpz_z): Rename from scm_integer_to_mpz_z.
* libguile/integers.h:
* libguile/numbers.c (scm_i_divide2double): Avoid SCM_I_BIG_MPZ.
2022-01-13 09:37:17 +01:00
Andy Wingo
f167627bb0 Expose frexp from integers lib
* libguile/integers.h:
* libguile/integers.c (scm_integer_frexp_z): Expose internally.
(scm_integer_to_double_z):
(scm_integer_inexact_sqrt_z): Adapt uses.
* libguile/numbers.c (log_of_exact_integer): Use exposed
scm_integer_frexp_z.
2022-01-13 09:37:17 +01:00
Andy Wingo
124d889227 Refactor scm_sqrt in terms of integers.[ch]
* libguile/integers.h:
* libguile/integers.c (scm_is_integer_perfect_square_i):
(scm_is_integer_perfect_square_z):
(scm_integer_floor_sqrt_i):
(scm_integer_floor_sqrt_z):
(scm_integer_inexact_sqrt_i):
(scm_integer_inexact_sqrt_z): New internal functions.
* libguile/numbers.c (scm_sqrt): Reimplement.
2022-01-13 09:37:17 +01:00
Andy Wingo
63a18a6c1a Reimplement exact-integer-sqrt with integers.[ch]
* libguile/numbers.c (scm_exact_integer_sqrt): Call out.
* libguile/integers.h:
* libguile/integers.c (scm_integer_exact_sqrt_i):
(scm_integer_exact_sqrt_z): New internal functions.
2022-01-13 09:37:17 +01:00
Andy Wingo
9a91c20a55 scm_to_mpz uses integer lib
* libguile/integers.h:
* libguile/integers.c (scm_integer_to_mpz_z): New internal function.
* libguile/numbers.c (scm_to_mpz): Use new function.
2022-01-13 09:37:17 +01:00
Andy Wingo
717e787da6 Reimplement scm_{to,from}_{int32,uint32}
* libguile/numbers.c (scm_to_int32, scm_from_int32, scm_to_uint32):
(scm_from_uint32): Reimplement inline.
* libguile/integers.c (make_bignum_2):
(make_bignum_from_uint64):
(negative_uint32_to_int32):
(positive_uint32_to_int32):
(bignum_to_int32):
(bignum_to_uint32):
(scm_integer_from_int32):
(scm_integer_from_uint32):
(scm_integer_to_int32_z):
(scm_integer_to_uint32_z): Better int32 support for 32-bit machines.
2022-01-13 09:37:17 +01:00
Andy Wingo
27910181c5 Reimplement scm_from_int8 etc
* libguile/integers.c (make_bignum_from_uint64):
(make_bignum_from_int64): New helpers.
(scm_integer_from_int64):
(scm_integer_from_uint64): New internal functions.
* libguile/integers.h: Declare new internal functions.
* libguile/numbers.c (range_error): Declare as noreturn.xo
(inum_in_range): New helper.
(scm_from_signed_integer):
(scm_to_signed_integer):
(scm_from_unsigned_integer):
(scm_to_unsigned_integer):
(scm_to_int8):
(scm_from_int8):
(scm_to_uint8):
(scm_from_uint8):
(scm_to_int16):
(scm_from_int16):
(scm_to_uint16):
(scm_from_uint16): Implement manually.
2022-01-13 09:37:16 +01:00
Andy Wingo
f4db3ca3f9 Reimplement scm_is_{un,}signed_integer for bignums
* libguile/integers.c (negative_int64):
(int64_magnitude):
(negative_uint64_to_int64):
(positive_uint64_to_int64):
(bignum_to_int64):
(bignum_to_uint64): New helpers.
(scm_integer_to_int64_z):
(scm_integer_to_uint64_z): New internal functions.
* libguile/integers.h: Declare internal functions.
* libguile/numbers.c (scm_is_signed_integer):
(scm_is_unsigned_integer): Simplify bigint cases.
2022-01-13 09:37:16 +01:00
Andy Wingo
7c53325c31 Fix scm_integer_to_double_z to always round; clean ups
* libguile/integers.c (scm_integer_to_double_z): Doubles that can't be
exactly represented as integers should round.
(bignum_frexp_z): New helper.
(scm_integer_from_mpz, scm_integer_from_double): New internal functions.
* libguile/numbers.h:
* libguile/numbers.c (scm_i_bigcmp, scm_i_dbl2big, scm_i_dbl2num):
Remove unused internal functions.
(scm_inexact_to_exact): Rework to avoid scm_i_dbl2big.
(scm_bigequal): Move here, from eq.c.
(scm_integer_to_double_z): Use the new helper.
(scm_i_big2dbl): Use scm_integer_to_double_z.
2022-01-13 09:37:16 +01:00
Andy Wingo
8b2d58b993 Clean up scm_divide
* libguile/integers.h:
* libguile/integers.c (scm_is_integer_divisible_ii):
(scm_is_integer_divisible_zi):
(scm_is_integer_divisible_zz): New helpers.
* libguile/numbers.c (invert, divide, complex_div): New helpers for
scm_divide.
(scm_divide): Adapt.
2022-01-13 09:37:16 +01:00
Andy Wingo
9179525a05 Simplify scm_product, use integer lib
* libguile/numbers.c (scm_product): Remove need for s_product defines.
Call out to product, as appropriate.
(product): New helper.
* libguile/integers.h:
* libguile/integers.c (scm_integer_mul_ii):
(scm_integer_mul_zi):
(scm_integer_mul_zz): New internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
c096670d38 Simplify scm_difference, use integer lib
* libguile/numbers.c (scm_sum): Remove need for s_sum defines.
(negate, difference): New helpers.
(scm_difference): Call out to difference or negate, as appropriate.
* libguile/integers.h:
* libguile/integers.c (scm_integer_negate_i):
(scm_integer_negate_z):
(scm_integer_sub_ii):
(scm_integer_sub_iz):
(scm_integer_sub_zi):
(scm_integer_sub_zz): New internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
10953e067c Clean up scm_sum
* libguile/integers.h:
* libguile/integers.c (scm_integer_to_double_z):
(scm_integer_add_ii, scm_integer_add_zi, scm_integer_add_zz): New
internal functions.
* libguile/numbers.c (sum): New helper for scm_sum.  Clean up to avoid
repetition.  The dispatch is less optimal but the code is shorter and
more maintainable; in any case if speed is important, the compiler needs
to be involved.
(scm_sum): Adapt.
2022-01-13 09:37:16 +01:00
Andy Wingo
54d7722523 Clean up <, reimplement in terms of integer lib
* libguile/numbers.c (scm_is_less_than, scm_is_greater_than):
(scm_is_less_than_or_equal, scm_is_greater_than_or_equal): New internal
functions.
(scm_less_p, scm_gr_p, scm_leq_p, scm_geq_p): Use new helpers.  Dispatch
to generics if operands aren't real -- a tightening relative to the
previous check which was just for numbers.
* libguile/integers.h:
* libguile/integers.c (scm_is_integer_less_than_ir):
(scm_is_integer_less_than_ri):
(scm_is_integer_less_than_zz):
(scm_is_integer_less_than_zr):
(scm_is_integer_less_than_rz):
(scm_is_integer_positive_z):
(scm_is_integer_negative_z): New internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
44bee08512 Reimplement = on integer lib, clean up scm_num_eq_p
* libguile/integers.h:
* libguile/integers.c (scm_is_integer_equal_ir):
(scm_is_integer_equal_ic):
(scm_is_integer_equal_zz):
(scm_is_integer_equal_zr):
(scm_is_integer_equal_zc): New internal functions.
* libguile/numbers.c (scm_num_eq_p): Rework to tail-recurse if we need
to swap arguments, to reduce duplication, and use the new integer lib.
2022-01-13 09:37:16 +01:00
Andy Wingo
fc36cd610d Implement integer-to-string with new integer library
* libguile/integers.c (scm_integer_to_string_i)
(scm_integer_to_string_z): New internal functions.
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_number_to_string): Use new internal
functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
fc4228c196 Implement scm_integer_length with new integer library
* libguile/integers.c (scm_integer_length_i)
(scm_integer_length_z): New internal functions.
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_integer_length): Use new internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
8b99ace658 Integer library takes bignums via opaque struct pointer
* libguile/integers.h (scm_bignum): Declare opaque struct type and a
function to cast SCM to this type.  Adapt all routines that take bignums
to take a "struct scm_bignum *".
* libguile/integers.c: Adapt.
* libguile/numbers.c: Adapt all users.
2022-01-13 09:37:16 +01:00
Andy Wingo
7ec40fe5b0 Implement scm_logcount with new integer library
* libguile/integers.c (scm_integer_logcount_i)
(scm_integer_logcount_z): New internal functions.
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_logcount): Use new internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
88f56e91aa Implement scm_bit_extract with new integer library
* libguile/integers.c (scm_integer_bit_extract_i)
(scm_integer_bit_extract_z): New internal functions.
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_bit_extract): Use new internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
35861b28bb Implement scm_ash with new integer library
* libguile/integers.c (scm_integer_lsh_iu, scm_integer_lsh_zu)
(scm_integer_floor_rsh_iu, scm_integer_floor_rsh_zu)
(scm_integer_round_rsh_iu, scm_integer_round_rsh_zu): New internal
functions.
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_ash): Use new internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
2d5dc6a14c Implement scm_modulo_expt with new integer library
* libguile/integers.c (scm_integer_modulo_expt_nnn):
(integer_init_mpz): New helper.
* libguile/integers.h: Declare the new internal function.
* libguile/numbers.c (scm_modulo_expt): Use new internal function.
2022-01-13 09:37:16 +01:00
Andy Wingo
b41714d277 Implement scm_lognot with new integer library
* libguile/integers.c (scm_integer_lognot_i, scm_integer_lognot_z):
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_lognot): Use new internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
89cd48fcac Implement scm_logbit_p with new integer library
* libguile/integers.c (scm_integer_logbit_ui, scm_integer_logbit_uz):
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_logbit_p): Use new internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
6298d73115 Implement scm_logtest with new integer library
* libguile/integers.c (scm_integer_logtest_ii, scm_integer_logtest_zi)
(scm_integer_logtest_zz): New internal functions.
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_logtest): Use new internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
459163fca1 Implement scm_logxor with new integer library
* libguile/integers.c (scm_integer_logxor_ii, scm_integer_logxor_zi)
(scm_integer_logxor_zz): New internal functions.
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_logxor): Use new internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
7e85ffa822 Implement scm_logior with new integer library
* libguile/integers.c (scm_integer_logior_ii, scm_integer_logior_zi)
(scm_integer_logior_zz): New internal functions.
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_logior): Use new internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
4a380aa6ac Implement scm_logand with new integer library
* libguile/integers.c (scm_integer_logand_ii, scm_integer_logand_zi)
(scm_integer_logand_zz): New internal functions.
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_logand): Use new internal functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
6fa9fcb313 Implement lcm with new integer lib
* libguile/integers.c (scm_integer_lcm_ii)
(scm_integer_lcm_zi, scm_integer_lcm_zz): New internal functions.
* libguile/integers.h: Declare internal functions.
* libguile/numbers.c (scm_lcm): Use the new functions.
2022-01-13 09:37:16 +01:00
Andy Wingo
1e0797db72 Implement gcd with new integer lib
* libguile/integers.c (scm_integer_gcd_ii)
(scm_integer_gcd_zi, scm_integer_gcd_zz): New internal functions.
* libguile/integers.h: Declare internal functions.
* libguile/numbers.c (scm_gcd): Use the new functions.
2022-01-13 09:37:16 +01:00