summaryrefslogtreecommitdiffstats
path: root/arith.c
Commit message (Collapse)AuthorAgeFilesLines
* Copyright year bump 2021.Kaz Kylheku2021-01-141-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * METALICENSE: 2020 copyrights bumped to 2021. Added note about SHA-256 routines from Colin Percival. * LICENSE, LICENSE-CYG, Makefile, alloca.h, args.c, args.h, arith.c, arith.h, buf.c, buf.h, cadr.c, cadr.h, chksum.c, chksum.h, chksums/crc32.c, chksums/crc32.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, lex.yy.c.shipped, lib.c, lib.h, linenoise/linenoise.c, linenoise/linenoise.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/copy-file.tl, share/txr/stdlib/debugger.tl, share/txr/stdlib/defset.tl, share/txr/stdlib/doloop.tl, share/txr/stdlib/each-prod.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/param.tl, share/txr/stdlib/path-test.tl, share/txr/stdlib/place.tl, share/txr/stdlib/pmac.tl, share/txr/stdlib/quips.tl, share/txr/stdlib/save-exe.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, time.c, time.h, tree.c, tree.h, txr.1, txr.c, txr.h, unwind.c, unwind.h, utf8.c, utf8.h, vm.c, vm.h, vmop.h, win/cleansvg.txr, y.tab.c.shipped: Copyright year bumped to 2021.
* int-flo: bugfix on 64 bitKaz Kylheku2020-12-221-2/+9
| | | | | | | | | | | | | | | * arith.c (int_flo): On 64 bit, we incorrectly handle the positive floating-point values which correspond to (expt 2 63) and (expt 2 64). This is because in the range check which detects whether a double value lands into the cnum or ucnum range, the 64 bit INT_PTR_MAX and UINT_PTR_MAX have no exact equivalent in the double type and are being converted to double values which are greater. That then causes the range check to incorrectly include double values that are actually out of range for the conversion to the cnum or ucnum type, causing bogus values. The quickest fix would be to use < comparison for a half-open range, but that sacrifices two double values on 32 bit, unnecessarily sending them to the text-based conversion code. So instead, let's subtract margins from the range constants on 64 bit.
* Change noreturn to NORETURN.Kaz Kylheku2020-08-071-7/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The noreturn macro is respelled to harmonize with the upper-case INLINE and NOINLINE. * lib.h (noreturn): Rename to NORETURN. * arith.c (not_number, not_integer, invalid_ops, invalid_op): Declaration updated. * arith.c (do_mp_error): Likewise. * eval.c (eval_error, no_bindable_error, dotted_form_error): Likewise. * eval.h (eval_error): Likewise. * lib.c (unsup_obj, callerror, wrongargs): Likewise. * match.c (sem_error): Likewise. * stream.c (unimpl, unimpl_put_string, unimpl_put_char, unimpl_put_byte, unimpl_get_line, unimpl_get_char, unimpl_get_byte, unimpl_unget_char, unimpl_unget_byte, unimpl_put_buf, unimpl_fill_buf, unimpl_seek, unimpl_truncate, unimpl_get_sock_family, unimpl_get_sock_type, unimpl_get_sock_peer, unimpl_set_sock_peer): Likewise. * struct.c (no_such_struct, no_such_slot, no_such_static_slot): Likewise. * unwind.h (jmp_restore, uw_throw, uw_throwf, uw_errorf, uw_errorfv, type_mismatch): Likewise.
* c_num: now takes self argument.Kaz Kylheku2020-06-291-10/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The c_num and c_unum functions now take a self argument for identifying the calling function. This requires changes in a large number of places. In a few places, additional functions acquire a self argument. The ffi module has the most extensive example of this. Some functions mention their name in a larger string, or have scattered literals giving their name; with the introduction of the self local variable, these are replaced by references to self. In the following changelog, the notation TS stands for "take self argument", meaning that the functions acquires a new "val self" argument. The notation DS means "define self": the functions in question defines a self variable, which they pass down. The notation PS means that the functions pass down an existing self variable to functions that now require it. * args.h (args_count): TS. * arith.c (c_unum, c_num): TS. (toint, exptv): DS. * buf.c (buf_check_len, buf_check_alloc_size, buf_check_index, buf_do_set_len, replace_buf, buf_put_buf, buf_put_i8, buf_put_u8, buf_put_char, buf_put_uchar, buf_get_bytes, buf_get_i8, buf_get_u8, buf_get_cptr, buf_strm_get_byte_callback, buf_strm_unget_byte, buf_swap32, str_buf, buf_int, buf_uint, int_buf, uint_buf): PS. (make_duplicate_buf, buf_shrink, sub_buf, buf_print, buf_pprint): DS. * chskum.c (sha256_stream_impl, sha256_buf, crc32_buf, md5_stream_impl, md5_buf): TS. (chksum_ensure_buf, sha256_stream, sha256, sha256_hash, md5_stream, md5, md5_hash): PS. (crc32_stream): DS. * combi.c (perm_while_fun, perm_gen_fun_common, perm_str_gen_fun, rperm_gen_fun, comb_vec_gen_fun, comb_str_gen_fun, rcomb_vec_gen_fun, rcomb_str_gen_fun): DS. * diff.c (dbg_clear, dbg_set, dbg_restore): DS. * eval.c (do_eval, gather_free_refs, maprodv, maprendv, maprodo, do_args_apf, do_args_ipf): DS. (op_dwim, me_op, map_common): PS. (prod_common): TS. * ffi.c (struct txr_ffi_type): release member TS. (make_ffi_type_pointer): PS and release argument TS. (ffi_varray_dynsize, ffi_array_in, ffi_array_put_common, ffi_array_get_common, ffi_varray_in, ffi_varray_null_term): PS. (ffi_simple_release, ffi_ptr_in_release, ffi_struct_release, ffi_wchar_array_get, ffi_array_release_common, ffi_array_release, ffi_varray_release): TS. (ffi_float_put, double_put, ffi_be_i16_put, ffi_be_u16_put, ffi_le_i16_put, ffi_le_u16_put, ffi_be_i32_put, ffi_be_u32_put, ffi_le_i32_put, ffi_sbit_put, ffi_ubit_put, ffi_buf_d_put, make_ffi_type_array, make_ffi_type_enum, ffi_type_compile, make_ffi_type_desc, ffi_make_call_desc, ffi_call_wrap, ffi_closure_dispatch_save, ffi_put_into, ffi_in, ffi_get, ffi_put, carray_set_length, carray_blank, carray_buf, carray_buf_sync, carray_cptr, carray_refset, carray_sub, carray_replace, carray_uint, carray_int): PS. (carray_vec, carray_list): DS. * filter.c (url_encode, url_decode, base64_stream_enc_impl): DS. * ftw.c (ftw_callback, ftw_wrap): DS. * gc.c (mark_obj, gc_set_delta): DS. * glob.c (glob_wrap): DS. * hash.c (equal_hash, eql_hash, eq_hash, do_make_hash, hash_equal, set_hash_traversal_limit, gen_hash_seed): DS. * itypes.c (c_i8, c_u8, c_i16, c_u16, c_i32, c_u32, c_i64, c_u64, c_short, c_ushort, c_int, c_uint, c_long, c_ulong): PS. * lib.c (seq_iter_rewind): TS and becomes internal. (seq_iter_init_with_info, seq_setpos, replace_str, less, replace_vec, diff, isec, obj_print_impl): PS. (nthcdr, equal, mkstring, mkustring, upcase_str, downcase_str, search_str, sub_str, cat_str, scat2, scat3, fmt_join, split_str_keep, split_str_set, trim_str, int_str, chr_int, chr_str, chr_str_set, vector, vecref, vecref_l, list_vec, copy_vec, sub_vec, cat_vec, lazy_str_put, lazy_str_gt, length_str_ge, length_str_lt, length_str_le, cptr_size_hint, cptr_int, out_lazy_str, out_quasi_str, time_string_local_time, time_string_utc, time_fields_local_time, time_fields_utc, time_struct_local, time_struct_utc, make_time, time_meth, time_parse_meth): DS. (init_str, cat_str_init, cat_str_measure, cat_str_append, vscat, time_fields_to_tm, time_struct_to_tm, make_time_impl): TS. * lib.h (seq_iter_rewind): Declaration removed. (c_num, c_unum, init_str): Declarations updated. * match.c (LOG_MISMATCH, LOG_MATCH): PS. (h_skip, h_coll, do_output_line, do_output, v_skip, v_fuzz, v_collect): DS. * parser.c (parser, circ_backpatch, report_security_problem, hist_save, repl, lino_fileno, lino_getch, lineno_getl, lineno_gets, lineno_open): DS. (parser_set_lineno, lisp_parse_impl): PS. * parser.l (YY_INPUT): PS. * rand.c (make_random_state): PS. * regex.c (print_rec): DS. (search_regex): PS. * signal.c (kill_wrap, raise_wrap, get_sig_handler, getitimer_wrap, setitimer_wrap): DS. * socket.c (addrinfo_in, sockaddr_pack, fd_timeout, to_connect, open_sockfd, sock_mark_connected, sock_timeout): TS. (getaddrinfo_wrap, dgram_set_sock_peer, sock_bind, sock_connect, sock_listen, sock_accept, sock_shutdown, sock_send_timeout, sock_recv_timeout, socketpair_wrap): DS. * stream.c (generic_fill_buf, errno_to_string, stdio_truncate, string_out_put_string, open_fileno, open_command, base_name, dir-name): DS. (unget_byte, put_buf, fill_buf, fill_buf_adjust, get_line_as_buf, formatv, put_byte, test_set_indent_mode, test_neq_set_indent_mode, set_indent_mode, set_indent, inc_indent, set_max_length, set_max_depth, open_subprocess, run ): PS. (fds_subst, fds_swizzle): TS. * struct.c (make_struct_type, super, umethod_args_fun): PS. (method_args_fun): DS. * strudel.c (strudel_put_buf, strudel_fill_buf): DS. * sysif.c (errno_wrap, exit_wrap, usleep_wrap, mkdir_wrap, ensure_dir, makedev_wrap, minor_wrap, major_wrap, mknod_wrap, mkfifo_wrap, wait_wrap, wifexited, wexitstatus, wifsignaled, wtermsig, wcoredump, wifstopped, wstopsig, wifcontinued, dup_wrap, close_wrap, exit_star_wrap, umask_wrap, setuid_wrap, seteuid_wrap, setgid_wrap, setegid_wrap, simulate_setuid_setgid, getpwuid_wrap, fnmatch_wrap, dlopen_wrap): DS. (chmod_wrap, do_chown, flock_pack, do_utimes, poll_wrap, setgroups_wrap, setresuid_wrap, setresgid_wrap, getgrgid_wrap): PS. (c_time): TS. * sysif.h (c_time): Declaration updated. * syslog.c (openlog_wrap, syslog_wrap): DS. * termios.c (termios_pack): TS. (tcgetattr_wrap, tcsetattr_wrap, tcsendbreak_wrap, tcdrain_wrap, tcflush_wrap, tcflow_rap, encode_speeds, decode_speeds): DS. * txr.c (compato, array_dim, gc_delta): DS. * unwind.c (uw_find_frames_by_mask): DS. * vm.c (vm_make_desc): PS. (vm_make_closure, vm_swtch): DS.
* Remove unnecessary #include directives.Kaz Kylheku2020-04-221-1/+0
| | | | | | | | | | Time for some spring cleaning. * args.c, arith.c, buf.c, cadr.c, chksum.c, debug.c, ftw.c, gc.c, gencadr.txr, glob.c, hash.c, lisplib.c, match.c, parser.c, parser.l, parser.y, rand.c, signal.c, stream.c, strudel.c, syslog.c, tree.c, unwind.c, utf8.c, vm.c: Numerous unnecessary #include directives removed.
* Reduce consing in /= function.Kaz Kylheku2020-04-211-10/+25
| | | | | | | | * arith.c (numneqv): Do not unconditionally cons all the arguments into a list. If the arguments structure has no trailing list, then just loop over the arguments in the arg array. Only cons all the arguments into a list if there is a trailing list.
* Extending =, <, >, <= and >= to work on sequences.Kaz Kylheku2020-04-211-9/+95
| | | | | | | | | | | | | * arith.c (seq_nueq, seq_lt, seq_le): New static functions. (gt, lt): Handle sequences via seq_lt. (ge, le): Handle sequences via seq_le. (numeq): Handle sequences via seq_eq. * txr.1: Documented, and also added missing documentation about comparison of ranges by these functions, fixed mistake in the syntax (> listed twice) and added some notes about symmetry of > >= and < <=.
* warning cleanup: remove unused parameters.Kaz Kylheku2020-04-051-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is the second round of an effort to enable GCC's -Wextra option. All function parameters that are unused and are removable are removed. They are eliminated from the function defintions, declarations, callers, and any related function pointer variables or structure members. * arith.c (nary_simple_op): Remove unused self parameter. See lib.c: maxv, minv. * chksum.c (crc32_buf, crc32_str): Remove unused self parameter. (crc32): Don't pass self to the above functions. * eval.c (copy_env_handler, copy_bh_env_handler): Remove unused parent parameter. See unwind.c. (supplement_op_syms): Remove unused max parameter. (me_op): Don't pass max to supplement_op_syms. * lib.c (seq_iter_rewind): Remove unused self parameter. (lazy_flatten_func): Remove unused env parameter. (lazy_flatten): Use func_n1 to create non-environment-carrying funtion out of lazy_flatten_func. (maxv, minv): Don't pass self parameter to nary_simple_op. (middle_pivot): Remove unused lessfun param. (quicksort): Don't pass lessfun to middle_pivot. (diff, isec): Don't pass self to seq_iter_rewind. * lib.h (seq_iter_rewind, nary_simple_op): Declarations updated. * struct.c (get_super_slots): Remove unused self parameteer. (make_struct_type): Don't pass self to get_super_slots. * sysif.c (flock_unpack): Remove unused self parameter. (fcntl_wrap): Don't pass self to flock_unpack. * unwind.c (uw_push_cont_copy): Remove parent parameter from function pointer parameter. See eval.c: copy_env_handler. (call_copy_handlers): Remove parent parameter and don't pass that argument to the indirect call via pointer to the copy handler function. (revive_cont, capture_cont): Don't pass 0 value to removed parent parameter of call_copy_handlers. * unwind.h (struct uw_cont_copy): Function pointer member copy loses parent parameter. (uw_push_cont_copy): Declaration updated.
* Copyright year bump 2020.Kaz Kylheku2019-12-311-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * LICENSE, LICENSE-CYG, METALICENSE, Makefile, alloca.h, args.c, args.h, arith.c, arith.h, buf.c, buf.h, cadr.c, cadr.h, chksum.c, chksum.h, chksums/crc32.c, chksums/crc32.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, linenoise/linenoise.c, linenoise/linenoise.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/debugger.tl, share/txr/stdlib/defset.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/param.tl, share/txr/stdlib/path-test.tl, share/txr/stdlib/place.tl, share/txr/stdlib/pmac.tl, share/txr/stdlib/save-exe.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, tree.c, tree.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 notices to 2020.
* Add hyperbolic functions: sinh, cosh, and others.Kaz Kylheku2019-12-071-0/+98
| | | | | | | | | | | | | | | | | | | | * arith.c (sinh_s, cosh_s, tanh_s, asinh_s, acosh_s, atanh_s): New symbol variables. (sinh, cosh, tanh, asinh, acosh, atanh): New static functions. (sineh, cosih, tangh, asineh, acosih, atangh): New functions. (arith_init): Register sinh, cosh, tanh, asinh, acosh and atanh intrinsic functions, and initialize the new symbol variables. * configure: Detect availability of hyperbolic functions in math library and defne HAVE_HYPERBOLICS as 1 in config.h accordingly. * lib.h (sineh, cosih, tangh, asineh, acosih, atangh): Declared. * txr.1: Documented new hyperbolic functions and their method counterparts that a numeric struct can implement.
* 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.