quad.h revision 114216
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
3850477Speter * $FreeBSD: head/sys/libkern/quad.h 114216 2003-04-29 13:36:06Z kan $
391541Srgrimes */
401541Srgrimes
41104667Sphk#ifndef _LIBKERN_QUAD_H
42104667Sphk#define _LIBKERN_QUAD_H
43104667Sphk
441541Srgrimes/*
451541Srgrimes * Quad arithmetic.
461541Srgrimes *
471541Srgrimes * This library makes the following assumptions:
481541Srgrimes *
491541Srgrimes *  - The type long long (aka quad_t) exists.
501541Srgrimes *
511541Srgrimes *  - A quad variable is exactly twice as long as `long'.
521541Srgrimes *
531541Srgrimes *  - The machine's arithmetic is two's complement.
541541Srgrimes *
551541Srgrimes * This library can provide 128-bit arithmetic on a machine with 128-bit
561541Srgrimes * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
571541Srgrimes * with 48-bit longs.
581541Srgrimes */
591541Srgrimes
6015311Sbde#include <sys/cdefs.h>
611541Srgrimes#include <sys/types.h>
62114216Skan#include <sys/limits.h>
6379418Sjulian#include <sys/syslimits.h>
641541Srgrimes
651541Srgrimes/*
661541Srgrimes * Depending on the desired operation, we view a `long long' (aka quad_t) in
671541Srgrimes * one or more of the following formats.
681541Srgrimes */
691541Srgrimesunion uu {
701541Srgrimes	quad_t	q;		/* as a (signed) quad */
711541Srgrimes	quad_t	uq;		/* as an unsigned quad */
721541Srgrimes	long	sl[2];		/* as two signed longs */
731541Srgrimes	u_long	ul[2];		/* as two unsigned longs */
741541Srgrimes};
751541Srgrimes
761541Srgrimes/*
771541Srgrimes * Define high and low longwords.
781541Srgrimes */
791541Srgrimes#define	H		_QUAD_HIGHWORD
801541Srgrimes#define	L		_QUAD_LOWWORD
811541Srgrimes
821541Srgrimes/*
831541Srgrimes * Total number of bits in a quad_t and in the pieces that make it up.
841541Srgrimes * These are used for shifting, and also below for halfword extraction
851541Srgrimes * and assembly.
861541Srgrimes */
871541Srgrimes#define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
881541Srgrimes#define	LONG_BITS	(sizeof(long) * CHAR_BIT)
891541Srgrimes#define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
901541Srgrimes
911541Srgrimes/*
921541Srgrimes * Extract high and low shortwords from longword, and move low shortword of
931541Srgrimes * longword to upper half of long, i.e., produce the upper longword of
941541Srgrimes * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
951541Srgrimes *
961541Srgrimes * These are used in the multiply code, to split a longword into upper
971541Srgrimes * and lower halves, and to reassemble a product as a quad_t, shifted left
981541Srgrimes * (sizeof(long)*CHAR_BIT/2).
991541Srgrimes */
1001541Srgrimes#define	HHALF(x)	((x) >> HALF_BITS)
1011541Srgrimes#define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
1021541Srgrimes#define	LHUP(x)		((x) << HALF_BITS)
1031541Srgrimes
10492741Salfredquad_t		__divdi3(quad_t a, quad_t b);
10592741Salfredquad_t		__moddi3(quad_t a, quad_t b);
10692741Salfredu_quad_t	__qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem);
10792741Salfredu_quad_t	__udivdi3(u_quad_t a, u_quad_t b);
10892741Salfredu_quad_t	__umoddi3(u_quad_t a, u_quad_t b);
10992741Salfredint		__ucmpdi2(u_quad_t a, u_quad_t b);
1101541Srgrimes
1111541Srgrimes/*
1121541Srgrimes * XXX
1131541Srgrimes * Compensate for gcc 1 vs gcc 2.  Gcc 1 defines ?sh?di3's second argument
1141541Srgrimes * as u_quad_t, while gcc 2 correctly uses int.  Unfortunately, we still use
1151541Srgrimes * both compilers.
1161541Srgrimes */
1171541Srgrimes#if __GNUC__ >= 2
1181541Srgrimestypedef unsigned int	qshift_t;
1191541Srgrimes#else
1201541Srgrimestypedef u_quad_t	qshift_t;
1211541Srgrimes#endif
122104667Sphk#endif /* _LIBKERN_QUAD_H */
123