Home | History | Annotate | Line # | Download | only in divrem
      1  1.4  itojun /*	$NetBSD: divremtest.c,v 1.4 2002/02/21 07:38:15 itojun Exp $	*/
      2  1.4  itojun 
      3  1.3    ross /*-
      4  1.3    ross  * Copyright (c) 2002 Ross Harvey
      5  1.3    ross  * All rights reserved.
      6  1.3    ross  *
      7  1.3    ross  * Redistribution and use in source and binary forms, with or without
      8  1.3    ross  * modification, are permitted provided that the following conditions
      9  1.3    ross  * are met:
     10  1.3    ross  * 1. Redistributions of source code must retain the above copyright
     11  1.3    ross  *    notice, this list of conditions and the following disclaimer.
     12  1.3    ross  *    Redistributions of source code must not add the GNU General Public
     13  1.3    ross  *    License or any similar license to this work or to derivative works
     14  1.3    ross  *    incorporating this code. No requirement to distribute source may
     15  1.3    ross  *    be imposed upon redistributions in binary form of this work or of
     16  1.3    ross  *    derivative works.
     17  1.3    ross  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.3    ross  *    notice, this list of conditions and the following disclaimer in the
     19  1.3    ross  *    documentation and/or other materials provided with the distribution.
     20  1.3    ross  *
     21  1.3    ross  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  1.3    ross  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  1.3    ross  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  1.3    ross  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  1.3    ross  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  1.3    ross  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  1.3    ross  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.3    ross  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  1.3    ross  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  1.3    ross  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  1.3    ross  * POSSIBILITY OF SUCH DAMAGE.
     32  1.3    ross  */
     33  1.3    ross 
     34  1.1    ross #include <stdio.h>
     35  1.1    ross #include <stdlib.h>
     36  1.1    ross #include <unistd.h>
     37  1.1    ross #include <fcntl.h>
     38  1.1    ross #include <stdint.h>
     39  1.2    ross #include <assert.h>
     40  1.2    ross #include <time.h>
     41  1.2    ross 
     42  1.2    ross #define	KLE	3	// exponent for k and l salt values
     43  1.2    ross #define	NEARBY	600	// all numbers +-NEARBY 0 and INTxx_MIN will be tried
     44  1.2    ross #define	RANDOMCOUNT 300000	// number of random(3)-based cases to run
     45  1.1    ross 
     46  1.1    ross #define	 IM(x)	 ((intmax_t)(x))
     47  1.1    ross #define	UIM(x)	((uintmax_t)(x))
     48  1.1    ross 
     49  1.1    ross #define TEST(id, a, b, c)                             			\
     50  1.1    ross     if (b) {                            				\
     51  1.1    ross 	c = (a) / (b);                        				\
     52  1.1    ross 	printf(id "%16jx / %16jx => %16jx\n", UIM(a), UIM(b), UIM(c));  \
     53  1.1    ross 	c = (a) % (b);                        				\
     54  1.1    ross 	printf(id "%16jx / %16jx => %16jx\n", UIM(a), UIM(b), UIM(c));  \
     55  1.1    ross     }
     56  1.1    ross 
     57  1.2    ross #define	T64S(a, b, c)	TEST("64 ", (a), (b), (c))
     58  1.2    ross #define	T64U(a, b, c)	TEST("64U", (a), (b), (c))
     59  1.2    ross 
     60  1.2    ross union {
     61  1.2    ross 	char	ranstate[128];
     62  1.2    ross 	long	alignme;
     63  1.2    ross } ranalign;
     64  1.2    ross 
     65  1.2    ross int enable_time_output;
     66  1.2    ross 
     67  1.2    ross void mark_time(const int phase)
     68  1.2    ross {
     69  1.2    ross static	time_t startphase;
     70  1.2    ross 	time_t t;
     71  1.2    ross 
     72  1.2    ross 	t = time(NULL);
     73  1.2    ross 	if (enable_time_output && phase != 0) {
     74  1.2    ross 		fprintf(stderr, "phase %d/6: %5d seconds\n", phase,
     75  1.2    ross 		    (int)(t - startphase));
     76  1.2    ross 		fflush(stderr);
     77  1.2    ross 	}
     78  1.2    ross 	startphase = t;
     79  1.2    ross }
     80  1.1    ross 
     81  1.1    ross int main(int ac, char **av)
     82  1.1    ross {
     83  1.2    ross      int32_t a32, b32, sr32;
     84  1.1    ross     uint32_t ur32;
     85  1.2    ross      intmax_t a64, b64, sr64;
     86  1.1    ross     uintmax_t ur64;
     87  1.2    ross      int i, j, k, l;
     88  1.1    ross 
     89  1.2    ross     enable_time_output = ac <= 1;
     90  1.2    ross     mark_time(0);
     91  1.2    ross     for(i = KLE; i <= 30; ++i) {
     92  1.2    ross 	a32 = 1 << i;
     93  1.2    ross 	for(j = KLE; j <= 30; ++j) {
     94  1.2    ross 	    b32 = 1 << j;
     95  1.2    ross 	    for(k = -(1 << KLE); k <= 1 << KLE; ++k) {
     96  1.2    ross 		for(l = -(1 << KLE); l <= 1 << KLE; ++l) {
     97  1.2    ross 		    TEST("32 ",  a32 + k,  b32 + l, sr32);
     98  1.2    ross 		    TEST("32 ",  a32 + k, -(b32 + l), sr32);
     99  1.2    ross 		    TEST("32 ", -(a32 + k),   b32 + l, sr32);
    100  1.2    ross 		    TEST("32 ", -(a32 + k), -(b32 + l), sr32);
    101  1.2    ross 		    assert((1U << i) + k >= 0);
    102  1.2    ross 		    assert((1U << j) + l >= 0);
    103  1.1    ross 		    TEST("32U", (1U << i) + k, (1U << j) + l, ur32);
    104  1.1    ross 		}
    105  1.1    ross 	    }
    106  1.1    ross 	}
    107  1.1    ross     }
    108  1.2    ross 
    109  1.2    ross     mark_time(1);
    110  1.2    ross     for(a32 = -NEARBY; a32 < NEARBY; ++a32) {
    111  1.2    ross 	for(b32 = -NEARBY; b32 < NEARBY; ++b32) {
    112  1.2    ross 	    TEST("32 ", a32, b32, sr32);
    113  1.2    ross 	    if (a32 >= 0 && b32 >= 0)
    114  1.2    ross 		TEST("32U", (unsigned)a32, (unsigned)b32, ur32);
    115  1.2    ross 	}
    116  1.2    ross     }
    117  1.2    ross     mark_time(2);
    118  1.2    ross     for(a32 = INT32_MIN; a32 < INT32_MIN + NEARBY; ++a32) {
    119  1.2    ross 	for(b32 = INT32_MIN; b32 < INT32_MIN + NEARBY; ++b32)
    120  1.2    ross 	    TEST("32 ", a32, b32, sr32);
    121  1.2    ross 	for(b32 = -NEARBY; b32 < NEARBY; ++b32)
    122  1.2    ross 	    if (a32 != INT32_MIN || b32 != -1)
    123  1.2    ross 		TEST("32 ", a32, b32, sr32);
    124  1.2    ross     }
    125  1.2    ross 
    126  1.2    ross     mark_time(3);
    127  1.1    ross     if (sizeof(intmax_t) == 4)
    128  1.1    ross 	exit(0);
    129  1.2    ross     for(i = KLE; i <= 62; ++i) {
    130  1.2    ross 	a64 = IM(1) << i;
    131  1.2    ross 	for(j = KLE; j <= 62; ++j) {
    132  1.2    ross 	    b64 = IM(1) << j;
    133  1.2    ross 	    for(k = -(1 << KLE); k <= 1 << KLE; ++k) {
    134  1.2    ross 		for(l = -(1 << KLE); l <= 1 << KLE; ++l) {
    135  1.2    ross 		    T64S( a64 + k,  b64 + l, sr64);
    136  1.2    ross 		    T64S( a64 + k, -b64 + l, sr64);
    137  1.2    ross 		    T64S(-a64 + k,  b64 + l, sr64);
    138  1.2    ross 		    T64S(-a64 + k, -b64 + l, sr64);
    139  1.2    ross 		    T64U(UIM(a64) + k, UIM(b64) + l, ur64);
    140  1.1    ross 		}
    141  1.1    ross 	    }
    142  1.1    ross 	}
    143  1.1    ross     }
    144  1.2    ross 
    145  1.2    ross     mark_time(4);
    146  1.2    ross     for(a64 = -(1 << KLE); a64 < 1 << KLE; ++a64) {
    147  1.2    ross 	for(b64 = -(1 << KLE); b64 < 1 << KLE; ++b64) {
    148  1.2    ross 	    TEST("64 ", a64, b64, sr64);
    149  1.2    ross 	    if (a64 >= 0 && b64 >= 0)
    150  1.2    ross 		TEST("64U", (unsigned)a64, (unsigned)b64, ur64);
    151  1.2    ross 	}
    152  1.2    ross     }
    153  1.2    ross     for(a64 = INT64_MIN; a64 < INT64_MIN + NEARBY; ++a64) {
    154  1.2    ross 	for(b64 = INT64_MIN; b64 < INT64_MIN + NEARBY; ++b64)
    155  1.2    ross 	    TEST("64 ", a64, b64, sr64);
    156  1.2    ross 	for(b64 = -NEARBY; b64 < NEARBY; ++b64)
    157  1.2    ross 	    if (a64 != INT64_MIN || b64 != -1)
    158  1.2    ross 		TEST("64 ", a64, b64, sr64);
    159  1.2    ross     }
    160  1.2    ross     mark_time(5);
    161  1.2    ross     initstate(1UL, ranalign.ranstate, sizeof ranalign.ranstate);
    162  1.2    ross     for(i = 0; i < RANDOMCOUNT; ++i) {
    163  1.2    ross 	int32_t low32 = random();
    164  1.2    ross 	int64_t low64 = (intmax_t)random() << 32 | low32;
    165  1.2    ross 
    166  1.2    ross 	a32 = random();
    167  1.2    ross 	b32 = random();
    168  1.2    ross 	a64 = ((intmax_t)random() << 32) | a32;
    169  1.2    ross 	b64 = ((intmax_t)random() << 32) | b32;
    170  1.2    ross 	TEST("32 ", a32, b32, sr32);
    171  1.2    ross 	TEST("32u", (unsigned)a32 + low32, (unsigned)b32 + low32, ur32);
    172  1.2    ross 	TEST("32 ", -a32 - 1, b32, sr32);
    173  1.2    ross 	TEST("32 ", a32, -b32, sr32);
    174  1.2    ross 	if (a32 != INT32_MAX || b32 != 1)
    175  1.2    ross 	    TEST("32 ", -a32 - 1, -b32, sr32);
    176  1.2    ross 	TEST("64 ", a64, b64, sr64);
    177  1.2    ross 	TEST("64u", (unsigned)a64 + low64, (unsigned)b64 + low64, ur64);
    178  1.2    ross 	TEST("64 ", -a64 - 1, b64, sr64);
    179  1.2    ross 	TEST("64 ", a64, -b64, sr64);
    180  1.2    ross 	if (a64 != INT64_MAX || b64 != 1)
    181  1.2    ross 	    TEST("64 ", -a64 - 1, -b64, sr64);
    182  1.2    ross     }
    183  1.2    ross     mark_time(6);
    184  1.1    ross     exit(0);
    185  1.1    ross     return 0;
    186  1.1    ross }
    187