quad.h revision 15311
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1992, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This software was developed by the Computer Systems Engineering group
61541Srgrimes * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
71541Srgrimes * contributed to Berkeley.
81541Srgrimes *
91541Srgrimes * Redistribution and use in source and binary forms, with or without
101541Srgrimes * modification, are permitted provided that the following conditions
111541Srgrimes * are met:
121541Srgrimes * 1. Redistributions of source code must retain the above copyright
131541Srgrimes *    notice, this list of conditions and the following disclaimer.
141541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer in the
161541Srgrimes *    documentation and/or other materials provided with the distribution.
171541Srgrimes * 3. All advertising materials mentioning features or use of this software
181541Srgrimes *    must display the following acknowledgement:
191541Srgrimes *	This product includes software developed by the University of
201541Srgrimes *	California, Berkeley and its contributors.
211541Srgrimes * 4. Neither the name of the University nor the names of its contributors
221541Srgrimes *    may be used to endorse or promote products derived from this software
231541Srgrimes *    without specific prior written permission.
241541Srgrimes *
251541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351541Srgrimes * SUCH DAMAGE.
361541Srgrimes *
371541Srgrimes *	@(#)quad.h	8.1 (Berkeley) 6/4/93
3815311Sbde *	$Id: quad.h,v 1.3 1995/12/26 13:24:48 bde Exp $
391541Srgrimes */
401541Srgrimes
411541Srgrimes/*
421541Srgrimes * Quad arithmetic.
431541Srgrimes *
441541Srgrimes * This library makes the following assumptions:
451541Srgrimes *
461541Srgrimes *  - The type long long (aka quad_t) exists.
471541Srgrimes *
481541Srgrimes *  - A quad variable is exactly twice as long as `long'.
491541Srgrimes *
501541Srgrimes *  - The machine's arithmetic is two's complement.
511541Srgrimes *
521541Srgrimes * This library can provide 128-bit arithmetic on a machine with 128-bit
531541Srgrimes * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
541541Srgrimes * with 48-bit longs.
551541Srgrimes */
561541Srgrimes
5715311Sbde#include <sys/cdefs.h>
581541Srgrimes#include <sys/types.h>
591541Srgrimes#include <limits.h>
601541Srgrimes
611541Srgrimes/*
621541Srgrimes * Depending on the desired operation, we view a `long long' (aka quad_t) in
631541Srgrimes * one or more of the following formats.
641541Srgrimes */
651541Srgrimesunion uu {
661541Srgrimes	quad_t	q;		/* as a (signed) quad */
671541Srgrimes	quad_t	uq;		/* as an unsigned quad */
681541Srgrimes	long	sl[2];		/* as two signed longs */
691541Srgrimes	u_long	ul[2];		/* as two unsigned longs */
701541Srgrimes};
711541Srgrimes
721541Srgrimes/*
731541Srgrimes * Define high and low longwords.
741541Srgrimes */
751541Srgrimes#define	H		_QUAD_HIGHWORD
761541Srgrimes#define	L		_QUAD_LOWWORD
771541Srgrimes
781541Srgrimes/*
791541Srgrimes * Total number of bits in a quad_t and in the pieces that make it up.
801541Srgrimes * These are used for shifting, and also below for halfword extraction
811541Srgrimes * and assembly.
821541Srgrimes */
831541Srgrimes#define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
841541Srgrimes#define	LONG_BITS	(sizeof(long) * CHAR_BIT)
851541Srgrimes#define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
861541Srgrimes
871541Srgrimes/*
881541Srgrimes * Extract high and low shortwords from longword, and move low shortword of
891541Srgrimes * longword to upper half of long, i.e., produce the upper longword of
901541Srgrimes * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
911541Srgrimes *
921541Srgrimes * These are used in the multiply code, to split a longword into upper
931541Srgrimes * and lower halves, and to reassemble a product as a quad_t, shifted left
941541Srgrimes * (sizeof(long)*CHAR_BIT/2).
951541Srgrimes */
961541Srgrimes#define	HHALF(x)	((x) >> HALF_BITS)
971541Srgrimes#define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
981541Srgrimes#define	LHUP(x)		((x) << HALF_BITS)
991541Srgrimes
10013030Sbdequad_t		__divdi3 __P((quad_t a, quad_t b));
10113030Sbdequad_t		__moddi3 __P((quad_t a, quad_t b));
10213030Sbdeu_quad_t	__qdivrem __P((u_quad_t u, u_quad_t v, u_quad_t *rem));
10313030Sbdeu_quad_t	__udivdi3 __P((u_quad_t a, u_quad_t b));
10413030Sbdeu_quad_t	__umoddi3 __P((u_quad_t a, u_quad_t b));
1051541Srgrimes
1061541Srgrimes/*
1071541Srgrimes * XXX
1081541Srgrimes * Compensate for gcc 1 vs gcc 2.  Gcc 1 defines ?sh?di3's second argument
1091541Srgrimes * as u_quad_t, while gcc 2 correctly uses int.  Unfortunately, we still use
1101541Srgrimes * both compilers.
1111541Srgrimes */
1121541Srgrimes#if __GNUC__ >= 2
1131541Srgrimestypedef unsigned int	qshift_t;
1141541Srgrimes#else
1151541Srgrimestypedef u_quad_t	qshift_t;
1161541Srgrimes#endif
117