head 1.1; branch 1.1.1; access ; symbols initial:1.1.1.1 keithp:1.1.1; locks ; strict; comment @# @; 1.1 date 2005.07.30.18.55.53; author keithp; state Exp; branches 1.1.1.1; next ; commitid 74aa42ebcd284567; 1.1.1.1 date 2005.07.30.18.55.53; author keithp; state Exp; branches ; next ; commitid 74aa42ebcd284567; desc @@ 1.1 log @Initial revision @ text @#!/usr/bin/env nickle /* * checkdata.5c - validate integer operations on wide integers * * Copyright © 2005, Keith Packard * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2.1 of the GNU Lesser Public License as * published by the Free Software Foundation. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU Lesser Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ int width = 2; int[width] readnums () { int[width] x; for (int i = 0; i < width; i++) if (File::scanf ("%x", &x[i]) != 1) exit (0); return x; } int mask(int bits) { return (1 << bits) - 1; } int unsigned (int n, int bits) { return n & mask (bits); } int signed(int n, int bits) { n &= mask (bits); if ((n & (1 << (bits - 1))) != 0) n |= -1 << bits; return n; } void op (string name, int (int, int, int) f, int[2] n) { void dump (int bits, bool s) { int ibits = name == "." ? bits // 2 : bits; int obits = bits; try { int a = (s ? signed : unsigned) (n[0], ibits); int b = (s ? signed : unsigned) (n[1], ibits); int v = (s ? signed : unsigned) (f (a, b, obits), obits); printf ("%s %3d %s %x\n", s ? "s" : "u", bits, name, v); } catch divide_by_zero (string msg, real a, real b) { return; } } dump (64, true); dump (64, false); dump (128, true); dump (128, false); } int div(int a, int b) { int d = abs (a) // abs (b); if ((a < 0) != (b < 0)) d = -d; return d; } int rem(int a, int b) { int r = abs (a) % abs (b); if (a < 0) r = -r; return r; } for (;;) { int[*] n = readnums(); for (int bits = 64; bits <= 128; bits <<= 1) for (int f = 0; f < 2; f++) printf ("%s %3d %x %x\n", f == 0 ? "s" : "u", bits, (f == 0 ? signed : unsigned) (n[0], bits), (f == 0 ? signed : unsigned) (n[1], bits)); printf ("\n"); op ("+", int func (int a, int b, int bits) { return a + b; }, n); op ("-", int func (int a, int b, int bits) { return a - b; }, n); op ("*", int func (int a, int b, int bits) { return a * b; }, n); op (".", int func (int a, int b, int bits) { return a * b; }, n); op (">>", int func (int a, int b, int bits) { return unsigned (a, bits) >> (b % bits); }, n); op (">>>", int func (int a, int b, int bits) { return signed (a, bits) >> (b % bits); }, n); op ("<<", int func (int a, int b, int bits) { return a << (b & (bits-1)); }, n); op ("/", int func (int a, int b, int bits) { return div(a, b); }, n); op ("%", int func (int a, int b, int bits) { return rem(a, b); }, n); op ("<", int func (int a, int b, int bits) { return a < b ? 1 : 0; }, n); op ("<=", int func (int a, int b, int bits) { return a <= b ? 1 : 0; }, n); op ("==", int func (int a, int b, int bits) { return a == b ? 1 : 0; }, n); op ("!=", int func (int a, int b, int bits) { return a != b ? 1 : 0; }, n); op (">=", int func (int a, int b, int bits) { return a >= b ? 1 : 0; }, n); op (">", int func (int a, int b, int bits) { return a > b ? 1 : 0; }, n); printf ("\n"); } @ 1.1.1.1 log @Import test framework for validating wide integer implementation @ text @@