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