xref: /freebsd/sys/libkern/quad.h (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
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 
36c7e459edSBruce Evans #ifndef _LIBKERN_QUAD_H_
37c7e459edSBruce Evans #define	_LIBKERN_QUAD_H_
38943e5db9SPoul-Henning Kamp 
39df8bae1dSRodney W. Grimes /*
40df8bae1dSRodney W. Grimes  * Quad arithmetic.
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * This library makes the following assumptions:
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  *  - The type long long (aka quad_t) exists.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  *  - A quad variable is exactly twice as long as `long'.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  *  - The machine's arithmetic is two's complement.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * This library can provide 128-bit arithmetic on a machine with 128-bit
51df8bae1dSRodney W. Grimes  * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
52df8bae1dSRodney W. Grimes  * with 48-bit longs.
53df8bae1dSRodney W. Grimes  */
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <sys/types.h>
56104a9b7eSAlexander Kabaev #include <sys/limits.h>
570b1ae809SJulian Elischer #include <sys/syslimits.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes /*
60df8bae1dSRodney W. Grimes  * Depending on the desired operation, we view a `long long' (aka quad_t) in
61df8bae1dSRodney W. Grimes  * one or more of the following formats.
62df8bae1dSRodney W. Grimes  */
63df8bae1dSRodney W. Grimes union uu {
64df8bae1dSRodney W. Grimes 	quad_t	q;		/* as a (signed) quad */
65df8bae1dSRodney W. Grimes 	quad_t	uq;		/* as an unsigned quad */
66df8bae1dSRodney W. Grimes 	long	sl[2];		/* as two signed longs */
67df8bae1dSRodney W. Grimes 	u_long	ul[2];		/* as two unsigned longs */
68df8bae1dSRodney W. Grimes };
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  * Define high and low longwords.
72df8bae1dSRodney W. Grimes  */
73df8bae1dSRodney W. Grimes #define	H		_QUAD_HIGHWORD
74df8bae1dSRodney W. Grimes #define	L		_QUAD_LOWWORD
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes /*
77df8bae1dSRodney W. Grimes  * Total number of bits in a quad_t and in the pieces that make it up.
78df8bae1dSRodney W. Grimes  * These are used for shifting, and also below for halfword extraction
79df8bae1dSRodney W. Grimes  * and assembly.
80df8bae1dSRodney W. Grimes  */
81df8bae1dSRodney W. Grimes #define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
82df8bae1dSRodney W. Grimes #define	LONG_BITS	(sizeof(long) * CHAR_BIT)
83df8bae1dSRodney W. Grimes #define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes /*
86df8bae1dSRodney W. Grimes  * Extract high and low shortwords from longword, and move low shortword of
87df8bae1dSRodney W. Grimes  * longword to upper half of long, i.e., produce the upper longword of
88df8bae1dSRodney W. Grimes  * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
89df8bae1dSRodney W. Grimes  *
90df8bae1dSRodney W. Grimes  * These are used in the multiply code, to split a longword into upper
91df8bae1dSRodney W. Grimes  * and lower halves, and to reassemble a product as a quad_t, shifted left
92df8bae1dSRodney W. Grimes  * (sizeof(long)*CHAR_BIT/2).
93df8bae1dSRodney W. Grimes  */
94df8bae1dSRodney W. Grimes #define	HHALF(x)	((x) >> HALF_BITS)
95df8bae1dSRodney W. Grimes #define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
96df8bae1dSRodney W. Grimes #define	LHUP(x)		((x) << HALF_BITS)
97df8bae1dSRodney W. Grimes 
98c7e459edSBruce Evans typedef unsigned int	qshift_t;
99c7e459edSBruce Evans 
100c7e459edSBruce Evans quad_t		__ashldi3(quad_t, qshift_t);
101c7e459edSBruce Evans quad_t		__ashrdi3(quad_t, qshift_t);
102c2602b5bSMarcel Moolenaar int		__cmpdi2(quad_t a, quad_t b);
1038febc6baSAlfred Perlstein quad_t		__divdi3(quad_t a, quad_t b);
104*cc4f247fSJohn Baldwin quad_t		__divmoddi4(quad_t a, quad_t b, quad_t *rem);
105c7e459edSBruce Evans quad_t		__lshrdi3(quad_t, qshift_t);
1068febc6baSAlfred Perlstein quad_t		__moddi3(quad_t a, quad_t b);
1078febc6baSAlfred Perlstein u_quad_t	__qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem);
1088febc6baSAlfred Perlstein u_quad_t	__udivdi3(u_quad_t a, u_quad_t b);
109*cc4f247fSJohn Baldwin u_quad_t	__udivmoddi4(u_quad_t a, u_quad_t b, u_quad_t *rem);
1108febc6baSAlfred Perlstein u_quad_t	__umoddi3(u_quad_t a, u_quad_t b);
1118febc6baSAlfred Perlstein int		__ucmpdi2(u_quad_t a, u_quad_t b);
112df8bae1dSRodney W. Grimes 
11331735bf3SAndrew Turner /* ARM EABI support functions. */
11431735bf3SAndrew Turner #ifdef __ARM_EABI__
11531735bf3SAndrew Turner int		__aeabi_ulcmp(unsigned long long, unsigned long long);
11631735bf3SAndrew Turner #endif
11731735bf3SAndrew Turner 
118c7e459edSBruce Evans #endif /* !_LIBKERN_QUAD_H_ */
119