xref: /netbsd-src/lib/libc/quad/TESTS/divrem.c (revision eb7c1594f145c931049e1fd9eb056a5987e87e59)
1*eb7c1594Sagc /*	$NetBSD: divrem.c,v 1.4 2003/08/07 16:43:18 agc Exp $	*/
2d48964d4Scgd 
3edfa8336Scgd /*-
4edfa8336Scgd  * Copyright (c) 1992, 1993
5edfa8336Scgd  *	The Regents of the University of California.  All rights reserved.
6edfa8336Scgd  *
7edfa8336Scgd  * This software was developed by the Computer Systems Engineering group
8edfa8336Scgd  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9edfa8336Scgd  * contributed to Berkeley.
10edfa8336Scgd  *
11edfa8336Scgd  * Redistribution and use in source and binary forms, with or without
12edfa8336Scgd  * modification, are permitted provided that the following conditions
13edfa8336Scgd  * are met:
14edfa8336Scgd  * 1. Redistributions of source code must retain the above copyright
15edfa8336Scgd  *    notice, this list of conditions and the following disclaimer.
16edfa8336Scgd  * 2. Redistributions in binary form must reproduce the above copyright
17edfa8336Scgd  *    notice, this list of conditions and the following disclaimer in the
18edfa8336Scgd  *    documentation and/or other materials provided with the distribution.
19*eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
20edfa8336Scgd  *    may be used to endorse or promote products derived from this software
21edfa8336Scgd  *    without specific prior written permission.
22edfa8336Scgd  *
23edfa8336Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24edfa8336Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25edfa8336Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26edfa8336Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27edfa8336Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28edfa8336Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29edfa8336Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30edfa8336Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31edfa8336Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32edfa8336Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33edfa8336Scgd  * SUCH DAMAGE.
34edfa8336Scgd  */
35edfa8336Scgd 
36edfa8336Scgd #ifndef lint
37edfa8336Scgd static char copyright[] =
38edfa8336Scgd "@(#) Copyright (c) 1992, 1993\n\
39edfa8336Scgd 	The Regents of the University of California.  All rights reserved.\n";
40edfa8336Scgd #endif /* not lint */
41edfa8336Scgd 
42edfa8336Scgd #ifndef lint
43d48964d4Scgd #if 0
44edfa8336Scgd static char sccsid[] = "@(#)divrem.c	8.1 (Berkeley) 6/4/93";
45d48964d4Scgd #else
46*eb7c1594Sagc static char rcsid[] = "$NetBSD: divrem.c,v 1.4 2003/08/07 16:43:18 agc Exp $";
47d48964d4Scgd #endif
48edfa8336Scgd #endif /* not lint */
49edfa8336Scgd 
50edfa8336Scgd #include <stdio.h>
51edfa8336Scgd 
main()52edfa8336Scgd main()
53edfa8336Scgd {
5437b34d51Sscw 	union { long long q; unsigned int v[2]; } a, b, q, r;
55edfa8336Scgd 	char buf[300];
56edfa8336Scgd 	extern long long __qdivrem(unsigned long long, unsigned long long,
57edfa8336Scgd 	    unsigned long long *);
58edfa8336Scgd 
59edfa8336Scgd 	for (;;) {
60edfa8336Scgd 		printf("> ");
61edfa8336Scgd 		if (fgets(buf, sizeof buf, stdin) == NULL)
62edfa8336Scgd 			break;
6337b34d51Sscw 		if (sscanf(buf, "%u:%u %u:%u",
64edfa8336Scgd 			    &a.v[0], &a.v[1], &b.v[0], &b.v[1]) != 4 &&
6537b34d51Sscw 		    sscanf(buf, "0x%x:%x 0x%x:%x",
66edfa8336Scgd 			    &a.v[0], &a.v[1], &b.v[0], &b.v[1]) != 4) {
67edfa8336Scgd 			printf("eh?\n");
68edfa8336Scgd 			continue;
69edfa8336Scgd 		}
70edfa8336Scgd 		q.q = __qdivrem(a.q, b.q, &r.q);
7137b34d51Sscw 		printf("%x:%x /%% %x:%x => q=%x:%x r=%x:%x\n",
72edfa8336Scgd 		    a.v[0], a.v[1], b.v[0], b.v[1],
73edfa8336Scgd 		    q.v[0], q.v[1], r.v[0], r.v[1]);
7437b34d51Sscw 		printf("  = %X%08X / %X%08X => %X%08X\n\
7537b34d51Sscw   = %X%08X %% %X%08X => %X%08X\n",
76edfa8336Scgd 		    a.v[0], a.v[1], b.v[0], b.v[1], q.v[0], q.v[1],
77edfa8336Scgd 		    a.v[0], a.v[1], b.v[0], b.v[1], r.v[0], r.v[1]);
78edfa8336Scgd 	}
79edfa8336Scgd 	exit(0);
80edfa8336Scgd }
81