quad.h revision 40874
1130803Smarcel/*-
2130803Smarcel * Copyright (c) 1992, 1993
3130803Smarcel *	The Regents of the University of California.  All rights reserved.
4130803Smarcel *
5130803Smarcel * This software was developed by the Computer Systems Engineering group
6130803Smarcel * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7130803Smarcel * contributed to Berkeley.
8130803Smarcel *
9130803Smarcel * Redistribution and use in source and binary forms, with or without
10130803Smarcel * modification, are permitted provided that the following conditions
11130803Smarcel * are met:
12130803Smarcel * 1. Redistributions of source code must retain the above copyright
13130803Smarcel *    notice, this list of conditions and the following disclaimer.
14130803Smarcel * 2. Redistributions in binary form must reproduce the above copyright
15130803Smarcel *    notice, this list of conditions and the following disclaimer in the
16130803Smarcel *    documentation and/or other materials provided with the distribution.
17130803Smarcel * 3. All advertising materials mentioning features or use of this software
18130803Smarcel *    must display the following acknowledgement:
19130803Smarcel *	This product includes software developed by the University of
20130803Smarcel *	California, Berkeley and its contributors.
21130803Smarcel * 4. Neither the name of the University nor the names of its contributors
22130803Smarcel *    may be used to endorse or promote products derived from this software
23130803Smarcel *    without specific prior written permission.
24130803Smarcel *
25130803Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26130803Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27130803Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28130803Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29130803Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30130803Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31130803Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32130803Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33130803Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34130803Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35130803Smarcel * SUCH DAMAGE.
36130803Smarcel *
37130803Smarcel *	@(#)quad.h	8.1 (Berkeley) 6/4/93
38130803Smarcel *	$Id: quad.h,v 1.6 1997/02/22 09:39:58 peter Exp $
39130803Smarcel */
40130803Smarcel
41130803Smarcel/*
42130803Smarcel * Quad arithmetic.
43130803Smarcel *
44130803Smarcel * This library makes the following assumptions:
45130803Smarcel *
46130803Smarcel *  - The type long long (aka quad_t) exists.
47130803Smarcel *
48130803Smarcel *  - A quad variable is exactly twice as long as `long'.
49130803Smarcel *
50130803Smarcel *  - The machine's arithmetic is two's complement.
51130803Smarcel *
52130803Smarcel * This library can provide 128-bit arithmetic on a machine with 128-bit
53130803Smarcel * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
54130803Smarcel * with 48-bit longs.
55130803Smarcel */
56130803Smarcel
57130803Smarcel#include <sys/cdefs.h>
58130803Smarcel#include <sys/types.h>
59130803Smarcel#include <limits.h>
60130803Smarcel
61130803Smarcel/*
62130803Smarcel * Depending on the desired operation, we view a `long long' (aka quad_t) in
63130803Smarcel * one or more of the following formats.
64130803Smarcel */
65130803Smarcelunion uu {
66130803Smarcel	quad_t	q;		/* as a (signed) quad */
67130803Smarcel	quad_t	uq;		/* as an unsigned quad */
68130803Smarcel	long	sl[2];		/* as two signed longs */
69130803Smarcel	u_long	ul[2];		/* as two unsigned longs */
70130803Smarcel};
71130803Smarcel
72130803Smarcel/*
73130803Smarcel * Define high and low longwords.
74130803Smarcel */
75130803Smarcel#define	H		_QUAD_HIGHWORD
76130803Smarcel#define	L		_QUAD_LOWWORD
77130803Smarcel
78130803Smarcel/*
79130803Smarcel * Total number of bits in a quad_t and in the pieces that make it up.
80130803Smarcel * These are used for shifting, and also below for halfword extraction
81130803Smarcel * and assembly.
82130803Smarcel */
83130803Smarcel#define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
84130803Smarcel#define	LONG_BITS	(sizeof(long) * CHAR_BIT)
85130803Smarcel#define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
86130803Smarcel
87130803Smarcel/*
88130803Smarcel * Extract high and low shortwords from longword, and move low shortword of
89130803Smarcel * longword to upper half of long, i.e., produce the upper longword of
90130803Smarcel * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
91130803Smarcel *
92130803Smarcel * These are used in the multiply code, to split a longword into upper
93130803Smarcel * and lower halves, and to reassemble a product as a quad_t, shifted left
94130803Smarcel * (sizeof(long)*CHAR_BIT/2).
95130803Smarcel */
96130803Smarcel#define	HHALF(x)	((x) >> HALF_BITS)
97130803Smarcel#define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
98130803Smarcel#define	LHUP(x)		((x) << HALF_BITS)
99130803Smarcel
100130803Smarcelquad_t		__divdi3 __P((quad_t a, quad_t b));
101130803Smarcelquad_t		__moddi3 __P((quad_t a, quad_t b));
102130803Smarcelu_quad_t	__qdivrem __P((u_quad_t u, u_quad_t v, u_quad_t *rem));
103130803Smarcelu_quad_t	__udivdi3 __P((u_quad_t a, u_quad_t b));
104130803Smarcelu_quad_t	__umoddi3 __P((u_quad_t a, u_quad_t b));
105130803Smarcel
106130803Smarcel/*
107130803Smarcel * XXX
108130803Smarcel * Compensate for gcc 1 vs gcc 2.  Gcc 1 defines ?sh?di3's second argument
109130803Smarcel * as u_quad_t, while gcc 2 correctly uses int.  Unfortunately, we still use
110130803Smarcel * both compilers.
111130803Smarcel */
112130803Smarcel#if __GNUC__ >= 2
113130803Smarceltypedef unsigned int	qshift_t;
114130803Smarcel#else
115130803Smarceltypedef u_quad_t	qshift_t;
116130803Smarcel#endif
117130803Smarcel