140874Smsmith/*-
240874Smsmith * Copyright (c) 1992, 1993
340874Smsmith *	The Regents of the University of California.  All rights reserved.
440874Smsmith *
540874Smsmith * This software was developed by the Computer Systems Engineering group
640874Smsmith * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
740874Smsmith * contributed to Berkeley.
840874Smsmith *
940874Smsmith * Redistribution and use in source and binary forms, with or without
1040874Smsmith * modification, are permitted provided that the following conditions
1140874Smsmith * are met:
1240874Smsmith * 1. Redistributions of source code must retain the above copyright
1340874Smsmith *    notice, this list of conditions and the following disclaimer.
1440874Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1540874Smsmith *    notice, this list of conditions and the following disclaimer in the
1640874Smsmith *    documentation and/or other materials provided with the distribution.
1740874Smsmith * 4. Neither the name of the University nor the names of its contributors
1840874Smsmith *    may be used to endorse or promote products derived from this software
1940874Smsmith *    without specific prior written permission.
2040874Smsmith *
2140874Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2240874Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2340874Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2440874Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2540874Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2640874Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2740874Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2840874Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2940874Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3040874Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3140874Smsmith * SUCH DAMAGE.
3240874Smsmith *
3340874Smsmith *	@(#)quad.h	8.1 (Berkeley) 6/4/93
3450476Speter * $FreeBSD$
3540874Smsmith */
3640874Smsmith
3740874Smsmith/*
3840874Smsmith * Quad arithmetic.
3940874Smsmith *
4040874Smsmith * This library makes the following assumptions:
4140874Smsmith *
4240874Smsmith *  - The type long long (aka quad_t) exists.
4340874Smsmith *
4440874Smsmith *  - A quad variable is exactly twice as long as `long'.
4540874Smsmith *
4640874Smsmith *  - The machine's arithmetic is two's complement.
4740874Smsmith *
4840874Smsmith * This library can provide 128-bit arithmetic on a machine with 128-bit
4940874Smsmith * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
5040874Smsmith * with 48-bit longs.
5140874Smsmith */
5240874Smsmith
5340874Smsmith#include <sys/cdefs.h>
5440874Smsmith#include <sys/types.h>
5540874Smsmith#include <limits.h>
5640874Smsmith
57271134Semaste_Static_assert(sizeof(quad_t) == sizeof(int) * 2,
58271134Semaste	"Bitwise function in libstand are broken on this architecture\n");
59271134Semaste
6040874Smsmith/*
6140874Smsmith * Depending on the desired operation, we view a `long long' (aka quad_t) in
6240874Smsmith * one or more of the following formats.
6340874Smsmith */
6440874Smsmithunion uu {
6540874Smsmith	quad_t	q;		/* as a (signed) quad */
6640874Smsmith	quad_t	uq;		/* as an unsigned quad */
67271134Semaste	int	sl[2];		/* as two signed ints */
68271134Semaste	u_int	ul[2];		/* as two unsigned ints */
6940874Smsmith};
7040874Smsmith
7140874Smsmith/*
7240874Smsmith * Define high and low longwords.
7340874Smsmith */
7440874Smsmith#define	H		_QUAD_HIGHWORD
7540874Smsmith#define	L		_QUAD_LOWWORD
7640874Smsmith
7740874Smsmith/*
7840874Smsmith * Total number of bits in a quad_t and in the pieces that make it up.
7940874Smsmith * These are used for shifting, and also below for halfword extraction
8040874Smsmith * and assembly.
8140874Smsmith */
8240874Smsmith#define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
83271134Semaste#define	HALF_BITS	(sizeof(int) * CHAR_BIT / 2)
8440874Smsmith
8540874Smsmith/*
8640874Smsmith * Extract high and low shortwords from longword, and move low shortword of
8740874Smsmith * longword to upper half of long, i.e., produce the upper longword of
8840874Smsmith * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
8940874Smsmith *
9040874Smsmith * These are used in the multiply code, to split a longword into upper
9140874Smsmith * and lower halves, and to reassemble a product as a quad_t, shifted left
9240874Smsmith * (sizeof(long)*CHAR_BIT/2).
9340874Smsmith */
9440874Smsmith#define	HHALF(x)	((x) >> HALF_BITS)
9540874Smsmith#define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
9640874Smsmith#define	LHUP(x)		((x) << HALF_BITS)
9740874Smsmith
9892917Sobrienquad_t		__divdi3(quad_t a, quad_t b);
9992917Sobrienquad_t		__moddi3(quad_t a, quad_t b);
10092917Sobrienu_quad_t	__qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem);
10192917Sobrienu_quad_t	__udivdi3(u_quad_t a, u_quad_t b);
10292917Sobrienu_quad_t	__umoddi3(u_quad_t a, u_quad_t b);
10340874Smsmith
10440874Smsmith/*
10540874Smsmith * XXX
10640874Smsmith * Compensate for gcc 1 vs gcc 2.  Gcc 1 defines ?sh?di3's second argument
10740874Smsmith * as u_quad_t, while gcc 2 correctly uses int.  Unfortunately, we still use
10840874Smsmith * both compilers.
10940874Smsmith */
11040874Smsmith#if __GNUC__ >= 2
11140874Smsmithtypedef unsigned int	qshift_t;
11240874Smsmith#else
11340874Smsmithtypedef u_quad_t	qshift_t;
11440874Smsmith#endif
115