xref: /csrg-svn/lib/libc/quad/quad.h (revision 53795)
153439Sbostic /*-
253439Sbostic  * Copyright (c) 1992 The Regents of the University of California.
353439Sbostic  * All rights reserved.
453439Sbostic  *
553794Sbostic  * This software was developed by the Computer Systems Engineering group
653794Sbostic  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
753794Sbostic  * contributed to Berkeley.
853794Sbostic  *
953439Sbostic  * %sccs.include.redist.c%
10*53795Sbostic  *
11*53795Sbostic  *	@(#)quad.h	5.6 (Berkeley) 06/02/92
1253794Sbostic  */
1353794Sbostic 
1453794Sbostic /*
1553794Sbostic  * Quad arithmetic.
1653439Sbostic  *
1753794Sbostic  * This library makes the following assumptions:
1853794Sbostic  *
1953794Sbostic  *  - The type long long (aka quad) exists.
2053794Sbostic  *
2153794Sbostic  *  - A quad variable is exactly twice as long as `long'.
2253794Sbostic  *
2353794Sbostic  *  - The machine's arithmetic is two's complement.
2453794Sbostic  *
2553794Sbostic  * All other machine parameters are encapsulated here.  This library can
2653794Sbostic  * provide 128-bit arithmetic on a machine with 128-bit quads and 64-bit
2753794Sbostic  * longs, for instance, or 96-bit arithmetic on machines with 48-bit longs.
2853439Sbostic  */
2953439Sbostic 
3053794Sbostic #ifndef SPARC_XXX
3153794Sbostic #include <machine/endian.h>		/* see #else case */
3253794Sbostic #else
3353794Sbostic /*
3453794Sbostic  * These are for testing and for illustration: we expect <machine/endian.h>
3553794Sbostic  * to define these.  Actually, these match most big-endian machines; for
3653794Sbostic  * most little-endian machines, all you need do is exchange _QUAD_HIGHWORD
3753794Sbostic  * and _QUAD_LOWWORD.
3853794Sbostic  */
39*53795Sbostic #define _QUAD_HIGHWORD	0
40*53795Sbostic #define _QUAD_LOWWORD	1
4153794Sbostic #endif
4253459Sbostic 
4353794Sbostic typedef long long quad;
4453794Sbostic typedef unsigned long long u_quad;
4553794Sbostic typedef unsigned long u_long;
4653459Sbostic 
4753794Sbostic #include <limits.h>
4853794Sbostic /*
4953794Sbostic  * We expect something like these from <limits.h>, which should be provided on
5053794Sbostic  * any ANSI C system.
5153794Sbostic #define	USHRT_MAX	0xffff
5253794Sbostic #define	CHAR_BIT	8
5353794Sbostic  */
5453459Sbostic 
5553794Sbostic /*
5653794Sbostic  * Depending on the desired operation, we view a `long long' (aka quad) in
5753794Sbostic  * one or more of the following formats.
5853794Sbostic  */
5953794Sbostic union uu {
6053794Sbostic 	quad	q;		/* as a (signed) quad */
6153794Sbostic 	quad	uq;		/* as an unsigned quad */
6253794Sbostic 	long	sl[2];		/* as two signed longs */
6353794Sbostic 	u_long	ul[2];		/* as two unsigned longs */
6453794Sbostic };
6553459Sbostic 
6653794Sbostic /*
6753794Sbostic  * Define high and low longwords.
6853794Sbostic  */
6953794Sbostic #define	H		_QUAD_HIGHWORD
7053794Sbostic #define	L		_QUAD_LOWWORD
7153459Sbostic 
7253794Sbostic /*
7353794Sbostic  * Total number of bits in a quad and in the pieces that make it up.
7453794Sbostic  * These are used for shifting, and also below for halfword extraction
7553794Sbostic  * and assembly.
7653794Sbostic  */
7753794Sbostic #define	QUAD_BITS	(sizeof(quad) * CHAR_BIT)
7853794Sbostic #define	LONG_BITS	(sizeof(long) * CHAR_BIT)
7953794Sbostic #define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
8053459Sbostic 
8153794Sbostic /*
8253794Sbostic  * Extract high and low shortwords from longword, and move low shortword of
8353794Sbostic  * longword to upper half of long, i.e., produce the upper longword of
8453794Sbostic  * ((quad)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
8553794Sbostic  *
8653794Sbostic  * These are used in the multiply code, to split a longword into upper
8753794Sbostic  * and lower halves, and to reassemble a product as a quad, shifted left
8853794Sbostic  * (sizeof(long)*CHAR_BIT/2).
8953794Sbostic  */
9053794Sbostic #define	HHALF(x)	((x) >> HALF_BITS)
9153794Sbostic #define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
9253794Sbostic #define	LHUP(x)		((x) << HALF_BITS)
9353439Sbostic 
9453794Sbostic extern u_quad __qdivrem(u_quad u, u_quad v, u_quad *rem);
95