quad.h revision 128019
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 1992, 1993
3219820Sjeff *	The Regents of the University of California.  All rights reserved.
4219820Sjeff *
5219820Sjeff * This software was developed by the Computer Systems Engineering group
6219820Sjeff * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7219820Sjeff * contributed to Berkeley.
8219820Sjeff *
9219820Sjeff * Redistribution and use in source and binary forms, with or without
10219820Sjeff * modification, are permitted provided that the following conditions
11219820Sjeff * are met:
12219820Sjeff * 1. Redistributions of source code must retain the above copyright
13219820Sjeff *    notice, this list of conditions and the following disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff * 4. Neither the name of the University nor the names of its contributors
18219820Sjeff *    may be used to endorse or promote products derived from this software
19219820Sjeff *    without specific prior written permission.
20219820Sjeff *
21219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24219820Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31219820Sjeff * SUCH DAMAGE.
32219820Sjeff *
33219820Sjeff *	@(#)quad.h	8.1 (Berkeley) 6/4/93
34219820Sjeff * $FreeBSD: head/sys/libkern/quad.h 128019 2004-04-07 20:46:16Z imp $
35219820Sjeff */
36219820Sjeff
37219820Sjeff#ifndef _LIBKERN_QUAD_H
38219820Sjeff#define _LIBKERN_QUAD_H
39219820Sjeff
40219820Sjeff/*
41219820Sjeff * Quad arithmetic.
42219820Sjeff *
43219820Sjeff * This library makes the following assumptions:
44219820Sjeff *
45219820Sjeff *  - The type long long (aka quad_t) exists.
46219820Sjeff *
47219820Sjeff *  - A quad variable is exactly twice as long as `long'.
48219820Sjeff *
49219820Sjeff *  - The machine's arithmetic is two's complement.
50219820Sjeff *
51219820Sjeff * This library can provide 128-bit arithmetic on a machine with 128-bit
52219820Sjeff * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
53219820Sjeff * with 48-bit longs.
54219820Sjeff */
55219820Sjeff
56219820Sjeff#include <sys/cdefs.h>
57219820Sjeff#include <sys/types.h>
58219820Sjeff#include <sys/limits.h>
59219820Sjeff#include <sys/syslimits.h>
60219820Sjeff
61219820Sjeff/*
62219820Sjeff * Depending on the desired operation, we view a `long long' (aka quad_t) in
63219820Sjeff * one or more of the following formats.
64219820Sjeff */
65219820Sjeffunion uu {
66219820Sjeff	quad_t	q;		/* as a (signed) quad */
67219820Sjeff	quad_t	uq;		/* as an unsigned quad */
68219820Sjeff	long	sl[2];		/* as two signed longs */
69219820Sjeff	u_long	ul[2];		/* as two unsigned longs */
70219820Sjeff};
71219820Sjeff
72219820Sjeff/*
73219820Sjeff * Define high and low longwords.
74219820Sjeff */
75219820Sjeff#define	H		_QUAD_HIGHWORD
76219820Sjeff#define	L		_QUAD_LOWWORD
77219820Sjeff
78219820Sjeff/*
79219820Sjeff * Total number of bits in a quad_t and in the pieces that make it up.
80219820Sjeff * These are used for shifting, and also below for halfword extraction
81219820Sjeff * and assembly.
82219820Sjeff */
83219820Sjeff#define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
84219820Sjeff#define	LONG_BITS	(sizeof(long) * CHAR_BIT)
85219820Sjeff#define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
86219820Sjeff
87219820Sjeff/*
88219820Sjeff * Extract high and low shortwords from longword, and move low shortword of
89219820Sjeff * longword to upper half of long, i.e., produce the upper longword of
90219820Sjeff * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
91219820Sjeff *
92219820Sjeff * These are used in the multiply code, to split a longword into upper
93219820Sjeff * and lower halves, and to reassemble a product as a quad_t, shifted left
94219820Sjeff * (sizeof(long)*CHAR_BIT/2).
95219820Sjeff */
96219820Sjeff#define	HHALF(x)	((x) >> HALF_BITS)
97219820Sjeff#define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
98219820Sjeff#define	LHUP(x)		((x) << HALF_BITS)
99219820Sjeff
100219820Sjeffquad_t		__divdi3(quad_t a, quad_t b);
101219820Sjeffquad_t		__moddi3(quad_t a, quad_t b);
102219820Sjeffu_quad_t	__qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem);
103219820Sjeffu_quad_t	__udivdi3(u_quad_t a, u_quad_t b);
104219820Sjeffu_quad_t	__umoddi3(u_quad_t a, u_quad_t b);
105219820Sjeffint		__ucmpdi2(u_quad_t a, u_quad_t b);
106219820Sjeff
107219820Sjefftypedef unsigned int	qshift_t;
108219820Sjeff#endif /* _LIBKERN_QUAD_H */
109219820Sjeff