xref: /openbsd-src/sys/lib/libkern/quad.h (revision b5be37d2a6fbef83b90ca99497343fa8dbb0f3fc)
1df930be7Sderaadt /*-
2df930be7Sderaadt  * Copyright (c) 1992, 1993
3df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
4df930be7Sderaadt  *
5df930be7Sderaadt  * This software was developed by the Computer Systems Engineering group
6df930be7Sderaadt  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7df930be7Sderaadt  * contributed to Berkeley.
8df930be7Sderaadt  *
9df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
10df930be7Sderaadt  * modification, are permitted provided that the following conditions
11df930be7Sderaadt  * are met:
12df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
13df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
14df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
15df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
16df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
1729295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
18df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
19df930be7Sderaadt  *    without specific prior written permission.
20df930be7Sderaadt  *
21df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df930be7Sderaadt  * SUCH DAMAGE.
32df930be7Sderaadt  *
33*b5be37d2Sderaadt  *	$OpenBSD: quad.h,v 1.12 2014/06/10 04:16:57 deraadt Exp $
34df930be7Sderaadt  */
35df930be7Sderaadt 
36df930be7Sderaadt /*
37df930be7Sderaadt  * Quad arithmetic.
38df930be7Sderaadt  *
39df930be7Sderaadt  * This library makes the following assumptions:
40df930be7Sderaadt  *
41df930be7Sderaadt  *  - The type long long (aka quad_t) exists.
42df930be7Sderaadt  *
43f6a3c9b1Smickey  *  - A quad variable is exactly twice as long as `int'.
44df930be7Sderaadt  *
45df930be7Sderaadt  *  - The machine's arithmetic is two's complement.
46df930be7Sderaadt  *
47df930be7Sderaadt  * This library can provide 128-bit arithmetic on a machine with 128-bit
48f6a3c9b1Smickey  * quads and 64-bit ints, for instance, or 96-bit arithmetic on machines
49f6a3c9b1Smickey  * with 48-bit ints.
50df930be7Sderaadt  */
51df930be7Sderaadt 
52df930be7Sderaadt #include <sys/types.h>
535733d2deSespie #include <sys/limits.h>
54df930be7Sderaadt 
55df930be7Sderaadt /*
56df930be7Sderaadt  * Depending on the desired operation, we view a `long long' (aka quad_t) in
57df930be7Sderaadt  * one or more of the following formats.
58df930be7Sderaadt  */
59df930be7Sderaadt union uu {
60df930be7Sderaadt 	quad_t	q;		/* as a (signed) quad */
61df930be7Sderaadt 	u_quad_t uq;		/* as an unsigned quad */
62f6a3c9b1Smickey 	int	sl[2];		/* as two signed ints */
63f6a3c9b1Smickey 	u_int	ul[2];		/* as two unsigned ints */
64df930be7Sderaadt };
65df930be7Sderaadt 
66df930be7Sderaadt /*
67f6a3c9b1Smickey  * Define high and low parts of a quad_t.
68df930be7Sderaadt  */
69df930be7Sderaadt #define	H		_QUAD_HIGHWORD
70df930be7Sderaadt #define	L		_QUAD_LOWWORD
71df930be7Sderaadt 
72df930be7Sderaadt /*
73df930be7Sderaadt  * Total number of bits in a quad_t and in the pieces that make it up.
74df930be7Sderaadt  * These are used for shifting, and also below for halfword extraction
75df930be7Sderaadt  * and assembly.
76df930be7Sderaadt  */
77df930be7Sderaadt #define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
78f6a3c9b1Smickey #define	INT_BITS	(sizeof(int) * CHAR_BIT)
79f6a3c9b1Smickey #define	HALF_BITS	(sizeof(int) * CHAR_BIT / 2)
80df930be7Sderaadt 
81df930be7Sderaadt /*
82df930be7Sderaadt  * Extract high and low shortwords from longword, and move low shortword of
83df930be7Sderaadt  * longword to upper half of long, i.e., produce the upper longword of
84f6a3c9b1Smickey  * ((quad_t)(x) << (number_of_bits_in_int/2)).  (`x' must actually be u_int.)
85df930be7Sderaadt  *
86df930be7Sderaadt  * These are used in the multiply code, to split a longword into upper
87df930be7Sderaadt  * and lower halves, and to reassemble a product as a quad_t, shifted left
88f6a3c9b1Smickey  * (sizeof(int)*CHAR_BIT/2).
89df930be7Sderaadt  */
90f6a3c9b1Smickey #define	HHALF(x)	((u_int)(x) >> HALF_BITS)
91f6a3c9b1Smickey #define	LHALF(x)	((u_int)(x) & (((int)1 << HALF_BITS) - 1))
92f6a3c9b1Smickey #define	LHUP(x)		((u_int)(x) << HALF_BITS)
93df930be7Sderaadt 
94df930be7Sderaadt typedef unsigned int	qshift_t;
950c0430f8Sniklas 
96c4071fd1Smillert quad_t __adddi3(quad_t, quad_t);
97c4071fd1Smillert quad_t __anddi3(quad_t, quad_t);
98c4071fd1Smillert quad_t __ashldi3(quad_t, qshift_t);
99c4071fd1Smillert quad_t __ashrdi3(quad_t, qshift_t);
100c4071fd1Smillert int __cmpdi2(quad_t, quad_t);
101c4071fd1Smillert quad_t __divdi3(quad_t, quad_t);
102*b5be37d2Sderaadt quad_t __fixdfdi(double);
103*b5be37d2Sderaadt quad_t __fixsfdi(float);
104*b5be37d2Sderaadt u_quad_t __fixunsdfdi(double);
105*b5be37d2Sderaadt u_quad_t __fixunssfdi(float);
106*b5be37d2Sderaadt double __floatdidf(quad_t);
107*b5be37d2Sderaadt float __floatdisf(quad_t);
108*b5be37d2Sderaadt double __floatunsdidf(u_quad_t);
109c4071fd1Smillert quad_t __iordi3(quad_t, quad_t);
110c4071fd1Smillert quad_t __lshldi3(quad_t, qshift_t);
111c4071fd1Smillert quad_t __lshrdi3(quad_t, qshift_t);
112c4071fd1Smillert quad_t __moddi3(quad_t, quad_t);
113c4071fd1Smillert quad_t __muldi3(quad_t, quad_t);
114c4071fd1Smillert quad_t __negdi2(quad_t);
115c4071fd1Smillert quad_t __one_cmpldi2(quad_t);
116f6a3c9b1Smickey u_quad_t __qdivrem(u_quad_t, u_quad_t, u_quad_t *);
117c4071fd1Smillert quad_t __subdi3(quad_t, quad_t);
118c4071fd1Smillert int __ucmpdi2(u_quad_t, u_quad_t);
119c4071fd1Smillert u_quad_t __udivdi3(u_quad_t, u_quad_t );
120c4071fd1Smillert u_quad_t __umoddi3(u_quad_t, u_quad_t );
121c4071fd1Smillert quad_t __xordi3(quad_t, quad_t);
122