xref: /freebsd/sys/libkern/quad.h (revision cc4f247fb9468bbece5286557ab2d6810704aaf3)
1df8bae1dSRodney W. Grimes /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1992, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This software was developed by the Computer Systems Engineering group
8df8bae1dSRodney W. Grimes  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9df8bae1dSRodney W. Grimes  * contributed to Berkeley.
10df8bae1dSRodney W. Grimes  *
11df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
12df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
13df8bae1dSRodney W. Grimes  * are met:
14df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
16df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
18df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
20df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
21df8bae1dSRodney W. Grimes  *    without specific prior written permission.
22df8bae1dSRodney W. Grimes  *
23df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  *	@(#)quad.h	8.1 (Berkeley) 6/4/93
36c3aac50fSPeter Wemm  * $FreeBSD$
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39c7e459edSBruce Evans #ifndef _LIBKERN_QUAD_H_
40c7e459edSBruce Evans #define	_LIBKERN_QUAD_H_
41943e5db9SPoul-Henning Kamp 
42df8bae1dSRodney W. Grimes /*
43df8bae1dSRodney W. Grimes  * Quad arithmetic.
44df8bae1dSRodney W. Grimes  *
45df8bae1dSRodney W. Grimes  * This library makes the following assumptions:
46df8bae1dSRodney W. Grimes  *
47df8bae1dSRodney W. Grimes  *  - The type long long (aka quad_t) exists.
48df8bae1dSRodney W. Grimes  *
49df8bae1dSRodney W. Grimes  *  - A quad variable is exactly twice as long as `long'.
50df8bae1dSRodney W. Grimes  *
51df8bae1dSRodney W. Grimes  *  - The machine's arithmetic is two's complement.
52df8bae1dSRodney W. Grimes  *
53df8bae1dSRodney W. Grimes  * This library can provide 128-bit arithmetic on a machine with 128-bit
54df8bae1dSRodney W. Grimes  * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
55df8bae1dSRodney W. Grimes  * with 48-bit longs.
56df8bae1dSRodney W. Grimes  */
57df8bae1dSRodney W. Grimes 
5887b620baSBruce Evans #include <sys/cdefs.h>
59df8bae1dSRodney W. Grimes #include <sys/types.h>
60104a9b7eSAlexander Kabaev #include <sys/limits.h>
610b1ae809SJulian Elischer #include <sys/syslimits.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes /*
64df8bae1dSRodney W. Grimes  * Depending on the desired operation, we view a `long long' (aka quad_t) in
65df8bae1dSRodney W. Grimes  * one or more of the following formats.
66df8bae1dSRodney W. Grimes  */
67df8bae1dSRodney W. Grimes union uu {
68df8bae1dSRodney W. Grimes 	quad_t	q;		/* as a (signed) quad */
69df8bae1dSRodney W. Grimes 	quad_t	uq;		/* as an unsigned quad */
70df8bae1dSRodney W. Grimes 	long	sl[2];		/* as two signed longs */
71df8bae1dSRodney W. Grimes 	u_long	ul[2];		/* as two unsigned longs */
72df8bae1dSRodney W. Grimes };
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes /*
75df8bae1dSRodney W. Grimes  * Define high and low longwords.
76df8bae1dSRodney W. Grimes  */
77df8bae1dSRodney W. Grimes #define	H		_QUAD_HIGHWORD
78df8bae1dSRodney W. Grimes #define	L		_QUAD_LOWWORD
79df8bae1dSRodney W. Grimes 
80df8bae1dSRodney W. Grimes /*
81df8bae1dSRodney W. Grimes  * Total number of bits in a quad_t and in the pieces that make it up.
82df8bae1dSRodney W. Grimes  * These are used for shifting, and also below for halfword extraction
83df8bae1dSRodney W. Grimes  * and assembly.
84df8bae1dSRodney W. Grimes  */
85df8bae1dSRodney W. Grimes #define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
86df8bae1dSRodney W. Grimes #define	LONG_BITS	(sizeof(long) * CHAR_BIT)
87df8bae1dSRodney W. Grimes #define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
88df8bae1dSRodney W. Grimes 
89df8bae1dSRodney W. Grimes /*
90df8bae1dSRodney W. Grimes  * Extract high and low shortwords from longword, and move low shortword of
91df8bae1dSRodney W. Grimes  * longword to upper half of long, i.e., produce the upper longword of
92df8bae1dSRodney W. Grimes  * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
93df8bae1dSRodney W. Grimes  *
94df8bae1dSRodney W. Grimes  * These are used in the multiply code, to split a longword into upper
95df8bae1dSRodney W. Grimes  * and lower halves, and to reassemble a product as a quad_t, shifted left
96df8bae1dSRodney W. Grimes  * (sizeof(long)*CHAR_BIT/2).
97df8bae1dSRodney W. Grimes  */
98df8bae1dSRodney W. Grimes #define	HHALF(x)	((x) >> HALF_BITS)
99df8bae1dSRodney W. Grimes #define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
100df8bae1dSRodney W. Grimes #define	LHUP(x)		((x) << HALF_BITS)
101df8bae1dSRodney W. Grimes 
102c7e459edSBruce Evans typedef unsigned int	qshift_t;
103c7e459edSBruce Evans 
104c7e459edSBruce Evans quad_t		__ashldi3(quad_t, qshift_t);
105c7e459edSBruce Evans quad_t		__ashrdi3(quad_t, qshift_t);
106c2602b5bSMarcel Moolenaar int		__cmpdi2(quad_t a, quad_t b);
1078febc6baSAlfred Perlstein quad_t		__divdi3(quad_t a, quad_t b);
108*cc4f247fSJohn Baldwin quad_t		__divmoddi4(quad_t a, quad_t b, quad_t *rem);
109c7e459edSBruce Evans quad_t		__lshrdi3(quad_t, qshift_t);
1108febc6baSAlfred Perlstein quad_t		__moddi3(quad_t a, quad_t b);
1118febc6baSAlfred Perlstein u_quad_t	__qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem);
1128febc6baSAlfred Perlstein u_quad_t	__udivdi3(u_quad_t a, u_quad_t b);
113*cc4f247fSJohn Baldwin u_quad_t	__udivmoddi4(u_quad_t a, u_quad_t b, u_quad_t *rem);
1148febc6baSAlfred Perlstein u_quad_t	__umoddi3(u_quad_t a, u_quad_t b);
1158febc6baSAlfred Perlstein int		__ucmpdi2(u_quad_t a, u_quad_t b);
116df8bae1dSRodney W. Grimes 
11731735bf3SAndrew Turner /* ARM EABI support functions. */
11831735bf3SAndrew Turner #ifdef __ARM_EABI__
11931735bf3SAndrew Turner int		__aeabi_ulcmp(unsigned long long, unsigned long long);
12031735bf3SAndrew Turner #endif
12131735bf3SAndrew Turner 
122c7e459edSBruce Evans #endif /* !_LIBKERN_QUAD_H_ */
123