quad.h revision 50477
190792Sgshapiro/*-
2261363Sgshapiro * Copyright (c) 1992, 1993
390792Sgshapiro *	The Regents of the University of California.  All rights reserved.
490792Sgshapiro *
590792Sgshapiro * This software was developed by the Computer Systems Engineering group
690792Sgshapiro * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
790792Sgshapiro * contributed to Berkeley.
890792Sgshapiro *
990792Sgshapiro * Redistribution and use in source and binary forms, with or without
1090792Sgshapiro * modification, are permitted provided that the following conditions
1190792Sgshapiro * are met:
1290792Sgshapiro * 1. Redistributions of source code must retain the above copyright
1390792Sgshapiro *    notice, this list of conditions and the following disclaimer.
1490792Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
15266692Sgshapiro *    notice, this list of conditions and the following disclaimer in the
1690792Sgshapiro *    documentation and/or other materials provided with the distribution.
1790792Sgshapiro * 3. All advertising materials mentioning features or use of this software
1890792Sgshapiro *    must display the following acknowledgement:
1990792Sgshapiro *	This product includes software developed by the University of
20157001Sgshapiro *	California, Berkeley and its contributors.
2190792Sgshapiro * 4. Neither the name of the University nor the names of its contributors
2290792Sgshapiro *    may be used to endorse or promote products derived from this software
2390792Sgshapiro *    without specific prior written permission.
2490792Sgshapiro *
2590792Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2690792Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27120256Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28120256Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29120256Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30120256Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31120256Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32120256Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3390792Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3490792Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3590792Sgshapiro * SUCH DAMAGE.
3690792Sgshapiro *
3790792Sgshapiro *	@(#)quad.h	8.1 (Berkeley) 6/4/93
3890792Sgshapiro * $FreeBSD: head/sys/libkern/quad.h 50477 1999-08-28 01:08:13Z peter $
3990792Sgshapiro */
4090792Sgshapiro
4190792Sgshapiro/*
4290792Sgshapiro * Quad arithmetic.
4390792Sgshapiro *
4490792Sgshapiro * This library makes the following assumptions:
4590792Sgshapiro *
4690792Sgshapiro *  - The type long long (aka quad_t) exists.
4790792Sgshapiro *
4890792Sgshapiro *  - A quad variable is exactly twice as long as `long'.
4990792Sgshapiro *
5090792Sgshapiro *  - The machine's arithmetic is two's complement.
5190792Sgshapiro *
5290792Sgshapiro * This library can provide 128-bit arithmetic on a machine with 128-bit
5390792Sgshapiro * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
5490792Sgshapiro * with 48-bit longs.
5590792Sgshapiro */
5690792Sgshapiro
5790792Sgshapiro#include <sys/cdefs.h>
5890792Sgshapiro#include <sys/types.h>
5990792Sgshapiro#include <limits.h>
6090792Sgshapiro
6190792Sgshapiro/*
6290792Sgshapiro * Depending on the desired operation, we view a `long long' (aka quad_t) in
6390792Sgshapiro * one or more of the following formats.
6490792Sgshapiro */
6590792Sgshapirounion uu {
66141858Sgshapiro	quad_t	q;		/* as a (signed) quad */
6790792Sgshapiro	quad_t	uq;		/* as an unsigned quad */
6890792Sgshapiro	long	sl[2];		/* as two signed longs */
6990792Sgshapiro	u_long	ul[2];		/* as two unsigned longs */
7090792Sgshapiro};
7190792Sgshapiro
7290792Sgshapiro/*
7390792Sgshapiro * Define high and low longwords.
7490792Sgshapiro */
7590792Sgshapiro#define	H		_QUAD_HIGHWORD
7690792Sgshapiro#define	L		_QUAD_LOWWORD
7790792Sgshapiro
7890792Sgshapiro/*
7990792Sgshapiro * Total number of bits in a quad_t and in the pieces that make it up.
8090792Sgshapiro * These are used for shifting, and also below for halfword extraction
8190792Sgshapiro * and assembly.
8290792Sgshapiro */
8390792Sgshapiro#define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
8490792Sgshapiro#define	LONG_BITS	(sizeof(long) * CHAR_BIT)
8590792Sgshapiro#define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
8690792Sgshapiro
8790792Sgshapiro/*
8890792Sgshapiro * Extract high and low shortwords from longword, and move low shortword of
89141858Sgshapiro * longword to upper half of long, i.e., produce the upper longword of
9090792Sgshapiro * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
9190792Sgshapiro *
9290792Sgshapiro * These are used in the multiply code, to split a longword into upper
9390792Sgshapiro * and lower halves, and to reassemble a product as a quad_t, shifted left
9490792Sgshapiro * (sizeof(long)*CHAR_BIT/2).
9590792Sgshapiro */
9690792Sgshapiro#define	HHALF(x)	((x) >> HALF_BITS)
9790792Sgshapiro#define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
9890792Sgshapiro#define	LHUP(x)		((x) << HALF_BITS)
9990792Sgshapiro
10090792Sgshapiroquad_t		__divdi3 __P((quad_t a, quad_t b));
10190792Sgshapiroquad_t		__moddi3 __P((quad_t a, quad_t b));
10290792Sgshapirou_quad_t	__qdivrem __P((u_quad_t u, u_quad_t v, u_quad_t *rem));
10390792Sgshapirou_quad_t	__udivdi3 __P((u_quad_t a, u_quad_t b));
10490792Sgshapirou_quad_t	__umoddi3 __P((u_quad_t a, u_quad_t b));
10590792Sgshapiro
10690792Sgshapiro/*
10790792Sgshapiro * XXX
10890792Sgshapiro * Compensate for gcc 1 vs gcc 2.  Gcc 1 defines ?sh?di3's second argument
10990792Sgshapiro * as u_quad_t, while gcc 2 correctly uses int.  Unfortunately, we still use
11090792Sgshapiro * both compilers.
11190792Sgshapiro */
11290792Sgshapiro#if __GNUC__ >= 2
11390792Sgshapirotypedef unsigned int	qshift_t;
11490792Sgshapiro#else
11590792Sgshapirotypedef u_quad_t	qshift_t;
11690792Sgshapiro#endif
11790792Sgshapiro