xref: /freebsd/sys/libkern/quad.h (revision c7e459ed6c95f1ad522d898f478ca67e43a2144e)
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  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)quad.h	8.1 (Berkeley) 6/4/93
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37c7e459edSBruce Evans #ifndef _LIBKERN_QUAD_H_
38c7e459edSBruce Evans #define	_LIBKERN_QUAD_H_
39943e5db9SPoul-Henning Kamp 
40df8bae1dSRodney W. Grimes /*
41df8bae1dSRodney W. Grimes  * Quad arithmetic.
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  * This library makes the following assumptions:
44df8bae1dSRodney W. Grimes  *
45df8bae1dSRodney W. Grimes  *  - The type long long (aka quad_t) exists.
46df8bae1dSRodney W. Grimes  *
47df8bae1dSRodney W. Grimes  *  - A quad variable is exactly twice as long as `long'.
48df8bae1dSRodney W. Grimes  *
49df8bae1dSRodney W. Grimes  *  - The machine's arithmetic is two's complement.
50df8bae1dSRodney W. Grimes  *
51df8bae1dSRodney W. Grimes  * This library can provide 128-bit arithmetic on a machine with 128-bit
52df8bae1dSRodney W. Grimes  * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
53df8bae1dSRodney W. Grimes  * with 48-bit longs.
54df8bae1dSRodney W. Grimes  */
55df8bae1dSRodney W. Grimes 
5687b620baSBruce Evans #include <sys/cdefs.h>
57df8bae1dSRodney W. Grimes #include <sys/types.h>
58104a9b7eSAlexander Kabaev #include <sys/limits.h>
590b1ae809SJulian Elischer #include <sys/syslimits.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  * Depending on the desired operation, we view a `long long' (aka quad_t) in
63df8bae1dSRodney W. Grimes  * one or more of the following formats.
64df8bae1dSRodney W. Grimes  */
65df8bae1dSRodney W. Grimes union uu {
66df8bae1dSRodney W. Grimes 	quad_t	q;		/* as a (signed) quad */
67df8bae1dSRodney W. Grimes 	quad_t	uq;		/* as an unsigned quad */
68df8bae1dSRodney W. Grimes 	long	sl[2];		/* as two signed longs */
69df8bae1dSRodney W. Grimes 	u_long	ul[2];		/* as two unsigned longs */
70df8bae1dSRodney W. Grimes };
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes /*
73df8bae1dSRodney W. Grimes  * Define high and low longwords.
74df8bae1dSRodney W. Grimes  */
75df8bae1dSRodney W. Grimes #define	H		_QUAD_HIGHWORD
76df8bae1dSRodney W. Grimes #define	L		_QUAD_LOWWORD
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes /*
79df8bae1dSRodney W. Grimes  * Total number of bits in a quad_t and in the pieces that make it up.
80df8bae1dSRodney W. Grimes  * These are used for shifting, and also below for halfword extraction
81df8bae1dSRodney W. Grimes  * and assembly.
82df8bae1dSRodney W. Grimes  */
83df8bae1dSRodney W. Grimes #define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
84df8bae1dSRodney W. Grimes #define	LONG_BITS	(sizeof(long) * CHAR_BIT)
85df8bae1dSRodney W. Grimes #define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
86df8bae1dSRodney W. Grimes 
87df8bae1dSRodney W. Grimes /*
88df8bae1dSRodney W. Grimes  * Extract high and low shortwords from longword, and move low shortword of
89df8bae1dSRodney W. Grimes  * longword to upper half of long, i.e., produce the upper longword of
90df8bae1dSRodney W. Grimes  * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
91df8bae1dSRodney W. Grimes  *
92df8bae1dSRodney W. Grimes  * These are used in the multiply code, to split a longword into upper
93df8bae1dSRodney W. Grimes  * and lower halves, and to reassemble a product as a quad_t, shifted left
94df8bae1dSRodney W. Grimes  * (sizeof(long)*CHAR_BIT/2).
95df8bae1dSRodney W. Grimes  */
96df8bae1dSRodney W. Grimes #define	HHALF(x)	((x) >> HALF_BITS)
97df8bae1dSRodney W. Grimes #define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
98df8bae1dSRodney W. Grimes #define	LHUP(x)		((x) << HALF_BITS)
99df8bae1dSRodney W. Grimes 
100c7e459edSBruce Evans typedef unsigned int	qshift_t;
101c7e459edSBruce Evans 
102c7e459edSBruce Evans quad_t		__ashldi3(quad_t, qshift_t);
103c7e459edSBruce Evans quad_t		__ashrdi3(quad_t, qshift_t);
1048febc6baSAlfred Perlstein quad_t		__divdi3(quad_t a, quad_t b);
105c7e459edSBruce Evans quad_t		__lshrdi3(quad_t, qshift_t);
1068febc6baSAlfred Perlstein quad_t		__moddi3(quad_t a, quad_t b);
1078febc6baSAlfred Perlstein u_quad_t	__qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem);
1088febc6baSAlfred Perlstein u_quad_t	__udivdi3(u_quad_t a, u_quad_t b);
1098febc6baSAlfred Perlstein u_quad_t	__umoddi3(u_quad_t a, u_quad_t b);
1108febc6baSAlfred Perlstein int		__ucmpdi2(u_quad_t a, u_quad_t b);
111df8bae1dSRodney W. Grimes 
112c7e459edSBruce Evans #endif /* !_LIBKERN_QUAD_H_ */
113