summaryrefslogtreecommitdiff
path: root/libguile/numbers.c
Commit message (Collapse)AuthorAgeFilesLines
* Handle GMP allocations through libgc and remove bignum finalizers.Ludovic Courtès2020-02-121-7/+12
| | | | | | | | | | | | | | | | This significantly speeds up loads that create lots of bignums, like (language cps slot-allocation) for files with many top-level definitions. Compiling such a file is typically 2.5 times faster. See <https://lists.gnu.org/archive/html/guile-devel/2020-02/msg00023.html>. * libguile/numbers.c (custom_gmp_malloc): Use 'scm_gc_malloc_pointerless' instead of 'scm_malloc'. (custom_gmp_realloc): Use 'scm_gc_realloc'. (custom_gmp_free): Remove call to 'free'. (make_bignum): Use 'scm_gc_malloc' instead of 'scm_gc_malloc_pointerless'. Call 'scm_i_set_finalizer' only when SCM_INSTALL_GMP_MEMORY_FUNCTIONS is false.
* Optimize fixnum exact integer square roots.Mark H Weaver2019-05-071-32/+41
| | | | | | | * libguile/numbers.c (scm_exact_integer_sqrt, scm_sqrt) (exact_integer_is_perfect_square, exact_integer_floor_square_root): Where it is trivial to do so, use GMP's low-level mpn functions to avoid heap allocation.
* Fix indentation in scm_sum.Mark H Weaver2019-05-071-67/+68
|
* Fix typo in comment.Mark H Weaver2019-05-071-1/+1
|
* In 'ash' and 'round-ash', handle right shift count of LONG_MIN.Mark H Weaver2018-10-141-2/+11
| | | | | | | | | | | | Fixes <https://bugs.gnu.org/21901>. Reported by Zefram <zefram@fysh.org>. * libguile/numbers.c: Add another top-level 'verify' to ensure that LONG_MIN is not a fixnum. (scm_ash, scm_round_ash): Ensure that when the shift count is LONG_MIN, it is not handled via the normal code path, to avoid signed overflow when the shift count is negated. * test-suite/tests/numbers.test: Add tests.
* Fix 'round-ash' of negative integers by huge right shift counts.Mark H Weaver2018-10-141-6/+10
| | | | | | | | | | | | | | This is a followup to commit 011aec7e240ef987931548d90c53e6692c85d01c. When rounding, right shifting a negative integer by a huge shift count results in 0, not -1. * libguile/numbers.c: Add top-level 'verify' to ensure that the assumptions in 'scm_ash' and 'scm_round_ash' are valid. (scm_round_ash): In the case that handles huge right shifts, require that the shift count _exceeds_ the integer length, and return 0 instead of -1. * test-suite/tests/numbers.test: Adjust tests accordingly.
* Gracefully handle huge shift counts in 'ash' and 'round-ash'.Mark H Weaver2018-10-141-3/+31
| | | | | | | | | | | | Fixes <https://bugs.gnu.org/32644>. Reported by Stefan Israelsson Tampe <stefan.itampe@gmail.com>. The need for this arose because the type inferrer for 'ursh' sometimes passes (- 1 (expt 2 64)) as the second argument to 'ash'. * libguile/numbers.c (scm_ash, scm_round_ash): Gracefully handle several cases where the shift count does not fit in a C 'long'. * test-suite/tests/numbers.test: Add tests.
* Fix typo in comment within numbers.cMark H Weaver2018-06-111-1/+1
|
* Do not assume that sizeof (long) == sizeof (void *) == sizeof (SCM).Mark H Weaver2017-03-011-7/+3
| | | | | | | | | | | | | | | | | | This assumption does not hold on systems that use the LLP64 data model. Partially fixes <https://debbugs.gnu.org/22406>. Reported by Peter TB Brett <peter@peter-b.co.uk>. * libguile/numbers.h (scm_t_inum): Move here from numbers.c, and change to be equivalent to 'long' (formerly 'scm_t_signed_bits'). (SCM_MOST_POSITIVE_FIXNUM, SCM_MOST_NEGATIVE_FIXNUM): Define based on SCM_I_FIXNUM_BIT instead of SCM_T_SIGNED_BITS_MAX. (SCM_I_INUM): Adjust definitions to return a 'scm_t_inum', and avoiding the assumption that SCM_UNPACK returns a 'long'. * libguile/numbers.c (scm_t_inum): Move definition to numbers.h. Verify that 'scm_t_inum' fits within a SCM value. (scm_i_inum2big): Remove preprocessor code that forced a compile error unless sizeof (long) == sizeof (void *).
* Avoid signed integer overflows in numeric conversions.Mark H Weaver2017-03-011-5/+10
| | | | | | | | Reported by Miroslav Lichvar <mlichvar@redhat.com> in <https://lists.gnu.org/archive/html/guile-devel/2016-02/msg00045.html> * libguile/conv-integer.i.c: Avoid signed overflow. * libguile/numbers.c (scm_is_signed_integer): Avoid signed overflow.
* Fix (* x -1) for GOOPS typesAndy Wingo2017-02-281-15/+13
| | | | | | | * libguile/numbers.c (scm_product): Only reduce (* x -1) to (- x) when X is a bignum. Fixes weirdness when X is not a number and instead multiplication should dispatch to GOOPS. Thanks to Alejandro Sanchez for the report.
* Use labs instead of abs where neededAndy Wingo2017-02-211-1/+1
| | | | | * libguile/numbers.c (log_of_fraction): Use labs instead of abs on longs. Thanks to Matt Wette for the tip.
* Fix make-polar signedness of zeros on macOSMatt Wette2017-02-211-0/+2
| | | | | | * configure.ac: Check for __sincos. * libguile/numbers.c (scm_c_make_polar): Fall back to __sincos if possible. Fixes zero signedness of make-polar on macOS.
* Speed up number->stringAndy Wingo2017-02-171-3/+3
| | | | | * libguile/numbers.c (scm_number_to_string): Use scm_from_latin1_string where appropriate. Avoids mucking about with iconv.
* Deprecate dynamic rootsAndy Wingo2016-11-211-1/+0
| | | | | | | | | | | | | * libguile/root.h: * libguile/root.c: Remove these files. * libguile/deprecated.h: * libguile/deprecated.c (scm_internal_cwdr, scm_call_with_dynamic_root) (scm_dynamic_root, scm_apply_with_dynamic_root): Deprecate. Remove all root.h usage, which was vestigial. * module/ice-9/serialize.scm: Use (current-thread) instead of (dynamic-root).
* Fix atan procedure when applied to complex numbers.Mark H Weaver2016-05-221-2/+2
| | | | | | | | Fixes a regression introduced in commit ad79736c68a803a59814fbfc0cb4b092c2b4cddf. * libguile/numbers.c (scm_atan): Fix the complex case. * test-suite/tests/numbers.test ("atan"): Add test.
* Don't return expressions from void functions in numbers.cMark H Weaver2016-05-221-138/+108
| | | | | | | | | | Although popular compilers allow it as long as the expression is of type void, it violates C99 and some compilers choke on it. * libguile/numbers.c (scm_euclidean_divide, scm_floor_divide) (scm_ceiling_divide, scm_truncate_divide, scm_centered_divide) (scm_round_divide): Don't use the return statement with an expression from functions with return type void.
* scm_lfwrite, not scm_lfwrite_unlockedAndy Wingo2016-04-261-5/+5
| | | | | | | * libguile/ports.h (scm_lfwrite_unlocked): Remove. * libguile/ports.c (scm_lfwrite): Rename from scm_lfwrite_unlocked. * libguile/numbers.c: * libguile/print.c: Adapt to call scm_lfwrite.
* Merge branch 'stable-2.0'Mark H Weaver2014-04-251-5/+11
|\ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Conflicts: GUILE-VERSION NEWS guile-readline/ice-9/readline.scm libguile/async.c libguile/backtrace.c libguile/deprecated.h libguile/gc-malloc.c libguile/gdbint.c libguile/init.c libguile/ioext.c libguile/mallocs.c libguile/print.c libguile/rw.c libguile/scmsigs.c libguile/script.c libguile/simpos.c libguile/snarf.h libguile/strports.c libguile/threads.c libguile/vm-i-scheme.c libguile/vm-i-system.c module/srfi/srfi-18.scm test-suite/Makefile.am test-suite/standalone/test-num2integral.c
| * Avoid undefined behavior regarding signed integers and left shifts.Mark H Weaver2014-03-111-6/+12
| | | | | | | | | | | | | | | | | | * libguile/numbers.c (scm_logbit_p): If the requested bit is the sign bit (or above), check the sign portably. Otherwise, ensure that we're testing the bit in a two's complement representation. (left_shift_exact_integer): Avoid left-shifting negative integers. * libguile/vm-i-scheme.c (ash): Avoid left-shifting negative integers.
* | Merge branch 'stable-2.0'Mark H Weaver2014-01-091-2/+21
|\| | | | | | | | | | | Conflicts: module/system/vm/traps.scm test-suite/tests/peval.test
| * Implement 'exact-integer?' and 'scm_is_exact_integer'.Mark H Weaver2014-01-081-2/+21
| | | | | | | | | | | | | | | | | | | | | | | | | | * libguile/numbers.c (scm_exact_integer_p, scm_is_exact_integer): New procedures. (scm_integer_p): Improve docstring. * libguile/numbers.h (scm_exact_integer_p, scm_is_exact_integer): New prototypes. * doc/ref/api-data.texi (Integers): Add docs. * test-suite/tests/numbers.test ("exact-integer?"): Add tests.
* | Merge commit 'd360671c1cca335600079f1c5714572d1c2e676d'Andy Wingo2013-11-281-1/+1
|\|
| * Fix edge case in 'ash'.Mark H Weaver2013-10-031-3/+6
| | | | | | | | | | | | | | | | * libguile/numbers.c (left_shift_exact_integer): Fix edge case where N is -1 and count is SCM_I_FIXNUM_BIT-1 to return the most negative fixnum. Previously this result was returned as a bignum. * test-suite/tests/numbers.test (ash): Add tests.
| * Revert "Fix edge case in 'ash'."Mark H Weaver2013-10-031-7/+4
| | | | | | | | This reverts commit 8df68898b9f6ba15171244f1f3549688f13d605f.
* | Merge remote-tracking branch 'origin/stable-2.0'Andy Wingo2013-10-031-4/+7
|\| | | | | | | | | | | | | Conflicts: libguile/deprecated.h libguile/programs.c libguile/programs.h
| * Fix edge case in 'ash'.Mark H Weaver2013-10-031-4/+7
| | | | | | | | | | | | | | * libguile/numbers.c (scm_ash): Fix (ash -1 SCM_I_FIXNUM_BIT-1) to return a fixnum instead of a bignum. * test-suite/tests/numbers.test (ash): Add tests.
* | Micro-optimize char_decimal_value.Andy Wingo2013-08-311-12/+17
| | | | | | | | * libguile/numbers.c (char_decimal_value): A wee micro-optimization.
* | Merge remote-tracking branch 'origin/stable-2.0'Mark H Weaver2013-08-111-6/+23
|\|
| * Fix inappropriate uses of scm_syserror in numbers.c.Mark H Weaver2013-08-101-4/+5
| | | | | | | | | | | | | | * libguile/numbers.c (mem2ureal, left_shift_exact_integer, floor_right_shift_exact_integer, round_right_shift_exact_integer): Use 'assert' instead of 'scm_syserror' to indicate a case that should never happen.
| * Fix numerator and denominator handling of signed zeroes and infinities.Mark H Weaver2013-08-091-2/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * libguile/numbers.c (scm_numerator, scm_denominator): Handle signed zeroes and infinities in accordance with the corresponding R6RS flonum procedures. * module/rnrs/arithmetic/flonums.scm (flnumerator, fldenominator): Remove special handling of infinities. * test-suite/tests/numbers.test (numerator, denominator): Add tests. Convert existing tests to use 'pass-if-equal'. * test-suite/tests/r6rs-arithmetic-flonums.test (flnumerator): Fix broken test of (flnumerator -0.0).
* | Eliminate use of deprecated 'SCM_WTA_DISPATCH_2' in numbers.c.Mark H Weaver2013-08-081-1/+1
| | | | | | | | | | * libguile/numbers.c (scm_gcd): Use 'scm_wta_dispatch_2' not 'SCM_WTA_DISPATCH_2'.
* | Merge remote-tracking branch 'origin/stable-2.0'Mark H Weaver2013-08-061-222/+312
|\| | | | | | | | | | | Conflicts: libguile/numbers.c libguile/vm-i-scheme.c
| * Use Gnulib's 'isfinite' in numbers.c.Mark H Weaver2013-08-061-15/+6
| | | | | | | | | | | | | | * libguile/numbers.c (DOUBLE_IS_FINITE, DOUBLE_IS_POSITIVE_INFINITY, DOUBLE_IS_NEGATIVE_INFINITY): Remove. (scm_odd_p, scm_even_p, scm_finite_p, icmplx2str, scm_rational_p, scm_inexact_to_exact): Use 'isfinite' instead of 'DOUBLE_IS_FINITE'.
| * Use Gnulib's 'copysign' in numbers.c.Mark H Weaver2013-08-061-18/+10
| | | | | | | | | | | | | | * libguile/numbers.c (double_is_non_negative_zero): Remove. (idbl2str, scm_max, scm_min, scm_angle, log_of_shifted_double, scm_log10): Use 'copysign' to check signs of zeroes, instead of 'double_is_non_negative_zero'.
| * Add 'scm_i_from_double' and use it.Mark H Weaver2013-08-021-117/+123
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * libguile/numbers.c (scm_i_from_double): New static function. (scm_from_double): Just call 'scm_i_from_double'. (scm_inf, scm_nan, scm_abs, scm_i_inexact_floor_quotient, scm_i_inexact_floor_remainder, scm_i_inexact_floor_divide, scm_i_inexact_ceiling_quotient, scm_i_inexact_ceiling_remainder, scm_i_inexact_ceiling_divide, scm_i_inexact_truncate_quotient, scm_i_inexact_truncate_remainder, scm_i_inexact_truncate_divide, scm_i_inexact_centered_quotient, scm_i_inexact_centered_remainder, scm_i_inexact_centered_divide, scm_i_inexact_round_quotient, scm_i_inexact_round_remainder, scm_i_inexact_round_divide, scm_max, scm_min, scm_sum, scm_difference, scm_product, scm_divide, scm_truncate_number, scm_round_number, scm_floor, scm_ceiling, scm_expt, scm_sin, scm_cos, scm_tan, scm_sinh, scm_cosh, scm_tanh, scm_asin, scm_acos, scm_atan, scm_sys_asinh, scm_sys_acosh, scm_sys_atanh, scm_real_part, scm_imag_part, scm_magnitude, scm_angle, scm_exact_to_inexact, log_of_shifted_double, log_of_fraction, scm_log10, scm_exp, scm_sqrt, scm_init_numbers): Use 'scm_i_from_double' instead of 'scm_from_double'.
| * Fix minor formatting error in 'rationalize'.Mark H Weaver2013-07-211-2/+2
| | | | | | | | * libguile/numbers.c (scm_rationalize): Fix formatting.
| * Rewrite 'rationalize' to fix bugs and improve efficiency.Mark H Weaver2013-07-211-76/+177
| | | | | | | | | | | | | | | | | | | | Fixes <http://bugs.gnu.org/14905>. Reported by Göran Weinholt <goran@weinholt.se>. * libguile/numbers.c (scm_rationalize): Rewrite. Previously an incorrect algorithm was used which failed in many cases. * test-suite/tests/numbers.test (rationalize): Add tests.
* | Merge remote-tracking branch 'origin/stable-2.0'Mark H Weaver2013-07-161-27/+53
|\| | | | | | | | | Conflicts: libguile/numbers.c
| * gcd and lcm support inexact integer arguments.Mark H Weaver2013-07-161-16/+45
| | | | | | | | | | | | | | | | | | Fixes <http://bugs.gnu.org/14870>. Reported by Göran Weinholt <goran@weinholt.se>. * libguile/numbers.c (scm_gcd, scm_lcm): Support inexact integers. * test-suite/tests/numbers.test (gcd, lcm): Add tests.
| * min and max: NaNs beat infinities, per R6RS errata.Mark H Weaver2013-07-161-10/+8
| | | | | | | | | | | | | | | | | | | | Fixes <http://bugs.gnu.org/14865>. Reported by Göran Weinholt <goran@weinholt.se>. * libguile/numbers.c (scm_min, scm_max): NaNs beat infinities, as per the R6RS errata. * test-suite/tests/numbers.test (min, max): Update tests.
* | Merge remote-tracking branch 'origin/stable-2.0'Mark H Weaver2013-07-161-33/+82
|\| | | | | | | | | | | Conflicts: libguile/keywords.c libguile/vm.c
| * Avoid lossy conversion from inum to double in numerical comparisons.Mark H Weaver2013-07-161-2/+38
| | | | | | | | | | | | * libguile/numbers.c (scm_less_p): Avoid converting inums to doubles. * test-suite/tests/numbers.test (<): Add tests.
| * Fix bugs in numerical equality predicate.Mark H Weaver2013-07-161-27/+33
| | | | | | | | | | | | | | | | | | | | * libguile/numbers.c (scm_num_eq_p): Fix bug comparing fractions to infinities (reported by Göran Weinholt <goran@weinholt.se>). Fix erroneous comment describing the logic behind inum/flonum comparison. Use similar logic for inum/complex comparison to avoid rounding errors. Make minor indentation fixes and simplifications. * test-suite/tests/numbers.test (=): Add tests.
| * Fix rounding in scm_i_divide2double for negative arguments.Mark H Weaver2013-07-161-4/+11
| | | | | | | | | | | | | | | | | | | | * libguile/numbers.c (INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE): New macro. (scm_i_divide2double): Use INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE to determine if our fast path is safe. Previously, negative arguments were not checked properly. * test-suite/tests/numbers.test (exact->inexact): Add tests.
* | Merge remote-tracking branch 'origin/stable-2.0'Mark H Weaver2013-03-281-1/+30
|\|
| * Allow compilation with GMP < 5.0.0.Ludovic Courtès2013-03-281-1/+30
| | | | | | | | | | | | | | * libguile/numbers.c (VARARG_MPZ_ITERATOR)[!HAVE_DECL_MPZ_INITS]: New macro. (mpz_inits, mpz_clears)[!HAVE_DECL_MPZ_INITS]: New functions. * configure.ac: Check for the declaration of `mpz_inits'.
* | Merge remote-tracking branch 'origin/stable-2.0'Mark H Weaver2013-03-281-606/+1038
|\| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Conflicts: configure.ac libguile/deprecated.c libguile/deprecated.h libguile/filesys.h libguile/fluids.c libguile/fports.c libguile/gc.c libguile/guile.c libguile/numbers.c libguile/objcodes.c libguile/r6rs-ports.c libguile/smob.c libguile/socket.c libguile/threads.h module/language/scheme/decompile-tree-il.scm module/language/tree-il/peval.scm test-suite/tests/syncase.test
| * Improve sqrt handling of large integers and large and small rationals.Mark H Weaver2013-03-201-21/+112
| | | | | | | | | | | | | | | | | | | | | | | | | | * libguile/numbers.c (exact_integer_is_perfect_square, exact_integer_floor_square_root): New static functions. (scm_sqrt): Use SCM_LIKELY. Add 'scm_t_inum' variable in inum case to reduce the number of uses of SCM_I_INUM. Rename 'mpz_t' variable. Remove unneeded sign check. Handle bignums too large to fit in a double. Handle fractions too large or too small to fit in a normalized double. * test-suite/tests/numbers.test ("sqrt"): Add tests.
| * Optimize inum case of exact-integer-sqrt.Mark H Weaver2013-03-201-16/+8
| | | | | | | | | | * libguile/numbers.c (scm_exact_integer_sqrt): Use GMP for inum case. It is faster than what we had before.