xref: /csrg-svn/lib/libc/quad/quad.h (revision 53794)
153439Sbostic /*-
253439Sbostic  * Copyright (c) 1992 The Regents of the University of California.
353439Sbostic  * All rights reserved.
453439Sbostic  *
5*53794Sbostic  * This software was developed by the Computer Systems Engineering group
6*53794Sbostic  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7*53794Sbostic  * contributed to Berkeley.
8*53794Sbostic  *
953439Sbostic  * %sccs.include.redist.c%
10*53794Sbostic  */
11*53794Sbostic 
12*53794Sbostic /*
13*53794Sbostic  * Quad arithmetic.
1453439Sbostic  *
15*53794Sbostic  * This library makes the following assumptions:
16*53794Sbostic  *
17*53794Sbostic  *  - The type long long (aka quad) exists.
18*53794Sbostic  *
19*53794Sbostic  *  - A quad variable is exactly twice as long as `long'.
20*53794Sbostic  *
21*53794Sbostic  *  - The machine's arithmetic is two's complement.
22*53794Sbostic  *
23*53794Sbostic  * All other machine parameters are encapsulated here.  This library can
24*53794Sbostic  * provide 128-bit arithmetic on a machine with 128-bit quads and 64-bit
25*53794Sbostic  * longs, for instance, or 96-bit arithmetic on machines with 48-bit longs.
2653439Sbostic  */
2753439Sbostic 
28*53794Sbostic #ifndef SPARC_XXX
29*53794Sbostic #include <machine/endian.h>		/* see #else case */
30*53794Sbostic #else
31*53794Sbostic /*
32*53794Sbostic  * These are for testing and for illustration: we expect <machine/endian.h>
33*53794Sbostic  * to define these.  Actually, these match most big-endian machines; for
34*53794Sbostic  * most little-endian machines, all you need do is exchange _QUAD_HIGHWORD
35*53794Sbostic  * and _QUAD_LOWWORD.
36*53794Sbostic  */
37*53794Sbostic #define _QUAD_HIGHWORD 0
38*53794Sbostic #define _QUAD_LOWWORD 1
39*53794Sbostic #endif
4053459Sbostic 
41*53794Sbostic typedef long long quad;
42*53794Sbostic typedef unsigned long long u_quad;
43*53794Sbostic typedef unsigned long u_long;
4453459Sbostic 
45*53794Sbostic #include <limits.h>
46*53794Sbostic /*
47*53794Sbostic  * We expect something like these from <limits.h>, which should be provided on
48*53794Sbostic  * any ANSI C system.
49*53794Sbostic #define	USHRT_MAX	0xffff
50*53794Sbostic #define	CHAR_BIT	8
51*53794Sbostic  */
5253459Sbostic 
53*53794Sbostic /*
54*53794Sbostic  * Depending on the desired operation, we view a `long long' (aka quad) in
55*53794Sbostic  * one or more of the following formats.
56*53794Sbostic  */
57*53794Sbostic union uu {
58*53794Sbostic 	quad	q;		/* as a (signed) quad */
59*53794Sbostic 	quad	uq;		/* as an unsigned quad */
60*53794Sbostic 	long	sl[2];		/* as two signed longs */
61*53794Sbostic 	u_long	ul[2];		/* as two unsigned longs */
62*53794Sbostic };
6353459Sbostic 
64*53794Sbostic /*
65*53794Sbostic  * Define high and low longwords.
66*53794Sbostic  */
67*53794Sbostic #define	H		_QUAD_HIGHWORD
68*53794Sbostic #define	L		_QUAD_LOWWORD
6953459Sbostic 
70*53794Sbostic /*
71*53794Sbostic  * Total number of bits in a quad and in the pieces that make it up.
72*53794Sbostic  * These are used for shifting, and also below for halfword extraction
73*53794Sbostic  * and assembly.
74*53794Sbostic  */
75*53794Sbostic #define	QUAD_BITS	(sizeof(quad) * CHAR_BIT)
76*53794Sbostic #define	LONG_BITS	(sizeof(long) * CHAR_BIT)
77*53794Sbostic #define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
7853459Sbostic 
79*53794Sbostic /*
80*53794Sbostic  * Extract high and low shortwords from longword, and move low shortword of
81*53794Sbostic  * longword to upper half of long, i.e., produce the upper longword of
82*53794Sbostic  * ((quad)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
83*53794Sbostic  *
84*53794Sbostic  * These are used in the multiply code, to split a longword into upper
85*53794Sbostic  * and lower halves, and to reassemble a product as a quad, shifted left
86*53794Sbostic  * (sizeof(long)*CHAR_BIT/2).
87*53794Sbostic  */
88*53794Sbostic #define	HHALF(x)	((x) >> HALF_BITS)
89*53794Sbostic #define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
90*53794Sbostic #define	LHUP(x)		((x) << HALF_BITS)
9153439Sbostic 
92*53794Sbostic extern u_quad __qdivrem(u_quad u, u_quad v, u_quad *rem);
93