xref: /freebsd/sys/libkern/quad.h (revision 8febc6ba174e3f73f16b428e4e2cef352bfaab35)
1df8bae1dSRodney W. Grimes /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1992, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This software was developed by the Computer Systems Engineering group
6df8bae1dSRodney W. Grimes  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7df8bae1dSRodney W. Grimes  * contributed to Berkeley.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
17df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
18df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
19df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
20df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
21df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  *	@(#)quad.h	8.1 (Berkeley) 6/4/93
38c3aac50fSPeter Wemm  * $FreeBSD$
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes /*
42df8bae1dSRodney W. Grimes  * Quad arithmetic.
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * This library makes the following assumptions:
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  *  - The type long long (aka quad_t) exists.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  *  - A quad variable is exactly twice as long as `long'.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  *  - The machine's arithmetic is two's complement.
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  * This library can provide 128-bit arithmetic on a machine with 128-bit
53df8bae1dSRodney W. Grimes  * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
54df8bae1dSRodney W. Grimes  * with 48-bit longs.
55df8bae1dSRodney W. Grimes  */
56df8bae1dSRodney W. Grimes 
5787b620baSBruce Evans #include <sys/cdefs.h>
58df8bae1dSRodney W. Grimes #include <sys/types.h>
590b1ae809SJulian Elischer #include <sys/syslimits.h>
600b1ae809SJulian Elischer #include <machine/limits.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes /*
63df8bae1dSRodney W. Grimes  * Depending on the desired operation, we view a `long long' (aka quad_t) in
64df8bae1dSRodney W. Grimes  * one or more of the following formats.
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes union uu {
67df8bae1dSRodney W. Grimes 	quad_t	q;		/* as a (signed) quad */
68df8bae1dSRodney W. Grimes 	quad_t	uq;		/* as an unsigned quad */
69df8bae1dSRodney W. Grimes 	long	sl[2];		/* as two signed longs */
70df8bae1dSRodney W. Grimes 	u_long	ul[2];		/* as two unsigned longs */
71df8bae1dSRodney W. Grimes };
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes /*
74df8bae1dSRodney W. Grimes  * Define high and low longwords.
75df8bae1dSRodney W. Grimes  */
76df8bae1dSRodney W. Grimes #define	H		_QUAD_HIGHWORD
77df8bae1dSRodney W. Grimes #define	L		_QUAD_LOWWORD
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes /*
80df8bae1dSRodney W. Grimes  * Total number of bits in a quad_t and in the pieces that make it up.
81df8bae1dSRodney W. Grimes  * These are used for shifting, and also below for halfword extraction
82df8bae1dSRodney W. Grimes  * and assembly.
83df8bae1dSRodney W. Grimes  */
84df8bae1dSRodney W. Grimes #define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
85df8bae1dSRodney W. Grimes #define	LONG_BITS	(sizeof(long) * CHAR_BIT)
86df8bae1dSRodney W. Grimes #define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes /*
89df8bae1dSRodney W. Grimes  * Extract high and low shortwords from longword, and move low shortword of
90df8bae1dSRodney W. Grimes  * longword to upper half of long, i.e., produce the upper longword of
91df8bae1dSRodney W. Grimes  * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
92df8bae1dSRodney W. Grimes  *
93df8bae1dSRodney W. Grimes  * These are used in the multiply code, to split a longword into upper
94df8bae1dSRodney W. Grimes  * and lower halves, and to reassemble a product as a quad_t, shifted left
95df8bae1dSRodney W. Grimes  * (sizeof(long)*CHAR_BIT/2).
96df8bae1dSRodney W. Grimes  */
97df8bae1dSRodney W. Grimes #define	HHALF(x)	((x) >> HALF_BITS)
98df8bae1dSRodney W. Grimes #define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
99df8bae1dSRodney W. Grimes #define	LHUP(x)		((x) << HALF_BITS)
100df8bae1dSRodney W. Grimes 
1018febc6baSAlfred Perlstein quad_t		__divdi3(quad_t a, quad_t b);
1028febc6baSAlfred Perlstein quad_t		__moddi3(quad_t a, quad_t b);
1038febc6baSAlfred Perlstein u_quad_t	__qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem);
1048febc6baSAlfred Perlstein u_quad_t	__udivdi3(u_quad_t a, u_quad_t b);
1058febc6baSAlfred Perlstein u_quad_t	__umoddi3(u_quad_t a, u_quad_t b);
1068febc6baSAlfred Perlstein int		__ucmpdi2(u_quad_t a, u_quad_t b);
107df8bae1dSRodney W. Grimes 
108df8bae1dSRodney W. Grimes /*
109df8bae1dSRodney W. Grimes  * XXX
110df8bae1dSRodney W. Grimes  * Compensate for gcc 1 vs gcc 2.  Gcc 1 defines ?sh?di3's second argument
111df8bae1dSRodney W. Grimes  * as u_quad_t, while gcc 2 correctly uses int.  Unfortunately, we still use
112df8bae1dSRodney W. Grimes  * both compilers.
113df8bae1dSRodney W. Grimes  */
114df8bae1dSRodney W. Grimes #if __GNUC__ >= 2
115df8bae1dSRodney W. Grimes typedef unsigned int	qshift_t;
116df8bae1dSRodney W. Grimes #else
117df8bae1dSRodney W. Grimes typedef u_quad_t	qshift_t;
118df8bae1dSRodney W. Grimes #endif
119