summaryrefslogtreecommitdiffstats
path: root/arith.c
Commit message (Collapse)AuthorAgeFilesLines
* digits/digpow: recycle temporary conses.Kaz Kylheku2019-09-071-2/+3
| | | | | | | * arith.c (digcommon): Improved termination test in the second loop to avoid wasteful pop when the list of powers is empty. Use rcyc_pop to recycle these conses; they will be immediately reused for building the output list.
* digits/digpow: wrong results for radix powers.Kaz Kylheku2019-09-061-1/+1
| | | | | | * arith.c (digcommon): Fix off-by-one test causing incorrect results for radix powers like (digits 10) (digits 100) ... and analogously in other radices. Reported by vapnik spaknik.
* digits/digpow: disallow base 1.Kaz Kylheku2019-09-061-1/+1
| | | | | * arith.c (digcommon): Fix wrong test that allows base 1 to be admitted, resulting in infinitely looping behavior.
* ash: refactor in light of gc bug.Kaz Kylheku2019-06-191-24/+38
| | | | | | | | | * arith.c (ash): Introduce smaller scopes and move variable initializations close to their use. The NUM case of argument a no longer falls through to the BGNUM case, and doesn't cons up a bignum object. Rather, a temporary mp_int is allocated, used and freed. The gc_hint added in the previous commit is now gone.
* ash: gc problem.Kaz Kylheku2019-06-191-0/+1
| | | | | | | | | | | | | | | On 32 bit x86 Solaris 10, with gcc 4.9.0, this issue caused a miscompilation of the pset macro, due to ash abruptly returning zero, causing the op-code field of an instruction to be NOOP rather than GCALL. I'm committing this fix just for reference; I will immediately replace it with a refactoring of the function. * arith.c (ash): Add a gc_hint to prevent the a bignum from being reclaimed if the b = make_bignum() triggers gc. This happens in the case when the previous case computes a = make_bignum() and falls through.
* logcount: crash in 64 bit build.Kaz Kylheku2019-06-181-1/+1
| | | | | | | * arith.c (logcount): NUM case doesn't have a return statement in 64 bit build, statement causing fall-through to BGNUM case where the integer object is treated as a bignum. This bug has existed in the function ab initio.
* New function: bitset.Kaz Kylheku2019-06-181-1/+63
| | | | | | | | | | | * arith.c (bitset_s): New symbol variable. (bitset): New function. (arith_init): bitset_s initialized, bitset intrinsic registered. * lib.h (bitset): Declared. * txr.1: Documented bitset function and method.
* arith: INT_PTR_MIN overflow bugfix.Kaz Kylheku2019-06-031-1/+1
| | | | | * arith.c (int_flo): Don't subtract one from INT_PTR_MIN any more, since it is now the most negative integer already.
* arith: missing alloca.Kaz Kylheku2019-03-301-0/+1
| | | | | | * arith.c: we need to include "alloca.h" since we have moved some functions into here from lib.c which use "args.h" macros. This shows up on Cygwin and some other platforms.
* u-d-arithmetic: proper treatment of ceil & round.Kaz Kylheku2019-03-301-3/+21
| | | | | | | | | | | | | | | * arith.c (r_ceil_s, r_round_s): New symbol variables. (ceildiv, roundiv): Route binary cases involving struts directly to binary methods so the object is responsible for the complete implementation. (arith_init): Initialize r_ceil_s and r_round_s. * tests/016/ud-arith.tl (numbase): Binary methods added for ceil and round. Test cases added. * txr.1: Descriptions for binary ceil and round methods added; Notes about non-existence of binary methods removed from unary ceil and round removed.
* u-d-arithmetic: forgotten dispatch cases.Kaz Kylheku2019-03-301-0/+20
| | | | | * arith.c (plus, minus, mul, mod, floordiv): Dispatch reverse operaton for PTR/COBJ combination.
* Tests for user-defined arithmetic and fixes.Kaz Kylheku2019-03-301-29/+34
| | | | | | | | | | | | | | | | | | | | | | | * tests/016/ud-arith.expected b/tests/016/ud-arith.tl: New file. * tests/016/ud-arith.expected b/tests/016/ud-arith.expected: New file. * arith.c (divi): Bugfix: wrong argument tested for being a COBJ. (logtrunc): Fix incorrect method call: calling r-logtrunc-s for the object-in-left-position case. (sign_extend): Fix semantics not following documentation: dispatch method with original arguments. (divv): When there is just one argument, take advantage of the hitherto unused unary case of divi rather than giving it both arguments. The object dispatch is in that unary case, so we need it now. (arith_init): Fix wrong name of r_lognot_s symbol. * txr.1: Fix atan2 being documented as atan. Fix misspelling of r-lognot as lognot-r.
* New feature: user-defined math.Kaz Kylheku2019-03-301-135/+532
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Most of the library now accepts struct objects as arguments, relying on them to implement methods. * arith.c (minus_s, inv_minus_s, neg_s, abs_s, signum_s, mul_s, div_s, recip_s, inv_div_s, trunc1_s, trunc_s, r_trunc_s, mod_s, r_mod_s, zerop_s, plusp_s, minusp_s, evenp_s, oddp_s, gt_s, lt_s, ge_s, le_s, numeq_s, expt_s, r_expt_s, exptmod_s, isqrt_s, square_s, floor_s, floor1_s, r_floor_s, ceil_s, ceil1_s, round_s, round1_s, sin_s, cos_s, tan_s, asin_s, acos_s, atan_s, atan2_s, r_atan2_s, log_s, log2_s, log10_s, exp_s, sqrt_s, logand_s, logior_s, logxor_s, lognot1_s, lognot_s, r_lognot_s, logtrunc_s, r_logtrunc_s, sign_extend_s, ash_s, bit_s, width_s, logcount_s): New symbol variables. (not_struct_error, method_error, do_unary_method, do_binary_method, do_ternary_method): New static functions. (plus, minus, neg, abso, signum, mul, trunc1, trunc, mod, floordiv, round1, roundiv, divi, zerop, plusp, minusp, evenp, oddp, gt, lt, ge, le, numeq, expt, exptmod, isqrt, square, floorf, ceili, since, cosi, tang, asine, atang, atang2, loga, logten, logtwo, expo, sqroot, logand, logior, logxor, comp_trunc, lognot, sign_extend, ash, bit, logcount, width, bits, unary_num, unary_arith): Support struct arguments. (plusv, minusv, mulv, divv, sumv, prodv, gtv, ltv, gev, lev, numeqv, exptv, logandv, logiorv): Use symbol for self instead of string lit. (arith_init): Initialize new symbol variables. Replace existing intern calls in function registrations with references to some of these symbol variables. * txr.1: Documented.
* plus: missing breaks.Kaz Kylheku2019-03-251-0/+2
| | | | | | | | | * arith.c (plus): Add missing break statements to two switch cases. One is at the end, where it is not strictly necessary. The other omission is harmless also and may have been deliberate, though not commented by a /* fallthrough */. The other functions with this same pattern, namely minus, has the breaks.
* Move numeric functions from lib.c to arith.cKaz Kylheku2019-03-251-0/+445
| | | | | | | | | | | | | | | * arith.c, lib.c (num, c_num, c_fixnum, bad_float, flo, c_flo, fixnump, bignump, integerp, floatp, numberp nary_op, nary_simple_op, plusv, minusv, mulv, divv, logandv, logiorv, gtv, ltv, gev, lev, numeqv, numneqv, sumv, prod, exptv, gcdv, lcmv): Function definitions moved from lib.c to arith.c. (nary_op_keyfun, unary_num, unary_arith, unary_int, sumv, prodv, rexpt, abso_self): Static functions moved from libmoved from lib.c to arith.c. (max): New macro in arith.c. (arith_init): Registrations moved from eval.c. * eval.c (eval_init): Registrations moved to arith.c
* Register arithmetic functions in arith module.Kaz Kylheku2019-03-251-0/+49
| | | | | | | | * arith.c (plus_s): Definition moved here. (arith_init): Register numerous math intrinsics here. * eval.c (plus_s): Definition removed. (eval_init): Numerous math function registrations removed.
* expt: handle negative integer exponents with integer bases.Kaz Kylheku2019-03-091-17/+53
| | | | | | | | | * arith.c (expt): The function overhauled. Raising integers to negative integers now works. Raising zero to a negative is diagnosed as a division by zero for operands of all kinds. * txr.1: Documentation updated for expt, and also division by zero error is documented for the / function.
* mpi/arith: optimize "highest bit" with GCC builtins.Kaz Kylheku2019-02-191-1/+7
| | | | | | * arith.c (highest_bit): On GCC, use __builtin_clz. * mpi/mpi.c (s_highest_bit): Likewise.
* mul: add forgotten MPI error check.Kaz Kylheku2019-01-251-1/+4
| | | | | | * arith.c (mul): In code that is only compiled when HAVE_DOUBLE_INTPTR_T is missing/zero, we need to test the return value of MPI's mp_mul.
* Extend infrastructure for double_intptr_t.Kaz Kylheku2019-01-251-0/+47
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | We support an unsigned version of the type, and add functions for converting between Lisp values and both types. * arith.c (bignum_dbl_uipt): New function, unsigned companion to existing bignum_dbl_ipt. (c_dbl_num, c_dbl_unum): New functions. * arith.h (bignum_dbl_uipt, c_dbl_num, c_dbl_unum): Declared. * configure (superulong_t, SIZEOF_DOUBLE_INTPTR, DOUBLE_INTPTR_MAX, DOUBLE_INTPTR_MIN, DOUBLE_UINTPTR_MAX, double_uintptr_t): New definitions going into config.h. * lib.h (dbl_cnum, dbl_ucnum): New typedefs: double-sized analogs of cnum and ucnum. * mpi/mpi.c (mp_set_double_uintptr, mp_get_double_uintptr, mp_get_double_intptr): New functions. (s_mp_in_big_range): New static function. (mp_in_double_intptr_range, mp_in_double_uintptr_range): New functions. * mpi/mpi.h (mp_set_double_uintptr, mp_get_double_intptr, mp_get_double_uintptr, mp_in_double_intptr_range, mp_in_double_uintptr_range): Declared.
* Provide faster bignum-in-fixed-integer range tests in MPI.Kaz Kylheku2019-01-251-39/+2
| | | | | | | | | | | | | | | | | | | | | | | * mpi/mpi.c (mp_in_range, mp_in_intptr_range, mp_in_uintptr_range): New functions. * mpi/mpi.h (mp_in_range, mp_in_intptr_range, mp_in_uintptr_range): Declared. * arith.c (NUM_MAX_MP, INT_PTR_MAX_MP, UINT_PTR_MAX_MP, INT_PTR_MAX_SUCC_MP): Static variables removed. Note that INT_PTR_MAX_MP was not used at all! (normalize): Use mp_in_range instead magnitude comparison to NUM_MAX_MP. (in_int_ptr_range, in_uint_ptr_range): Static functions removed. (c_unum): Use mp_in_uintptr_range instead of in_uint_ptr_range. (arith_init): Remove initializations of removed variables. (arith_free_all): Remove cleanup of removed variables, leaving function empty. * lib.c (c_num): Use mp_in_intptr_range instead of in_int_ptr_range.
* lib: revise wording of integer range errors.Kaz Kylheku2019-01-241-1/+2
| | | | | | | | * arith.c (c_unum): Fix misleading error message, and instead specify the range that was violated. * lib.c (c_num): Similar change: don't refer to a 'cnum range' which means nothing to the user.
* Fix some instances of 4 bytes = 32 bits assumption.Kaz Kylheku2019-01-231-5/+5
| | | | | | | | | | | | | | | | | * hash.c (equal_hash, eql_hash, cobj_eq_hash_op, hash_hash_op): Multiply object size by CHAR_BIT and switch on number of bits, rather than bytes. * sysif.c (off_t_num): Likewise. * arith.c, ffi.c, itypes.c, rand.c: In numerous #if directive, fix size tests from bytes to bits. * configure: in the test that detects integer types, and in the test for enabling large file offsets, detect one more variable from the system: the value of CHAR_BIT. This turns into SIZEOF_BYTE. We use that value instead of a hard-coded 8.
* sysif: use double-intptr function from arith.Kaz Kylheku2019-01-231-1/+1
| | | | | | | | | | | * arith.c (bignum_dbl_ipt): Change internal function to external linkage. * arith.h (bignum_dbl_ipt): Conditionally declared. * sysif.c (num_off_t): Use bignum_dbl_ipt instead of open-coding exactly the same thing that bignum_dbl_ipt does. Abort is changed to same internal_error as in off_t_num.
* mpi: put access macros into mp_ namespaceKaz Kylheku2019-01-221-9/+9
| | | | | | | | | | | | | | | | | | * mpi/mpi.h (mp_sign, mp_isneg, mp_used, mp_alloc, mp_digits, mp_digit): New macros, based on renaming SIGN, ISNEG, USED, ALLOC, DIGITS and DIGIT. (mp_set_prec): Use mp_digits instead of DIGITS. * mpi/mpi.c (SIGN, ISNEG, USED, ALLOC, DIGITS, DIGIT): Macros defined here now, as local aliases for their namespaced counterparts. * arith.c (signum, floordiv): Replace ISNEG with mp_isneg. * rand.c (random): Replace ISNEG with mp_isneg. * sysif.c (off_t_num): Replace USED, DIGITS and ISNEG with mp_used, mp_digits and mp_isneg.
* mpi: hardening; bug found.Kaz Kylheku2019-01-181-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The flaw in the rand function caused by mp_count_ones is serious. For instance calls to (rand 3000000000) are expected to produce evenly distributed values in the range zero to one less than 3 billion. Due to this flaw, only values in the range [0, 2147483648) are produced. The function is wrongly informed that 3000000000 is a power of two, and so it subtracts one from the number of bits needed for the clamping bit mask, causing raw random values to be clamped to 31 bits instead of 32. * arith.c (logcount): Use mp_err to capture return value from mp_count_ones, rather than mp_size. If the result is less than zero, throw an internal error. (The only non-internal error indication that could come from this function is a memory allocation, and we already use chk_malloc under MPI). * mpi/mpi.c (mp_count_ones): Switch return type to mp_err, because this function returns error codes, which are negative. (mp_is_pow_two): Fix broken test that is always true. This affects our random number module; it's used in the random function for bignum moduli. (mp_toradix_case): Use unsigned char temporary for exchanging two unsigned chars, rather than a char temporary. (s_mp_div_d): Assert that the partial quotient t and remainder w, of mp_word_type, have values that fit into the mp_digit variables to which they are being assigned. (s_mp_ispow2d): The result of s_highest_bit(d) is unsigned, so if we subtract 1 from 0, we create a large value which likely converts to -1. Let's ensure that clearly with a cast to (int) of the return value. * mpi/mpi.h (mp_count_ones): Declaration updated. * mpi/mplogic.c (mpl_rsh, mpl_lsh): Fix two places where we shift the constant 1 (of type int) left by a number of bits that can exceed the type int. Note, that these functions are currently not called anywhere in TXR.
* int-flo: take advantage of unsigned range.Kaz Kylheku2019-01-181-0/+3
| | | | | | | | * arith.c (int_flo): If the floating point value is in the ucnum range, we can convert to integer by way of that type; we gain a little bit of range. For instance on 32 bits, floating values in the range 2147483648.0 to 4294967295.0 can be converted via the fast path.
* int-flo: fix always-false test.Kaz Kylheku2019-01-181-1/+1
| | | | | | | | * arith.c (int_flo): Fix reversed inequality tests which cause the test to be always false. This always false test prevents use of the faster code path for converting floating-point point values that are in range of the cptr type.
* Copyright year bump 2019.Kaz Kylheku2019-01-161-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * LICENSE, LICENSE-CYG, METALICENSE, Makefile, args.c, args.h, arith.c, arith.h, buf.c, buf.h, cadr.c, cadr.h, combi.c, combi.h, configure, debug.c, debug.h, eval.c, eval.h, ffi.c, ffi.h, filter.c, filter.h, ftw.h, gc.c, gc.h, glob.c, glob.h, hash.c, hash.h, itypes.c, itypes.h, jmp.S, lib.c, lib.h, lisplib.c, lisplib.h, match.c, match.h, parser.c, parser.h, parser.l, parser.y, protsym.c, rand.c, rand.h, regex.c, regex.h, share/txr/stdlib/asm.tl, share/txr/stdlib/awk.tl, share/txr/stdlib/build.tl, share/txr/stdlib/cadr.tl, share/txr/stdlib/compiler.tl, share/txr/stdlib/conv.tl, share/txr/stdlib/doloop.tl, share/txr/stdlib/error.tl, share/txr/stdlib/except.tl, share/txr/stdlib/ffi.tl, share/txr/stdlib/getopts.tl, share/txr/stdlib/getput.tl, share/txr/stdlib/hash.tl, share/txr/stdlib/ifa.tl, share/txr/stdlib/keyparams.tl, share/txr/stdlib/op.tl, share/txr/stdlib/package.tl, share/txr/stdlib/path-test.tl, share/txr/stdlib/place.tl, share/txr/stdlib/pmac.tl, share/txr/stdlib/socket.tl, share/txr/stdlib/stream-wrap.tl, share/txr/stdlib/struct.tl, share/txr/stdlib/tagbody.tl, share/txr/stdlib/termios.tl, share/txr/stdlib/trace.tl, share/txr/stdlib/txr-case.tl, share/txr/stdlib/type.tl, share/txr/stdlib/vm-param.tl, share/txr/stdlib/with-resources.tl, share/txr/stdlib/with-stream.tl, share/txr/stdlib/yield.tl, signal.c, signal.h, socket.c, socket.h, stream.c, stream.h, struct.c, struct.h, strudel.c, strudel.h, sysif.c, sysif.h, syslog.c, syslog.h, termios.c, termios.h, txr.1, txr.c, txr.h, unwind.c, unwind.h, utf8.c, utf8.h, vm.c, vm.h, vmop.h, win/cleansvg.txr: Extended Copyright line to 2018.
* New function: square.Kaz Kylheku2019-01-051-0/+53
| | | | | | | | | | | | | The square function calulates (* x x) but is faster for bignum integers by taking advantage of mp_sqr. * arith.c (square): New function. * eval.c (eval_init): Register square as intrinsic. * lib.h (square): Declared. * txr.1: Documented.
* nzerop: new function.Kaz Kylheku2018-12-131-0/+22
| | | | | | | | | | * arith.c (nzerop): New function. * eval.c (eval_init): Register nzerop intrinsic. * lib.h (nzerop): Declared. * txr.1: Documented.
* Use tnil instead of if2 in arithmetic predicates.Kaz Kylheku2018-12-131-9/+9
| | | | | * arith.c (zerop, plusp, minusp): style: if2(expr, t) should be using tnil(expr).
* plusp: wrong name in error message.Kaz Kylheku2018-12-131-1/+1
| | | | * arith.c (plusp): plusp wrongly reports itself as zerop.
* Streamline logand and logior slightly.Kaz Kylheku2018-11-251-18/+4
| | | | | | | | * arith.c (logand, logior): Remove wasteful zerop tests which involve a type check. Also don't check for a == b equivalence in CHR and NUM cases; this is a rare case that just adds to the cycles and instruction count. Blindly copying and pasting this code is what led to the bug in logxor.
* logxor: fix seriously broken function.Kaz Kylheku2018-11-251-0/+43
| | | | | | | | | | | | | Reported by Guillaume le Vaillant. * arith.c (logxor): Fix broken behavior when the arguments are the same nonzero fixnum, or the same bignum object. (logxor_old): New function: verbatim copy of previous logxor. * eval.c (eval_init): Register logxor intrinsic to the broken function if compatibility is 202 or less. * txr.1: Compat note added.
* compiler: use binary versions of common math functions.Kaz Kylheku2018-11-161-0/+11
| | | | | | | | | | | | | | | * arith.c (arith_init): Register functions in the sys package: b<, b>, b<=, b=, b+, b-, b*, b/ and neg. * share/txr/stdlib/compiler.tl (%nary-ops%, %bin-ops%, %bin-op%): New global variables. (compiler comp-fun-form): Transform two-argument calls to any of the variadic functions in %nary-ops% functions into calls to their binary counterpart. These calls are faster, since they bypass the wrapper which deals with the variable argument list. Also, we detect unary - and map it to the new sys:neg function, and reduce the one-argument cases of certain functions to noops.
* math: remove redundant type checks from NUM.Kaz Kylheku2018-11-161-118/+118
| | | | | | | | | | | | * arith.c (plus, minus, neg, abso, signum, mul, trunc, mod, floordiv, plusp, minusp, evenp, oddp, gt, lt, ge, le, numeq, expt, exptmod, isqrt, gcd, flo_int, logand, logior, logxor, comp_trunc, lognot, logtrunc, sign_extend, ash, bit, logcount, tofloat, toint, width, poly, rpoly): Use the unchecked c_n rather than c_num on quantities that are known to be of NUM and CHR type. * lib.h (c_n): New inline function.
* type_check: take function name arg.Kaz Kylheku2018-11-071-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * arith.c (flo_int): Pass down name to type_check. * eval.c (copy_env, env_fbind, env_vbind, env_vb_to_fb, func_get_name, lexical_var_p, lexical_fun_p, lexical_lisp1_binding, squash_menv_deleting_range, op_upenv): Pass relevant Lisp function name to type_check. (lookup_global_var, lookup_sym_lisp1, lookup_fun, lookup_mac, lookup_symac, lookup_symac_lisp1): For these widely used functions, pass situational prefix in place of function name. They may get a funtion name argument in the future. * gc.c (gc_finalize): Pass function name to type_check. * lib.c (throw_mismatch): Take function nme argument, incorporate into mesage. (lcons_fun, c_flo, string_extend, symbol_name, symbol_package, get_package, package_name, func_get_form, func_get_env, func_set_env, vec_set_length, length_vec, size_vec, list_vec, lay_str_force, lay_str_force_upto, lazy_str_get_trailing_list, from, too, set_from, set_to): Pass relevant Lisp function name to type_check. (symbol_setname, symbol_visible): Pass indication of internal error into type_check, since this doesn't pertain to any Lisp function being wrong. * lib.h (throw_mismatch): Declaration updated. (type_check): Take new parameter and pass down to throw_mismatch. * signal.c (set_sig_handler): Pass name down to type_check.
* math: improve error diagnosis.Kaz Kylheku2018-11-071-116/+191
| | | | | | | | | | | | | | | | | | | | | | | | | | More streamlined code, better identification of functions. * arith.c (not_number, not_integer, invalid_ops, invalid_op, divzero): New static functions. (num_to_buffer, bugnum_len, plus, minus, neg, abso, signum, mul, trunc1, mod, floordiv, round1, roundiv, divi, zerop, plusp, minusp, evenp, oddp, gt, lt, ge, le, numeq, expt, exptmod, floorf, ceili, sine, cosi, tang, asine, acosi, atang, loga, logten, logtwo, expo, sqroot, int_flo, flo_int, cum_norm_dist, inv_cum_norm): Establish function's Lisp name as self variable. Use new static functions for reporting common errors. Pass function name to new argument of c_flo function. * buf.c (buf_put_float, buf_put_double): Pass function's Lisp name to c_flo function. * ffi.c (ffi_float_put, ffi_double_put): Likewise. * lib.c (c_flo): Takes new argument, name of calling function. * lib.h (c_flo): Declaration updated. * stream.c (formatv): Pass function name to c_flo.
* Hide deprecated, undocumented variables.Kaz Kylheku2018-10-301-6/+13
| | | | | | | | | | * arith.c (arith_init): Do not define *flo-dig*, *flo-max*, *flo-min*, *flo-epsilon*, *pi* and *e* unless compatibility with TXR 199 or earlier is requested. * txr.c (txr_main): Likewise for *self-path*. * txr.1: Compat note added.
* New function: signum.Kaz Kylheku2018-08-071-0/+22
| | | | | | | arith.c (signum): New function. (arith_init): signum intrinsic registered. * txr.1: Documented.
* feature: support for floating-point rounding control.Kaz Kylheku2018-07-261-0/+29
| | | | | | | | | | | | | | | * arith.c (flo_get_round_mode, flo_set_round_mode): New functions. (arith_init): Register global lexical variables flo-near, flo-down, flo-up and flo-zero. Register flo-get-round-mode and flo-set-round-mode intrinsic functions. * configure: Test for fesetround and fegetround variables, and the associated constants, prpoducing a HAVE_ROUNDING_CTL_H variable in config.h. * txr.1: Documented new variables and functions.
* logcount: new function.Kaz Kylheku2018-05-181-0/+40
| | | | | | | | | | | | | | | | | | This is in ANSI CL; potentially useful and hard to implement efficiently in user code. * arith.c (logcount): New function. * eval.c (eval_init): Register logcount intrinsic. * lib.h (logcount): Declared. * mpi/mi.c (s_mp_count_ones): New static function. (mp_count_ones): New function. * mpi/mpi.h (mp_count_ones): Declared. * txr.1: Documented.
* width: misleading error message.Kaz Kylheku2018-05-181-1/+1
| | | | | * arith.c (width): Fix incorrect name in type error diagnostic. There is no such function as integer-length.
* bugfix: include most negative two's in cnum range.Kaz Kylheku2018-04-291-2/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The INT_PTR_MIN value isn't the smallest value actually representable in cnum, because it is just the additive inverse of INT_PTR_MAX. In two's complement, the INT_PTR_MIN-1 value is still representable. But we are excluding it. If a Lisp integer has the value INT_PTR_MIN-1, the c_num function will fail to convert it to a cnum. This situation occurs in FFI, where code may expect that the Lisp value #x-80000000 can convert to an external 32 bit integer type. This will be done by way of a conversion to cnum first via c_num (see ffi_i32_put for instance, which calls c_i32 which relies on c_num). * arith.c (INT_PTR_MAX_SUCC_MP): New static variable. This holds a bignum equivalent to INT_PTR_MAX + 1. (in_int_ptr_range): We now check whether the value against the range [-(INT_PTR_MAX + 1), (INT_PTR_MAX + 1)] and then check all possible return values. The MP_LT case is clearly in range, and MP_GT is out of the range. The interesting situation is MP_EQ: in that case we just test the sign to see whether we are looking at -(INT_PTR_MAX + 1). (int_flo): INT_PTR_MIN is referenced in this function, so we use INT_PTR_MIN - 1 instead. This allows that value to be handled via the simple bignum(n) case. (arith_init): Initialize INT_PTR_MAX_SUCC_MP. We cannot initialize it to INT_PTR_MAX + 1 directly because that expression overflows: insted we use INT_PTR_MIN - 1 and then flip the resulting bignum's sign. (arith_free_all): Call mp_clear on the new variable to release its digit buffer. * ffi.c (make_ffi_type_enum): Use INT_PTR_MIN - 1 as the initial value of the highest variable, to accurately calculate the range of the enum values if they contain INT_PTR_MIN - 1.
* code review: switch case breaks.Kaz Kylheku2018-03-081-0/+3
| | | | | | | | | | * arith.c (c_unum): Add fallthrough comment. (minus): Add missing break after case that handles char minus heap object.. This luckily isn't a bug because type(anum) isn't RNG, and so when it falls through, the next case also falls through. * lib.c (car): Add missing fallthrough comment.
* New function: bignum-len.Kaz Kylheku2018-03-061-0/+13
| | | | | | | | | * arith.c (bignum_len): Wew function. (arith_init): Register bignum-len intrinsic. * arith.h (bignum_len): Declared. * txr.1: Documented.
* Copyright year bump 2018.Kaz Kylheku2018-02-151-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * LICENSE, LICENSE-CYG, METALICENSE, Makefile, args.c, args.h, arith.c, arith.h, buf.c, buf.h, cadr.c, cadr.h, combi.c, combi.h, configure, debug.c, debug.h, eval.c, eval.h, ffi.c, ffi.h, filter.c, filter.h, ftw.c, ftw.h, gc.c, gc.h, glob.c, glob.h, hash.c, hash.h, itypes.c, itypes.h, jmp.S, lib.c, lib.h, lisplib.c, lisplib.h, match.c, match.h, parser.c, parser.h, parser.l, parser.y, protsym.c, rand.c, rand.h, regex.c, regex.h, share/txr/stdlib/awk.tl, share/txr/stdlib/build.tl, share/txr/stdlib/cadr.tl, share/txr/stdlib/conv.tl, share/txr/stdlib/doloop.tl, share/txr/stdlib/error.tl, share/txr/stdlib/except.tl, share/txr/stdlib/ffi.tl, share/txr/stdlib/getopts.tl, share/txr/stdlib/getput.tl, share/txr/stdlib/hash.tl, share/txr/stdlib/ifa.tl, share/txr/stdlib/keyparams.tl, share/txr/stdlib/op.tl, share/txr/stdlib/package.tl, share/txr/stdlib/path-test.tl, share/txr/stdlib/place.tl, share/txr/stdlib/pmac.tl, share/txr/stdlib/socket.tl, share/txr/stdlib/stream-wrap.tl, share/txr/stdlib/struct.tl, share/txr/stdlib/tagbody.tl, share/txr/stdlib/termios.tl, share/txr/stdlib/txr-case.tl, share/txr/stdlib/type.tl, share/txr/stdlib/with-resources.tl, share/txr/stdlib/with-stream.tl, share/txr/stdlib/yield.tl, signal.c, signal.h, socket.c, socket.h, stream.c, stream.h, struct.c, struct.h, strudel.c, strudel.h, sysif.c, sysif.h, syslog.c, syslog.h, termios.c, termios.h, txr.1, txr.c, txr.h, unwind.c, unwind.h, utf8.c, utf8.h, win/cleansvg.txr: Extended Copyright line to 2018.
* New functions for polynomial evaluation.Kaz Kylheku2017-09-051-0/+88
| | | | | | | | | * arith.c (poly, rpoly): New functions. (arith_init): Registered intrinsics poly and rpoly. * arith.h (poly, rpoly): Declared. * txr.1: Documented.
* New function: inverse of cumulative normal dist.Kaz Kylheku2017-08-311-0/+24
| | | | | | | | | | * arith.c (inv_cum_norm): New function. * arith.h (inv_cum_norm): Declared. * eval.c (eval_init): Register inv-cum-norm intrinsic. * txr.1: Documented.