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