quad.h revision 1541
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
381541Srgrimes */
391541Srgrimes
401541Srgrimes/*
411541Srgrimes * Quad arithmetic.
421541Srgrimes *
431541Srgrimes * This library makes the following assumptions:
441541Srgrimes *
451541Srgrimes *  - The type long long (aka quad_t) exists.
461541Srgrimes *
471541Srgrimes *  - A quad variable is exactly twice as long as `long'.
481541Srgrimes *
491541Srgrimes *  - The machine's arithmetic is two's complement.
501541Srgrimes *
511541Srgrimes * This library can provide 128-bit arithmetic on a machine with 128-bit
521541Srgrimes * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
531541Srgrimes * with 48-bit longs.
541541Srgrimes */
551541Srgrimes
561541Srgrimes#include <sys/types.h>
571541Srgrimes#include <limits.h>
581541Srgrimes
591541Srgrimes/*
601541Srgrimes * Depending on the desired operation, we view a `long long' (aka quad_t) in
611541Srgrimes * one or more of the following formats.
621541Srgrimes */
631541Srgrimesunion uu {
641541Srgrimes	quad_t	q;		/* as a (signed) quad */
651541Srgrimes	quad_t	uq;		/* as an unsigned quad */
661541Srgrimes	long	sl[2];		/* as two signed longs */
671541Srgrimes	u_long	ul[2];		/* as two unsigned longs */
681541Srgrimes};
691541Srgrimes
701541Srgrimes/*
711541Srgrimes * Define high and low longwords.
721541Srgrimes */
731541Srgrimes#define	H		_QUAD_HIGHWORD
741541Srgrimes#define	L		_QUAD_LOWWORD
751541Srgrimes
761541Srgrimes/*
771541Srgrimes * Total number of bits in a quad_t and in the pieces that make it up.
781541Srgrimes * These are used for shifting, and also below for halfword extraction
791541Srgrimes * and assembly.
801541Srgrimes */
811541Srgrimes#define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
821541Srgrimes#define	LONG_BITS	(sizeof(long) * CHAR_BIT)
831541Srgrimes#define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
841541Srgrimes
851541Srgrimes/*
861541Srgrimes * Extract high and low shortwords from longword, and move low shortword of
871541Srgrimes * longword to upper half of long, i.e., produce the upper longword of
881541Srgrimes * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
891541Srgrimes *
901541Srgrimes * These are used in the multiply code, to split a longword into upper
911541Srgrimes * and lower halves, and to reassemble a product as a quad_t, shifted left
921541Srgrimes * (sizeof(long)*CHAR_BIT/2).
931541Srgrimes */
941541Srgrimes#define	HHALF(x)	((x) >> HALF_BITS)
951541Srgrimes#define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
961541Srgrimes#define	LHUP(x)		((x) << HALF_BITS)
971541Srgrimes
981541Srgrimesextern u_quad_t __qdivrem __P((u_quad_t u, u_quad_t v, u_quad_t *rem));
991541Srgrimes
1001541Srgrimes/*
1011541Srgrimes * XXX
1021541Srgrimes * Compensate for gcc 1 vs gcc 2.  Gcc 1 defines ?sh?di3's second argument
1031541Srgrimes * as u_quad_t, while gcc 2 correctly uses int.  Unfortunately, we still use
1041541Srgrimes * both compilers.
1051541Srgrimes */
1061541Srgrimes#if __GNUC__ >= 2
1071541Srgrimestypedef unsigned int	qshift_t;
1081541Srgrimes#else
1091541Srgrimestypedef u_quad_t	qshift_t;
1101541Srgrimes#endif
111