xref: /titanic_51/usr/src/boot/lib/libstand/quad.h (revision 331f34b72954b02ddf511cd1d82a5159b3f44a55)
14a5d661aSToomas Soome /*-
24a5d661aSToomas Soome  * Copyright (c) 1992, 1993
34a5d661aSToomas Soome  *	The Regents of the University of California.  All rights reserved.
44a5d661aSToomas Soome  *
54a5d661aSToomas Soome  * This software was developed by the Computer Systems Engineering group
64a5d661aSToomas Soome  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
74a5d661aSToomas Soome  * contributed to Berkeley.
84a5d661aSToomas Soome  *
94a5d661aSToomas Soome  * Redistribution and use in source and binary forms, with or without
104a5d661aSToomas Soome  * modification, are permitted provided that the following conditions
114a5d661aSToomas Soome  * are met:
124a5d661aSToomas Soome  * 1. Redistributions of source code must retain the above copyright
134a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer.
144a5d661aSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
154a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
164a5d661aSToomas Soome  *    documentation and/or other materials provided with the distribution.
174a5d661aSToomas Soome  * 4. Neither the name of the University nor the names of its contributors
184a5d661aSToomas Soome  *    may be used to endorse or promote products derived from this software
194a5d661aSToomas Soome  *    without specific prior written permission.
204a5d661aSToomas Soome  *
214a5d661aSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224a5d661aSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234a5d661aSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244a5d661aSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254a5d661aSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264a5d661aSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274a5d661aSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284a5d661aSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294a5d661aSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304a5d661aSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314a5d661aSToomas Soome  * SUCH DAMAGE.
324a5d661aSToomas Soome  *
334a5d661aSToomas Soome  *	@(#)quad.h	8.1 (Berkeley) 6/4/93
344a5d661aSToomas Soome  * $FreeBSD$
354a5d661aSToomas Soome  */
364a5d661aSToomas Soome 
374a5d661aSToomas Soome /*
384a5d661aSToomas Soome  * Quad arithmetic.
394a5d661aSToomas Soome  *
404a5d661aSToomas Soome  * This library makes the following assumptions:
414a5d661aSToomas Soome  *
424a5d661aSToomas Soome  *  - The type long long (aka quad_t) exists.
434a5d661aSToomas Soome  *
444a5d661aSToomas Soome  *  - A quad variable is exactly twice as long as `long'.
454a5d661aSToomas Soome  *
464a5d661aSToomas Soome  *  - The machine's arithmetic is two's complement.
474a5d661aSToomas Soome  *
484a5d661aSToomas Soome  * This library can provide 128-bit arithmetic on a machine with 128-bit
494a5d661aSToomas Soome  * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
504a5d661aSToomas Soome  * with 48-bit longs.
514a5d661aSToomas Soome  */
524a5d661aSToomas Soome 
534a5d661aSToomas Soome #include <sys/cdefs.h>
544a5d661aSToomas Soome #include <sys/types.h>
554a5d661aSToomas Soome #include <limits.h>
564a5d661aSToomas Soome 
574a5d661aSToomas Soome _Static_assert(sizeof(quad_t) == sizeof(int) * 2,
584a5d661aSToomas Soome 	"Bitwise function in libstand are broken on this architecture\n");
594a5d661aSToomas Soome 
604a5d661aSToomas Soome /*
614a5d661aSToomas Soome  * Depending on the desired operation, we view a `long long' (aka quad_t) in
624a5d661aSToomas Soome  * one or more of the following formats.
634a5d661aSToomas Soome  */
644a5d661aSToomas Soome union uu {
654a5d661aSToomas Soome 	quad_t	q;		/* as a (signed) quad */
664a5d661aSToomas Soome 	quad_t	uq;		/* as an unsigned quad */
674a5d661aSToomas Soome 	int	sl[2];		/* as two signed ints */
684a5d661aSToomas Soome 	u_int	ul[2];		/* as two unsigned ints */
694a5d661aSToomas Soome };
704a5d661aSToomas Soome 
714a5d661aSToomas Soome /*
724a5d661aSToomas Soome  * Define high and low longwords.
734a5d661aSToomas Soome  */
744a5d661aSToomas Soome #define	H		_QUAD_HIGHWORD
754a5d661aSToomas Soome #define	L		_QUAD_LOWWORD
764a5d661aSToomas Soome 
774a5d661aSToomas Soome /*
784a5d661aSToomas Soome  * Total number of bits in a quad_t and in the pieces that make it up.
794a5d661aSToomas Soome  * These are used for shifting, and also below for halfword extraction
804a5d661aSToomas Soome  * and assembly.
814a5d661aSToomas Soome  */
824a5d661aSToomas Soome #define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
834a5d661aSToomas Soome #define	HALF_BITS	(sizeof(int) * CHAR_BIT / 2)
844a5d661aSToomas Soome 
854a5d661aSToomas Soome /*
864a5d661aSToomas Soome  * Extract high and low shortwords from longword, and move low shortword of
874a5d661aSToomas Soome  * longword to upper half of long, i.e., produce the upper longword of
884a5d661aSToomas Soome  * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
894a5d661aSToomas Soome  *
904a5d661aSToomas Soome  * These are used in the multiply code, to split a longword into upper
914a5d661aSToomas Soome  * and lower halves, and to reassemble a product as a quad_t, shifted left
924a5d661aSToomas Soome  * (sizeof(long)*CHAR_BIT/2).
934a5d661aSToomas Soome  */
944a5d661aSToomas Soome #define	HHALF(x)	((x) >> HALF_BITS)
954a5d661aSToomas Soome #define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
964a5d661aSToomas Soome #define	LHUP(x)		((x) << HALF_BITS)
974a5d661aSToomas Soome 
984a5d661aSToomas Soome quad_t		__divdi3(quad_t a, quad_t b);
994a5d661aSToomas Soome quad_t		__moddi3(quad_t a, quad_t b);
100*331f34b7SToomas Soome u_quad_t	__udivmoddi4(u_quad_t u, u_quad_t v, u_quad_t *rem);
1014a5d661aSToomas Soome u_quad_t	__udivdi3(u_quad_t a, u_quad_t b);
1024a5d661aSToomas Soome u_quad_t	__umoddi3(u_quad_t a, u_quad_t b);
1034a5d661aSToomas Soome 
1044a5d661aSToomas Soome /*
1054a5d661aSToomas Soome  * XXX
1064a5d661aSToomas Soome  * Compensate for gcc 1 vs gcc 2.  Gcc 1 defines ?sh?di3's second argument
1074a5d661aSToomas Soome  * as u_quad_t, while gcc 2 correctly uses int.  Unfortunately, we still use
1084a5d661aSToomas Soome  * both compilers.
1094a5d661aSToomas Soome  */
1104a5d661aSToomas Soome #if __GNUC__ >= 2
1114a5d661aSToomas Soome typedef unsigned int	qshift_t;
1124a5d661aSToomas Soome #else
1134a5d661aSToomas Soome typedef u_quad_t	qshift_t;
1144a5d661aSToomas Soome #endif
115