/* return the comparison */ return cmp(abs_a, abs_b); } define dms_cmp(a,b) { local abs_a, abs_b; /* scalars of the arguments */ /* compute scalars of the arguments */ abs_a = dms_abs(a); abs_b = dms_abs(b); /* return the equality comparison */ return (abs_a == abs_b); } define dms_inc(a) { local obj dms ans; /* return value */ /* increment a dms object */ if (istype(a, ans)) { ans = a; ++ans.sec; /* return normalized result */ ans = fixdms(ans); return ans; } /* increment a scalar */ return a+1; } define dms_dec(a) { local obj dms ans; /* return value */ /* decrement a dms object */ if (istype(a, ans)) { ans = a; --ans.sec; /* return normalized result */ ans = fixdms(ans); return ans; } /* decrement a scalar */ return a-1; } define fixdms(a) { local obj dms ans; /* temp value */ /* firewall */ if (! istype(a, ans)) { quit "attempt to fix a non dms object"; } /* force minutes to be intergral */ a.min += frac(a.deg) * 60; a.deg = int(a.deg); /* force degrees to be intergral */ a.sec += frac(a.min) * 60; a.min = int(a.min); /* carry excess seconds into minutes */ a.min += a.sec // 60; a.sec %= 60; /* carry excess minutes into degrees */ a.deg += a.min // 60; a.min %= 60; /* round degrees :-) */ a.deg %= 360; /* return normalized result */ return a; } if (config("resource_debug") & 3) { print "obj dms {deg, min, sec} defined"; } /* * solve - solve f(x) = 0 to within the desired error value for x * * Copyright (C) 1999 David I. Bell * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.2 $ * @(#) $Id: solve.cal,v 30.2 2008/05/10 13:30:00 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/cal/RCS/solve.cal,v $ * * Under source code control: 1990/02/15 01:50:37 * File existed as early as: before 1990 * * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ /* * Solve the equation f(x) = 0 to within the desired error value for x. * The function 'f' must be defined outside of this routine, and the low * and high values are guesses which must produce values with opposite signs. */ define solve(low, high, epsilon) { local flow, fhigh, fmid, mid, places; if (isnull(epsilon)) epsilon = epsilon(); if (epsilon <= 0) quit "Non-positive epsilon value"; places = highbit(1 + int(1/epsilon)) + 1; flow = f(low); if (abs(flow) < epsilon) return low; fhigh = f(high); if (abs(fhigh) < epsilon) return high; if (sgn(flow) == sgn(fhigh)) quit "Non-opposite signs"; while (1) { mid = bround(high - fhigh * (high - low) / (fhigh - flow), places); if ((mid == low) || (mid == high)) places++; fmid = f(mid); if (abs(fmid) < epsilon) return mid; if (sgn(fmid) == sgn(flow)) { low = mid; flow = fmid; } else { high = mid; fhigh = fmid; } } } /* * linear - perform a simple two point 2D linear interpolation * * Copyright (C) 2005-2007 Landon Curt Noll * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.2 $ * @(#) $Id: linear.cal,v 30.2 2007/03/17 05:57:42 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/cal/RCS/linear.cal,v $ * * Under source code control: 2005/12/12 06:41:50 * File existed as early as: 2005 * * chongo /\oo/\ http://www.isthe.com/chongo/ * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ /* * linear - perform a simple two point 2D linear interpolation * * given: * x0, y0 first known point on the line * x1, y1 second knonw point on the line * x a given point to interpolate on * * returns: * y such that (x,y) is on the line defined by (x0,y0) and (x1,y1) * * NOTE: The line cannot be vertical. So x0 != y0. */ define linear(x0, y0, x1, y1, x) { /* firewall */ if (!isnum(x0) || ! isnum(y0) || !isnum(x1) || ! isnum(y1) || !isnum(x)) { quit "non-numeric argument passed to linear"; } if (x0 == x1) { quit "linear given a line with an infinite slope"; } /* return y = y0 + (delta_Y/delta_X) * (x - x0) */ return y0 + (((y1-y0)/(x1-x0)) * (x - x0)); } /* * test3500 - 3500 series of the regress.cal test suite * * Copyright (C) 1999 Ernest Bowen and Landon Curt Noll * * Primary author: Ernest Bowen * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.1 $ * @(#) $Id: test3500.cal,v 30.1 2007/03/16 11:09:54 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/cal/RCS/test3500.cal,v $ * * Under source code control: 1995/12/18 22:50:46 * File existed as early as: 1995 * * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ /* * Stringent tests of the functions frem, fcnt, gcdrem. * * testf(n) gives n tests of frem(x,y) and fcnt(x,y) with randomly * integers x and y generated so that x = f * y^k where f, y and * k are randomly generated. * * testg(n) gives n tests of gcdrem(x,y) with x and y generated as for * testf(n). * * testh(n,N) gives n tests of g = gcdrem(x,y) where x and y are products of * powers of small primes some of which are common to both x and y. * This test uses f = abs(x) and iteratively f = frem(f,p) where * p varies over the prime divisors of y; the final value for f * should equal g. For both x and y the primes are raised to the * power rand(N); N defaults to 10. * * If verbose is > 1, the numbers x, y and values for some of the * functions will be displayed. Numbers used in testf() * and testg() occasionally have thousands of digits. * */ defaultverbose = 1; /* default verbose value */ define testfrem(x,y,verbose) { local f, n; if (isnull(verbose)) verbose = defaultverbose; f = frem(x,y); n = fcnt(x,y); if (verbose > 1) printf("frem = %d, fcnt = %d\n\n", f, n); if (abs(x) != f * abs(y)^n) return 1; if (!ismult(x,y) || abs(y) <= 1) { if (f != abs(x)) return 2; if (n != 0) return 3; return 0; } if (x == 0) { if (f != 0 || n != 0) return 4; return 0; } if (f < 0 || !isint(f) || n <= 0) return 5; if (ismult(f, y)) return 6; if (!ismult(x, y^n)) return 7; if (ismult(x, y^(n+1))) return 8; return 0; } define testgcdrem(x,y,verbose) { local d, q; if (isnull(verbose)) verbose = defaultverbose; d = gcdrem(x,y); if (verbose > 1) printf("gcdrem = %d\n\n", d); if (y == 0) { if (d != 1) return 1; return 0; } if (x == 0) { if (d != 0) return 2; return 0; } if (d <= 0) return 3; q = x/d; if (!isint(q)) return 4; if (!isrel(d, y)) return 5; if (!isrel(d, q)) return 6; return 0; } define testf(str,n,verbose) { local m, x, y, i, k, y1, f1, f, fail; if (isnull(verbose)) verbose = defaultverbose; if (verbose > 0) { print str:":",:; } m = 0; for (i = 0; i < n; i++) { y1 = rand(2^rand(1,6)); y = rand(-(2^y1), 1 + 2^y1); f1 = rand(2^rand(1,11)); f = rand(-(2^f1), 1+2^f1); k = rand(1,1+2^10); x = f * y^k; if (verbose > 1) { printf("x = %d\n", x); printf("y = %d\n", y); } fail = testfrem(x,y,verbose); if (fail != 0) { printf("*** Failure %d on loop %d\n", fail, i); if (verbose > 1) { printf(" x = %d\n", x); printf(" y = %d\n", y); } m++; } } if (verbose > 0) { if (m) { printf("*** %d error(s)\n", m); } else { printf("no errors\n"); } } return m; } define testg(str,n,verbose) { local m, x, y, i, k, y1, f1, f, fail; if (isnull(verbose)) verbose = defaultverbose; if (verbose > 0) { print str:":",:; } m = 0; for (i = 0; i < n; i++) { y1 = rand(2^rand(1,6)); y = rand(-(2^y1), 1 + 2^y1); f1 = rand(2^rand(1,11)); f = rand(-(2^f1), 1+2^f1); k = rand(1,1+2^10); x = f * y^k; if (verbose > 1) { printf("x = %d\n", x); printf("y = %d\n", y); } fail = testgcdrem(x,y,verbose); if (fail != 0) { printf("*** Failure %d on loop %d\n", fail, i); if (verbose > 1) { printf(" x = %d\n", x); printf(" y = %d\n", y); } m++; } } if (verbose > 0) { if (m) { printf("*** %d error(s)\n", m); } else { printf("no errors\n"); } } return m; } define testh(str,n,N,verbose) { local m, i, x, y, f, g; if (isnull(verbose)) verbose = defaultverbose; if (verbose > 0) { print str:":",:; } m = 0; if (isnull(N)) N = 61; for (i = 0; i < n; i ++) { x = 2^rand(N)*3^rand(N) * 7^rand(N) * 11^rand(N) * 101^rand(N); y = 2^rand(N) * 3^rand(N) * 5^rand(N) * 11^rand(N) * 53^rand(N); if (rand(2)) x = -x; if (rand(2)) y = -y; if (verbose > 1) { printf("x = %d\n", x); printf("y = %d\n", y); } f = abs(x); g = gcdrem(x,y); if (ismult(y,2)) f = frem(f,2); if (ismult(y,3)) f = frem(f,3); if (ismult(y,5)) f = frem(f,5); if (ismult(y,11)) f = frem(f,11); if (ismult(y,53)) f = frem(f,53); if (f != g) { printf("*** Failure on loop %d\n", i); if (verbose > 1) { printf(" x = %d\n", x); printf(" y = %d\n", y); printf(" f = %d\n", f); printf(" g = %d\n", g); } m++; } } if (verbose > 0) { if (m) { printf("*** %d error(s)\n", m); } else { printf("no errors\n"); } } return m; } /* * test3500 - perform all of the above tests a bunch of times */ define test3500(verbose, tnum, n, N) { /* set test parameters */ if (isnull(verbose)) { verbose = defaultverbose; } if (isnull(tnum)) { tnum = 3501; /* default test number */ } if (isnull(n)) { n = 200; } if (isnull(N)) { N = 61; } /* * test a lot of stuff */ srand(3500e3500); err += testf(strcat(str(tnum++), ": frem/fcnt"), n, verbose); err += testg(strcat(str(tnum++), ": gcdrem"), n, verbose); err += testh(strcat(str(tnum++),": gcdrem #2"), n, N, verbose); if (verbose > 1) { if (err) { print "***", err, "error(s) found in testall"; } else { print "no errors in testall"; } } return tnum; } /* * varargs - example of a varargs-like use * * Copyright (C) 1999 David I. Bell * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.1 $ * @(#) $Id: varargs.cal,v 30.1 2007/03/16 11:09:54 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/cal/RCS/varargs.cal,v $ * * Under source code control: 1991/05/22 21:56:34 * File existed as early as: 1991 * * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ /* * Example program to use 'varargs'. * * Program to sum the cubes of all the specified numbers. */ define sc() { local s, i; s = 0; for (i = 1; i <= param(0); i++) { if (!isnum(param(i))) { print "parameter",i,"is not a number"; continue; } s += param(i)^3; } return s; } if (config("resource_debug") & 3) { print "sc(a, b, ...) defined"; } /* * psqrt - calculate square roots modulo a prime * * Copyright (C) 1999 David I. Bell * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.1 $ * @(#) $Id: psqrt.cal,v 30.1 2007/03/16 11:09:54 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/cal/RCS/psqrt.cal,v $ * * Under source code control: 1990/02/15 01:50:35 * File existed as early as: before 1990 * * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ /* * Returns null if number is not prime or if there is no square root. * The smaller square root is always returned. */ define psqrt(u, p) { local p1, q, n, y, r, v, w, t, k; p1 = p - 1; r = lowbit(p1); q = p >> r; t = 1 << (r - 1); for (n = 2; ; n++) { if (ptest(n, 1) == 0) continue; y = pmod(n, q, p); k = pmod(y, t, p); if (k == 1) continue; if (k != p1) return; break; } t = pmod(u, (q - 1) / 2, p); v = (t * u) % p; w = (t^2 * u) % p; while (w != 1) { k = 0; t = w; do { k++; t = t^2 % p; } while (t != 1); if (k == r) return; t = pmod(y, 1 << (r - k - 1), p); y = t^2 % p; v = (v * t) % p; w = (w * y) % p; r = k; } return min(v, p - v); } i .Z ..j pzasusb8.calkargv.call register.calm pmodm127.caln halflen.cal/* * pzasusb8 - print numereator as a string of USB8s * * Copyright (C) 1999,2004 Ernest Bowen and Landon Curt Noll * * Primary author: Ernest Bowen * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.1 $ * @(#) $Id: pzasusb8.cal,v 30.1 2007/03/16 11:10:04 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/custom/RCS/pzasusb8.cal,v $ * * Under source code control: 1999/10/06 03:11:12 * File existed as early as: 1998 * * chongo /\oo/\ http://www.isthe.com/chongo/ * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ /* * firewall */ if (config("compile_custom") == 0) { quit "calc compiled without -DCUSTOM"; } else if (config("allow_custom") == 0) { quit "calc was run without the -D command line option"; } print "p(n) prints array in which numerator of n is stored as a"; print "sequence of 2-hex-digits representing unsigned characters."; print "h(n) printx n in hex notation. This should be the same as"; print "p(n) except for (1) its leading 0x, (2) possible trailing zeros"; print "in p(n), and (3) the order of the hex-digit pairs."; print "The following example show results for n = isqrt(2e100)."; print ""; define p(n) {custom("pzasusb8", n); print;} define h(n) = printf("%x\n", n); n = isqrt(2e100); print ""; p(n); h(n); print ""; print "BASEB: ", custom("sysinfo", "BASEB"); print "CALC_BYTE_ORDER: ", custom("sysinfo", "CALC_BYTE_ORDER"); print "BIG_ENDIAN: ", custom("sysinfo", "BIG_ENDIAN"); print "LITTLE_ENDIAN: ", custom("sysinfo", "LITTLE_ENDIAN"); print "LONG_BITS: ", custom("sysinfo", "LONG_BITS"); print "Calc sizes:"; show sizes; /* * argv - print information about various args * * Copyright (C) 1999 Landon Curt Noll * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.1 $ * @(#) $Id: argv.cal,v 30.1 2007/03/16 11:10:04 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/custom/RCS/argv.cal,v $ * * Under source code control: 1997/03/10 00:27:17 * File existed as early as: 1997 * * chongo /\oo/\ http://www.isthe.com/chongo/ * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ /* * This file is part of the custom sample calc files. * * NOTE: You must use a calc that was compiled with ALLOW_CUSTOM= -DCUSTOM * and run with a -C arg. */ if (config("compile_custom") == 0) { quit "calc compiled without -DCUSTOM"; } else if (config("allow_custom") == 0) { quit "calc was run without the -D command line option"; } define argv_test() { local i; /* arg number */ local junk; /* throw away value */ /* * process each arg passed to us */ for (i = 1; i <= param(0); ++i) { /* * This won't really work because all the arg numbers * will be reported as arg[0] ... but what the heck * this is only a demo! */ junk = custom("argv", param(i)); } return i-1; } /* * register - set or print a custom register value * * Copyright (C) 2007 Landon Curt Noll * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.1 $ * @(#) $Id: register.cal,v 30.1 2007/07/05 19:35:54 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/custom/RCS/register.cal,v $ * * Under source code control: 2007/07/05 11:11:11 * File existed as early as: 2007 * * chongo /\oo/\ http://www.isthe.com/chongo/ * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ /* * This file is part of the custom sample calc files. * * NOTE: You must use a calc that was compiled with ALLOW_CUSTOM= -DCUSTOM * and run with a -C arg. */ if (config("compile_custom") == 0) { quit "calc compiled without -DCUSTOM"; } else if (config("allow_custom") == 0) { quit "calc was run without the -D command line option"; } define register(num) { local reg_max; /* number of registers */ /* * firewall */ if (!isnum(num)) { return newerror("register only works on numeric values"); } /* * determine register count */ reg_max = custom("sysinfo", "REGNUM_MAX"); /* * determine the HALF length of a numeric value */ if (num < 0) { return newerror("register number must be >= 0"); } else if (num > reg_max) { return newerror("register number must be < REGNUM_MAX"); } else { return custom("register", num); } } /* * pmodm127 - test pmodm127's ability to calculate q mod 2^(2^127-1) * * Copyright (C) 2004 Landon Curt Noll * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.1 $ * @(#) $Id: pmodm127.cal,v 30.1 2007/03/16 11:10:04 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/custom/RCS/pmodm127.cal,v $ * * Under source code control: 2004/02/25 14:25:32 * File existed as early as: 2004 * * chongo /\oo/\ http://www.isthe.com/chongo/ * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ /* * This file is part of the custom sample calc files. * * NOTE: You must use a calc that was compiled with ALLOW_CUSTOM= -DCUSTOM * and run with a -C arg. */ if (config("compile_custom") == 0) { quit "calc compiled without -DCUSTOM"; } else if (config("allow_custom") == 0) { quit "calc was run without the -D command line option"; } define pmodm127_test1(testcnt) { local q; /* test factor */ local m127; /* 2^127-1 */ local i; /* * firewall */ if (!isint(testcnt) || testcnt <= 0) { return newerror("pmodm127_test1 must have an integer count>0"); } /* * perform testcnt divisor tests for primes of form 2*k*(2^127-1)+1 * * NOTE: Since this is just a test, we do not need to be optimal. */ m127 = 2^127 - 1; q = 2*4949132165849*m127+1; for (i=0; i < testcnt; ++i) { /* * determine next prime divisor */ q = nextcand(q, -1, 0, 1, 2*m127); /* compare custom function with its pmod() equivalent */ if (config("resource_debug") & 8) { print "testing", q; } if (pmod(2, m127, q) != custom("pmodm127", q)) { print "ERROR: pmodm127 failed for ", str(q); return newerror("pmodm127 failed for " + str(q)); } } /* return success count */ if (config("resource_debug") & 8) { print "passed", testcnt, "tests"; } return testcnt; } define pmodm127_test2(testcnt, seed) { local q; /* test factor */ local m127; /* 2^127-1 */ local i; /* * firewall */ if (!isint(testcnt) || testcnt <= 0) { return newerror("pmodm127_test2 must have an integer count>0"); } if (!isint(seed)) { return newerror("pmodm127_test2 must an integer seed"); } /* * perform testcnt divisor tests random integers over [1e51, 1e52) * * NOTE: Since this is just a test, we do not need to be optimal. */ m127 = 2^127 - 1; for (i=0; i < testcnt; ++i) { /* * determine next prime divisor */ q = rand(1e51, 1e52) | 0x1; if (config("resource_debug") & 8) { print "testing", q; } /* compare custom function with its pmod() equivalent */ if (pmod(2, m127, q) != custom("pmodm127", q)) { print "ERROR: pmodm127 failed for ", str(q); print "ERROR: ", pmod(2,m127,q), " != ", custom("pmodm127", q); return newerror("pmodm127 failed for " + str(q)); } } /* return success count */ if (config("resource_debug") & 8) { print "passed", testcnt, "tests"; } return testcnt; } if ((config("resource_debug") & 3) && !(config("resource_debug") & 8)) { print "DEBUG: use config('resource_debug',", config("resource_debug")|8:") to enable more debugging"; } /* * halflen - determine the length of numeric value in HALFs * * Copyright (C) 1999,2004 Landon Curt Noll * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.1 $ * @(#) $Id: halflen.cal,v 30.1 2007/03/16 11:10:04 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/custom/RCS/halflen.cal,v $ * * Under source code control: 1997/03/08 20:51:32 * File existed as early as: 1997 * * chongo /\oo/\ http://www.isthe.com/chongo/ * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ /* * This file is part of the custom sample calc files. * * NOTE: You must use a calc that was compiled with ALLOW_CUSTOM= -DCUSTOM * and run with a -C arg. */ if (config("compile_custom") == 0) { quit "calc compiled without -DCUSTOM"; } else if (config("allow_custom") == 0) { quit "calc was run without the -D command line option"; } define halflen(num) { local baseb; /* bit len of a HALF */ /* * firewall */ if (!isnum(num)) { return newerror("halflen only works on numeric values"); } /* * determine baseb */ baseb = custom("sysinfo","BASEB"); /* * determine the HALF length of a numeric value */ if (num == 0) { /* consider 0 to be 1 HALF long */ return 1; } else if (isint(num)) { return (highbit(num)+baseb-1)/baseb; } else if (isreal(num)) { return halflen(num(num)) + halflen(den(num)); } else if (isnum(num)) { return halflen(re(num)) + halflen(im(num)); } else { return newerror("halflen only works on numeric values"); } } /* * mod - routines to handle numbers modulo a specified number * * Copyright (C) 1999 David I. Bell * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.1 $ * @(#) $Id: mod.cal,v 30.1 2007/03/16 11:09:54 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/cal/RCS/mod.cal,v $ * * Under source code control: 1990/02/15 01:50:34 * File existed as early as: before 1990 * * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ obj mod {a}; /* definition of the object */ global mod_value = 100; /* modulus value (value of N) */ define lmod(a) { local obj mod x; if (!isreal(a) || !isint(a)) quit "Bad argument for lmod function"; x.a = a % mod_value; return x; } define mod_print(a) { if (digits(mod_value) <= 20) print a.a, "(mod", mod_value : ")" :; else print a.a, "(mod N)" :; } define mod_one() { return lmod(1); } define mod_cmp(a, b) { if (isnum(a)) return (a % mod_value) != b.a; if (isnum(b)) return (b % mod_value) != a.a; return a.a != b.a; } define mod_rel(a, b) { if (isnum(a)) a = lmod(a); if (isnum(b)) b = lmod(b); if (a.a < b.a) return -1; return a.a != b.a; } define mod_add(a, b) { local obj mod x; if (isnum(b)) { if (!isint(b)) quit "Adding non-integer"; x.a = (a.a + b) % mod_value; return x; } if (isnum(a)) { if (!isint(a)) quit "Adding non-integer"; x.a = (a + b.a) % mod_value; return x; } x.a = (a.a + b.a) % mod_value; return x; } define mod_sub(a, b) { return a + (-b); } define mod_neg(a) { local obj mod x; x.a = mod_value - a.a; return x; } define mod_mul(a, b) { local obj mod x; if (isnum(b)) { if (!isint(b)) quit "Multiplying by non-integer"; x.a = (a.a * b) % mod_value; return x; } if (isnum(a)) { if (!isint(a)) quit "Multiplying by non-integer"; x.a = (a * b.a) % mod_value; return x; } x.a = (a.a * b.a) % mod_value; return x; } define mod_square(a) { local obj mod x; x.a = a.a^2 % mod_value; return x; } define mod_inc(a) { local x; x = a; if (++x.a == mod_value) x.a = 0; return x; } define mod_dec(a) { local x; x = a; if (--x.a < 0) x.a = mod_value - 1; return x; } define mod_inv(a) { local obj mod x; x.a = minv(a.a, mod_value); return x; } define mod_div(a, b) { local c; local obj mod x; local obj mod y; if (isnum(a)) a = lmod(a); if (isnum(b)) b = lmod(b); c = gcd(a.a, b.a); x.a = a.a / c; y.a = b.a / c; return x * inverse(y); } define mod_pow(a, b) { local x, y, z; obj mod x; y = a; z = b; if (b < 0) { y = inverse(a); z = -b; } x.a = pmod(y.a, z, mod_value); return x; } if (config("resource_debug") & 3) { print "obj mod {a} defined"; print "mod_value defined"; print "set mod_value as needed"; } /* * mfactor - return the lowest factor of 2^n-1, for n > 0 * * Copyright (C) 1999 Landon Curt Noll * * Calc is open software; you can redistribute it and/or modify it under * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. * * Calc is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * A copy of version 2.1 of the GNU Lesser General Public License is * distributed with calc under the filename COPYING-LGPL. You should have * received a copy with calc; if not, write to Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * @(#) $Revision: 30.1 $ * @(#) $Id: mfactor.cal,v 30.1 2007/03/16 11:09:54 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/cal/RCS/mfactor.cal,v $ * * Under source code control: 1996/07/06 06:09:40 * File existed as early as: 1996 * * chongo /\oo/\ http://www.isthe.com/chongo/ * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ */ /* * hset method * * We will assume that mfactor is called with p_elim == 17. * * n = (the Mersenne exponent we are testing) * Q = 4*2*3*5*7*11*13*17 (4 * pfact(of some reasonable integer)) * * We first determine all values of h mod Q such that: * * gcd(h*n+1, Q) == 1 and h*n+1 == +/-1 mod 8 * * There will be 2*1*2*4*6*10*12*16 such values of h. * * For efficiency, we keep the difference between consecutive h values * in the hset[] difference array with hset[0] being the first h value. * Last, we multiply the hset[] values by n so that we only need * to add sequential values of hset[] to get factor candidates. * * We need only test factors of the form: * * (Q*g*n + hx) + 1 * * where: * * g is an integer >= 0 * hx is computed from hset[] difference value described above * * Note that (Q*g*n + hx) is always even and that hx is a multiple * of n. Thus the typical factor form: * * 2*k*n + 1 * * implies that: * * k = (Q*g + hx/n)/2 * * This allows us to quickly eliminate factor values that are divisible * by 2, 3, 5, 7, 11, 13 or 17. (well <= p value found below) * * The following loop shows how test_factor is advanced to higher test * values using hset[]. Here, hcount is the number of elements in hset[]. * It can be shown that hset[0] == 0. We add hset[hcount] to the hset[] * array for looping control convenience. * * (* increase test_factor thru other possible test values *) * test_factor = 0; * hindx = 0; * do { * while (++hindx <= hcount) { * test_factor += hset[hindx]; * } * hindx = 0; * } while (test_factor < some_limit); * * The test, mfactor(67, 1, 10000) took on an 200 Mhz r4k (user CPU seconds): * * 210.83 (prior to use of hset[]) * 78.35 (hset[] for p_elim = 7) * 73.87 (hset[] for p_elim = 11) * 73.92 (hset[] for p_elim = 13) * 234.16 (hset[] for p_elim = 17) * p_elim == 19 requires over 190 Megs of memory * * Over a long period of time, the call to load_hset() becomes insignificant. * If we look at the user CPU seconds from the first 10000 cycle to the * end of the test we find: * * 205.00 (prior to use of hset[]) * 75.89 (hset[] for p_elim = 7) * 73.74 (hset[] for p_elim = 11) * 70.61 (hset[] for p_elim = 13) * 57.78 (hset[] for p_elim = 17) * p_elim == 19 rejected because of memory size * * The p_elim == 17 overhead takes ~3 minutes on an 200 Mhz r4k CPU and * requires about ~13 Megs of memory. The p_elim == 13 overhead * takes about 3 seconds and requires ~1.5 Megs of memory. * * The value p_elim == 17 is best for long factorizations. It is the * fastest even thought the initial startup overhead is larger than * for p_elim == 13. * * NOTE: The values above are prior to optimizations where hset[] was * multiplied by n plus other optimizations. Thus, the CPU * times you may get will not likely match the above values. */ /* * mfactor - find a factor of a Mersenne Number * * Mersenne numbers are numbers of the form: * * 2^n-1 for integer n > 0 * * We know that factors of a Mersenne number are of the form: * * 2*k*n+1 and +/- 1 mod 8 * * We make use of the hset[] difference array to eliminate factor * candidates that would otherwise be divisible by 2, 3, 5, 7 ... p_elim. * * given: * n attempt to factor M(n) = 2^n-1 * start_k the value k in 2*k*n+1 to start the search (def: 1) * rept_loop loop cycle reporting (def: 10000) * p_elim largest prime to eliminate from test factors (def: 17) * * returns: * factor of (2^n)-1 * * NOTE: The p_elim argument is optional and defaults to 17. A p_elim value * of 17 is faster than 13 for even medium length runs. However 13 * uses less memory and has a shorter startup time. */ define mfactor(n, start_k, rept_loop, p_elim) { local Q; /* 4*pfact(p_elim), hset[] cycle size */ local hcount; /* elements in the hset[] difference array */ local loop; /* report loop count */ local q; /* test factor of 2^n-1 */ local g; /* g as in test candidate form: (Q*g*hset[i])*n + 1 */ local hindx; /* hset[] index */ local i; local tmp; local tmp2; /* * firewall */ if (!isint(n) || n <= 0) { quit "n must be an integer > 0"; } if (!isint(start_k)) { start_k = 1; } else if (!isint(start_k) || start_k <= 0) { quit "start_k must be an integer > 0"; } if (!isint(rept_loop)) { rept_loop = 10000; } if (rept_loop < 1) { quit "rept_loop must be an integer > 0"; } if (!isint(p_elim)) { p_elim = 17; } if (p_elim < 3) { quit "p_elim must be an integer > 2 (try 13 or 17)"; } /* * declare our global values */ Q = 4*pfact(p_elim); hcount = 2; /* allocate the h difference array */ for (i=2; i <= p_elim; i = nextcand(i)) { hcount *= (i-1); } local mat hset[hcount+1]; /* * load the hset[] difference array */ { local x; /* h*n+1 mod 8 */ local h; /* potential h value */ local last_h; /* previous valid h value */ last_h = 0; for (i=0,h=0; h < Q; ++h) { if (gcd(h*n+1,Q) == 1) { x = (h*n+1) % 8; if (x == 1 || x == 7) { hset[i++] = (h-last_h) * n; last_h = h; } } } hset[hcount] = Q*n - last_h*n; } /* * setup * * determine the next g and hset[] index (hindx) values such that: * * 2*start_k <= (Q*g + hset[hindx]) * * and (Q*g + hset[hindx]) is a minimum and where: * * Q = (4 * pfact(of some reasonable integer)) * g = (some integer) (hset[] cycle number) * * We also compute 'q', the next test candidate. */ g = (2*start_k) // Q; tmp = 2*start_k - Q*g; for (tmp2=0, hindx=0; hindx < hcount && (tmp2 += hset[hindx]/n) < tmp; ++hindx) { } if (hindx == hcount) { /* we are beyond the end of a hset[] cycle, start at the next */ ++g; hindx = 0; tmp2 = hset[0]/n; } q = (Q*g + tmp2)*n + 1; /* * look for a factor * * We ignore factors that themselves are divisible by a prime <= * some small prime p. * * This process is guaranteed to find the smallest factor * of 2^n-1. A smallest factor of 2^n-1 must be prime, otherwise * the divisors of that factor would also be factors of 2^n-1. * Thus we know that if a test factor itself is not prime, it * cannot be the smallest factor of 2^n-1. * * Eliminating all non-prime test factors would take too long. * However we can eliminate 80.81% of the test factors * by not using test factors that are divisible by a prime <= 17. */ if (pmod(2,n,q) == 1) { return q; } else { /* report this loop */ printf("at 2*%d*%d+1, cpu: %f\n", (q-1)/(2*n), n, usertime()); fflush(files(1)); loop = 0; } do { /* * determine if we need to report * * NOTE: (q-1)/(2*n) is the k value from 2*k*n + 1. */ if (rept_loop <= ++loop) { /* report this loop */ printf("at 2*%d*%d+1, cpu: %f\n", (q-1)/(2*n), n, usertime()); fflush(files(1)); loop = 0; } /* * skip if divisable by a prime <= 449 * * The value 281 was determined by timing loops * which found that 281 was at or near the * minimum time to factor 2^(2^127-1)-1. * * The addition of the do { ... } while (factor(q, 449)>1); * loop reduced the factoring loop time (36504 k values with * the hset[] initialization time removed) from 25.69 sec to * 15.62 sec of CPU time on a 200Mhz r4k. */ do { /* * determine the next factor candidate */ q += hset[++hindx]; if (hindx >= hcount) { hindx = 0; /* * if we cared about g, * then we wound ++g here too */ } } while (factor(q, 449) > 1); } while (pmod(2,n,q) != 1); /* * return the factor found * * q is a factor of (2^n)-1 */ return q; } if (config("resource_debug") & 3) { print "mfactor(n [, start_k=1 [, rept_loop=10000 [, p_elim=17]]])" } q .Z ..r argvs helptsysinfoudevnullvregisterwpzasusb8xpmodm127NAME argv - displays information about its args SYNOPSIS custom("argv" [, arg ...]) TYPES arg any return int DESCRIPTION This custom function will, for each arg given print: arg number arg type number of elements (size()) memory size (sizeof()) The number of args passed, not counting the initial "argv" name arg is returned. EXAMPLE > foo=5^713; bar=17; baz=list(2,3,4); > custom("argv", foo, bar, baz, 3+4.5i, pi()) arg[0] rational_value size=1 sizeof=272 arg[1] rational_value size=1 sizeof=68 arg[2] list size=3 sizeof=256 arg[3] complex_value size=1 sizeof=140 arg[4] rational_value size=1 sizeof=84 5 LIMITS calc must be built with ALLOW_CUSTOM= -DCUSTOM calc must be executed with a -C arg. LIBRARY none SEE ALSO custom ## Copyright (C) 1999 Landon Curt Noll ## ## Calc is open software; you can redistribute it and/or modify it under ## the terms of the version 2.1 of the GNU Lesser General Public License ## as published by the Free Software Foundation. ## ## Calc is distributed in the hope that it will be useful, but WITHOUT ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General ## Public License for more details. ## ## A copy of version 2.1 of the GNU Lesser General Public License is ## distributed with calc under the filename COPYING-LGPL. You should have ## received a copy with calc; if not, write to Free Software Foundation, Inc. ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ## ## @(#) $Revision: 30.1 $ ## @(#) $Id: argv,v 30.1 2007/03/16 11:10:04 chongo Exp $ ## @(#) $Source: /usr/local/src/cmd/calc/custom/RCS/argv,v $ ## ## Under source code control: 1997/03/09 20:28:01 ## File existed as early as: 1997 ## ## chongo /\oo/\ http://www.isthe.com/chongo/ ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ NAME help - help for custom functions SYNOPSIS custom("help", name) TYPES name string return null DESCRIPTION This custom function will display the help for the builtin function named by the name argument. EXAMPLE > custom("help", "custom_cal") ... output the same as is produced by help custhelp/custom_cal ... LIMITS calc must be built with ALLOW_CUSTOM= -DCUSTOM calc must be executed with a -C arg. LIBRARY none SEE ALSO custom ## Copyright (C) 1999 Landon Curt Noll ## ## Calc is open software; you can redistribute it and/or modify it under ## the terms of the version 2.1 of the GNU Lesser General Public License ## as published by the Free Software Foundation. ## ## Calc is distributed in the hope that it will be useful, but WITHOUT ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General ## Public License for more details. ## ## A copy of version 2.1 of the GNU Lesser General Public License is ## distributed with calc under the filename COPYING-LGPL. You should have ## received a copy with calc; if not, write to Free Software Foundation, Inc. ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ## ## @(#) $Revision: 30.1 $ ## @(#) $Id: help,v 30.1 2007/03/16 11:10:04 chongo Exp $ ## @(#) $Source: /usr/local/src/cmd/calc/custom/RCS/help,v $ ## ## Under source code control: 1997/03/09 06:03:58 ## File existed as early as: 1997 ## ## chongo /\oo/\ http://www.isthe.com/chongo/ ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ NAME sysinfo - return a calc #define value SYNOPSIS custom("sysinfo" [, infoname]); TYPES infoname string or int return int, string or null DESCRIPTION This custom function will return the value certain selected #defile values. The infoname arg must be a string that matches the given #define name. For conveience, the case infoname does not matter, so "baseb" and "BASEB" refer to the same #define value. The return value is either an integer or a string depending on the type of #define selected. If infoname is unknown, NULL is returned. If no infoname is given then a list of infonames and meanings are printed. In this case, null is returned. If infoname is a number, then it is interpreted as follows: 0 print all infonames and meanings (same as no infoname) 1 print all infonames and values 2 print all infoname meanings and values EXAMPLE > custom("sysinfo", "baseb") 32 > custom("sysinfo") ... a list of infonames and meanings are printed ... > custom("sysinfo", 0) ... a list of infonames and meanings are printed ... > custom("sysinfo", 1) ... a list of infonames and values are printed ... > custom("sysinfo", 2) ... a list of infoname meanings and values are printed ... LIMITS calc must be built with ALLOW_CUSTOM= -DCUSTOM calc must be executed with a -C arg. LIBRARY none SEE ALSO custom ## Copyright (C) 1999 Landon Curt Noll ## ## Calc is open software; you can redistribute it and/or modify it under ## the terms of the version 2.1 of the GNU Lesser General Public License ## as published by the Free Software Foundation. ## ## Calc is distributed in the hope that it will be useful, but WITHOUT ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General ## Public License for more details. ## ## A copy of version 2.1 of the GNU Lesser General Public License is ## distributed with calc under the filename COPYING-LGPL. You should have ## received a copy with calc; if not, write to Free Software Foundation, Inc. ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ## ## @(#) $Revision: 30.1 $ ## @(#) $Id: sysinfo,v 30.1 2007/03/16 11:10:04 chongo Exp $ ## @(#) $Source: /usr/local/src/cmd/calc/custom/RCS/sysinfo,v $ ## ## Under source code control: 1997/03/09 23:14:40 ## File existed as early as: 1997 ## ## chongo /\oo/\ http://www.isthe.com/chongo/ ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ NAME devnull - does nothing SYNOPSIS custom("devnull" [, arg ...]) TYPES arg any return null DESCRIPTION This custom function does nothing. It is intented for testing of the general custom interface. EXAMPLE > custom("devnull", foo, bar, baz, 3+4.5i, pi()) LIMITS calc must be built with ALLOW_CUSTOM= -DCUSTOM calc must be executed with a -C arg. LIBRARY none SEE ALSO custom ## Copyright (C) 1999 Landon Curt Noll ## ## Calc is open software; you can redistribute it and/or modify it under ## the terms of the version 2.1 of the GNU Lesser General Public License ## as published by the Free Software Foundation. ## ## Calc is distributed in the hope that it will be useful, but WITHOUT ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General ## Public License for more details. ## ## A copy of version 2.1 of the GNU Lesser General Public License is ## distributed with calc under the filename COPYING-LGPL. You should have ## received a copy with calc; if not, write to Free Software Foundation, Inc. ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ## ## @(#) $Revision: 30.1 $ ## @(#) $Id: devnull,v 30.1 2007/03/16 11:10:04 chongo Exp $ ## @(#) $Source: /usr/local/src/cmd/calc/custom/RCS/devnull,v $ ## ## Under source code control: 1997/03/09 17:49:12 ## File existed as early as: 1997 ## ## chongo /\oo/\ http://www.isthe.com/chongo/ ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ NAME register - set or print a custom register value SYNOPSIS custom("register", regnum[, value]) TYPES regnum int value any, &any return any DESCRIPTION This custom function reads or sets a value of a custom register. A custom register can hold any calc value. Without a value arg, the register value is return. With a value arg, the register is set with that value and the previous value is returned. The number of custom registers is defined at compile time. There will be at least 32 registers although there could be more added in the future. It may be deterimed by: custom("sysinfo", "REGNUM_MAX") The custom registers are initialized to 0 by the internal function libcalc_call_me_first() during calc startup. This custom interface is intented to make it easier to interface with special purpose hardware. EXAMPLE > custom("register", 3) 0 > custom("register", 3, 45) 0 > custom("register", 3) 45 > custom("register", 7, 2i), > custom("register", 8, 3i), > custom("register", 7) * custom("register", 8) -6 LIMITS calc must be built with ALLOW_CUSTOM= -DCUSTOM calc must be executed with a -C arg. The regnum numbe bee an integer >= 0 and < REGNUM_MAX where REGNUM_MAX is a compile time constant that is >= 32. LIBRARY none SEE ALSO custom ## Copyright (C) 2007 Landon Curt Noll ## ## Calc is open software; you can redistribute it and/or modify it under ## the terms of the version 2.1 of the GNU Lesser General Public License ## as published by the Free Software Foundation. ## ## Calc is distributed in the hope that it will be useful, but WITHOUT ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General ## Public License for more details. ## ## A copy of version 2.1 of the GNU Lesser General Public License is ## distributed with calc under the filename COPYING-LGPL. You should have ## received a copy with calc; if not, write to Free Software Foundation, Inc. ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. ## ## @(#) $Revision: 29.2 $ ## @(#) $Id: register,v 29.2 2007/07/05 19:35:20 chongo Exp $ ## @(#) $Source: /usr/local/src/cmd/calc/custom/RCS/register,v $ ## ## Under source code control: 2005/02/04 22:39:50 ## File existed as early as: 2005 ## ## chongo /\oo/\ http://www.isthe.com/chongo/ ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ NAME pzasusb8 - print a number in hex octets SYNOPSIS custom("pzasusb8", arg) TYPES arg real return null DESCRIPTION This custom function prints out the numerator of a real value in octets. Each HALF value is printed in a separate line. NOTE: The output will vary depending on the size of a HALF and the byte order of the system. See: custom("sysinfo", "BASEB") custom("sysinfo", "CALC_BYTE_ORDER") for details. This custom function is intented for testing of the general custom interface. EXAMPLE > custom("pzasusb8", 0x01020304050607080910111213141516); 0: 13141516 1: 09101112 2: 05060708 3: 01020304 > custom("pzasusb8", 10^25) 0: 4a000000 1: 16140148 2: 00084595 > printf("%x\n", 10^25); 0x84595161401484a000000 LIMITS calc must be built with ALLOW_CUSTOM= -DCUSTOM calc must be executed with a -C arg. LIBRARY none SEE ALSO custom ## Copyright (C) 1999 Landon Curt Noll ## ## Calc is open software; you can redistribute it and/or modify it under ## the terms of the version 2.1 of the GNU Lesser General Public License ## as published by the Free Software Foundation. ## ## Calc is distributed in the hope that it will be useful, but WITHOUT ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General ## Public License for more details. ## ## A copy of version 2.1 of the GNU Lesser General Public License is ## distributed with calc under the filename COPYING-LGPL. You should have ## received a copy with calc; if not, write to Free Software Foundation, Inc. ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ## ## @(#) $Revision: 30.1 $ ## @(#) $Id: pzasusb8,v 30.1 2007/03/16 11:10:04 chongo Exp $ ## @(#) $Source: /usr/local/src/cmd/calc/custom/RCS/pzasusb8,v $ ## ## Under source code control: 1999/10/06 04:05:43 ## File existed as early as: 1999 ## ## chongo /\oo/\ http://www.isthe.com/chongo/ ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ NAME pmodm127 - calculate q mod 2^(2^127-1) SYNOPSIS custom("pmodm127", q) TYPES q int > 0 return int DESCRIPTION This custom function will return the value: q mod 2^(2^127-1) This custom function serves as a demonstration of how to write a custom function. It performs the equivalent of: pmod(2, 2^127-1, q) The return value is integer in the half open range: [0, q). SPECIAL NOTE: Can you find a value, q, for which this custom function returns 1? If you do, send the value of q to chongo using the EMail address found at: http://www.isthe.com/chongo/address.html EXAMPLE > custom("pmodm127", 65537) 32769 > custom("pmodm127", 2^31-1) 8 > custom("pmodm127", 7^51+2) 11228202966269457258557496419097462731193173 LIMITS calc must be built with ALLOW_CUSTOM= -DCUSTOM calc must be executed with a -C arg. q must be an integer > 0 LIBRARY none SEE ALSO custom ## Copyright (C) 2004 Landon Curt Noll ## ## Ca