xref: /freebsd/sys/libkern/quad.h (revision 943e5db958faa4c8a7237df4f7d32aaf1a21ed78)
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 
41943e5db9SPoul-Henning Kamp #ifndef _LIBKERN_QUAD_H
42943e5db9SPoul-Henning Kamp #define _LIBKERN_QUAD_H
43943e5db9SPoul-Henning Kamp 
44df8bae1dSRodney W. Grimes /*
45df8bae1dSRodney W. Grimes  * Quad arithmetic.
46df8bae1dSRodney W. Grimes  *
47df8bae1dSRodney W. Grimes  * This library makes the following assumptions:
48df8bae1dSRodney W. Grimes  *
49df8bae1dSRodney W. Grimes  *  - The type long long (aka quad_t) exists.
50df8bae1dSRodney W. Grimes  *
51df8bae1dSRodney W. Grimes  *  - A quad variable is exactly twice as long as `long'.
52df8bae1dSRodney W. Grimes  *
53df8bae1dSRodney W. Grimes  *  - The machine's arithmetic is two's complement.
54df8bae1dSRodney W. Grimes  *
55df8bae1dSRodney W. Grimes  * This library can provide 128-bit arithmetic on a machine with 128-bit
56df8bae1dSRodney W. Grimes  * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
57df8bae1dSRodney W. Grimes  * with 48-bit longs.
58df8bae1dSRodney W. Grimes  */
59df8bae1dSRodney W. Grimes 
6087b620baSBruce Evans #include <sys/cdefs.h>
61df8bae1dSRodney W. Grimes #include <sys/types.h>
620b1ae809SJulian Elischer #include <sys/syslimits.h>
630b1ae809SJulian Elischer #include <machine/limits.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes /*
66df8bae1dSRodney W. Grimes  * Depending on the desired operation, we view a `long long' (aka quad_t) in
67df8bae1dSRodney W. Grimes  * one or more of the following formats.
68df8bae1dSRodney W. Grimes  */
69df8bae1dSRodney W. Grimes union uu {
70df8bae1dSRodney W. Grimes 	quad_t	q;		/* as a (signed) quad */
71df8bae1dSRodney W. Grimes 	quad_t	uq;		/* as an unsigned quad */
72df8bae1dSRodney W. Grimes 	long	sl[2];		/* as two signed longs */
73df8bae1dSRodney W. Grimes 	u_long	ul[2];		/* as two unsigned longs */
74df8bae1dSRodney W. Grimes };
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes /*
77df8bae1dSRodney W. Grimes  * Define high and low longwords.
78df8bae1dSRodney W. Grimes  */
79df8bae1dSRodney W. Grimes #define	H		_QUAD_HIGHWORD
80df8bae1dSRodney W. Grimes #define	L		_QUAD_LOWWORD
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes /*
83df8bae1dSRodney W. Grimes  * Total number of bits in a quad_t and in the pieces that make it up.
84df8bae1dSRodney W. Grimes  * These are used for shifting, and also below for halfword extraction
85df8bae1dSRodney W. Grimes  * and assembly.
86df8bae1dSRodney W. Grimes  */
87df8bae1dSRodney W. Grimes #define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
88df8bae1dSRodney W. Grimes #define	LONG_BITS	(sizeof(long) * CHAR_BIT)
89df8bae1dSRodney W. Grimes #define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes /*
92df8bae1dSRodney W. Grimes  * Extract high and low shortwords from longword, and move low shortword of
93df8bae1dSRodney W. Grimes  * longword to upper half of long, i.e., produce the upper longword of
94df8bae1dSRodney W. Grimes  * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
95df8bae1dSRodney W. Grimes  *
96df8bae1dSRodney W. Grimes  * These are used in the multiply code, to split a longword into upper
97df8bae1dSRodney W. Grimes  * and lower halves, and to reassemble a product as a quad_t, shifted left
98df8bae1dSRodney W. Grimes  * (sizeof(long)*CHAR_BIT/2).
99df8bae1dSRodney W. Grimes  */
100df8bae1dSRodney W. Grimes #define	HHALF(x)	((x) >> HALF_BITS)
101df8bae1dSRodney W. Grimes #define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
102df8bae1dSRodney W. Grimes #define	LHUP(x)		((x) << HALF_BITS)
103df8bae1dSRodney W. Grimes 
1048febc6baSAlfred Perlstein quad_t		__divdi3(quad_t a, quad_t b);
1058febc6baSAlfred Perlstein quad_t		__moddi3(quad_t a, quad_t b);
1068febc6baSAlfred Perlstein u_quad_t	__qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem);
1078febc6baSAlfred Perlstein u_quad_t	__udivdi3(u_quad_t a, u_quad_t b);
1088febc6baSAlfred Perlstein u_quad_t	__umoddi3(u_quad_t a, u_quad_t b);
1098febc6baSAlfred Perlstein int		__ucmpdi2(u_quad_t a, u_quad_t b);
110df8bae1dSRodney W. Grimes 
111df8bae1dSRodney W. Grimes /*
112df8bae1dSRodney W. Grimes  * XXX
113df8bae1dSRodney W. Grimes  * Compensate for gcc 1 vs gcc 2.  Gcc 1 defines ?sh?di3's second argument
114df8bae1dSRodney W. Grimes  * as u_quad_t, while gcc 2 correctly uses int.  Unfortunately, we still use
115df8bae1dSRodney W. Grimes  * both compilers.
116df8bae1dSRodney W. Grimes  */
117df8bae1dSRodney W. Grimes #if __GNUC__ >= 2
118df8bae1dSRodney W. Grimes typedef unsigned int	qshift_t;
119df8bae1dSRodney W. Grimes #else
120df8bae1dSRodney W. Grimes typedef u_quad_t	qshift_t;
121df8bae1dSRodney W. Grimes #endif
122943e5db9SPoul-Henning Kamp #endif /* _LIBKERN_QUAD_H */
123