xref: /illumos-gate/usr/src/boot/libsa/quad.h (revision 22028508)
1199767f8SToomas Soome /*-
2199767f8SToomas Soome  * Copyright (c) 1992, 1993
3199767f8SToomas Soome  *	The Regents of the University of California.  All rights reserved.
4199767f8SToomas Soome  *
5199767f8SToomas Soome  * This software was developed by the Computer Systems Engineering group
6199767f8SToomas Soome  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7199767f8SToomas Soome  * contributed to Berkeley.
8199767f8SToomas Soome  *
9199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
10199767f8SToomas Soome  * modification, are permitted provided that the following conditions
11199767f8SToomas Soome  * are met:
12199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
13199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
14199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
15199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
16199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
171974da4bSToomas Soome  * 3. Neither the name of the University nor the names of its contributors
18199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
19199767f8SToomas Soome  *    without specific prior written permission.
20199767f8SToomas Soome  *
21199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31199767f8SToomas Soome  * SUCH DAMAGE.
32199767f8SToomas Soome  *
33199767f8SToomas Soome  *	@(#)quad.h	8.1 (Berkeley) 6/4/93
34199767f8SToomas Soome  * $FreeBSD$
35199767f8SToomas Soome  */
36199767f8SToomas Soome 
37199767f8SToomas Soome /*
38199767f8SToomas Soome  * Quad arithmetic.
39199767f8SToomas Soome  *
40199767f8SToomas Soome  * This library makes the following assumptions:
41199767f8SToomas Soome  *
42199767f8SToomas Soome  *  - The type long long (aka quad_t) exists.
43199767f8SToomas Soome  *
44199767f8SToomas Soome  *  - A quad variable is exactly twice as long as `long'.
45199767f8SToomas Soome  *
46199767f8SToomas Soome  *  - The machine's arithmetic is two's complement.
47199767f8SToomas Soome  *
48199767f8SToomas Soome  * This library can provide 128-bit arithmetic on a machine with 128-bit
49199767f8SToomas Soome  * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
50199767f8SToomas Soome  * with 48-bit longs.
51199767f8SToomas Soome  */
52199767f8SToomas Soome 
53199767f8SToomas Soome #include <sys/cdefs.h>
54199767f8SToomas Soome #include <sys/types.h>
55199767f8SToomas Soome #include <limits.h>
56199767f8SToomas Soome 
57199767f8SToomas Soome _Static_assert(sizeof(quad_t) == sizeof(int) * 2,
58199767f8SToomas Soome 	"Bitwise function in libstand are broken on this architecture\n");
59199767f8SToomas Soome 
60199767f8SToomas Soome /*
61199767f8SToomas Soome  * Depending on the desired operation, we view a `long long' (aka quad_t) in
62199767f8SToomas Soome  * one or more of the following formats.
63199767f8SToomas Soome  */
64199767f8SToomas Soome union uu {
65199767f8SToomas Soome 	quad_t	q;		/* as a (signed) quad */
66199767f8SToomas Soome 	quad_t	uq;		/* as an unsigned quad */
67199767f8SToomas Soome 	int	sl[2];		/* as two signed ints */
68199767f8SToomas Soome 	u_int	ul[2];		/* as two unsigned ints */
69199767f8SToomas Soome };
70199767f8SToomas Soome 
71199767f8SToomas Soome /*
72199767f8SToomas Soome  * Define high and low longwords.
73199767f8SToomas Soome  */
74199767f8SToomas Soome #define	H		_QUAD_HIGHWORD
75199767f8SToomas Soome #define	L		_QUAD_LOWWORD
76199767f8SToomas Soome 
77199767f8SToomas Soome /*
78199767f8SToomas Soome  * Total number of bits in a quad_t and in the pieces that make it up.
79199767f8SToomas Soome  * These are used for shifting, and also below for halfword extraction
80199767f8SToomas Soome  * and assembly.
81199767f8SToomas Soome  */
82199767f8SToomas Soome #define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
83199767f8SToomas Soome #define	HALF_BITS	(sizeof(int) * CHAR_BIT / 2)
84199767f8SToomas Soome 
85199767f8SToomas Soome /*
86199767f8SToomas Soome  * Extract high and low shortwords from longword, and move low shortword of
87199767f8SToomas Soome  * longword to upper half of long, i.e., produce the upper longword of
88199767f8SToomas Soome  * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
89199767f8SToomas Soome  *
90199767f8SToomas Soome  * These are used in the multiply code, to split a longword into upper
91199767f8SToomas Soome  * and lower halves, and to reassemble a product as a quad_t, shifted left
92199767f8SToomas Soome  * (sizeof(long)*CHAR_BIT/2).
93199767f8SToomas Soome  */
94199767f8SToomas Soome #define	HHALF(x)	((x) >> HALF_BITS)
95199767f8SToomas Soome #define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
96199767f8SToomas Soome #define	LHUP(x)		((x) << HALF_BITS)
97199767f8SToomas Soome 
98199767f8SToomas Soome quad_t		__divdi3(quad_t a, quad_t b);
99199767f8SToomas Soome quad_t		__moddi3(quad_t a, quad_t b);
100d8b4f927SToomas Soome u_quad_t	__udivmoddi4(u_quad_t u, u_quad_t v, u_quad_t *rem);
101199767f8SToomas Soome u_quad_t	__udivdi3(u_quad_t a, u_quad_t b);
102199767f8SToomas Soome u_quad_t	__umoddi3(u_quad_t a, u_quad_t b);
103199767f8SToomas Soome 
104199767f8SToomas Soome /*
105199767f8SToomas Soome  * XXX
106199767f8SToomas Soome  * Compensate for gcc 1 vs gcc 2.  Gcc 1 defines ?sh?di3's second argument
107199767f8SToomas Soome  * as u_quad_t, while gcc 2 correctly uses int.  Unfortunately, we still use
108199767f8SToomas Soome  * both compilers.
109199767f8SToomas Soome  */
110199767f8SToomas Soome #if __GNUC__ >= 2
111199767f8SToomas Soome typedef unsigned int	qshift_t;
112199767f8SToomas Soome #else
113199767f8SToomas Soome typedef u_quad_t	qshift_t;
114199767f8SToomas Soome #endif
115