1*4a5d661aSToomas Soome /*- 2*4a5d661aSToomas Soome * Copyright (c) 1992, 1993 3*4a5d661aSToomas Soome * The Regents of the University of California. All rights reserved. 4*4a5d661aSToomas Soome * 5*4a5d661aSToomas Soome * This software was developed by the Computer Systems Engineering group 6*4a5d661aSToomas Soome * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 7*4a5d661aSToomas Soome * contributed to Berkeley. 8*4a5d661aSToomas Soome * 9*4a5d661aSToomas Soome * Redistribution and use in source and binary forms, with or without 10*4a5d661aSToomas Soome * modification, are permitted provided that the following conditions 11*4a5d661aSToomas Soome * are met: 12*4a5d661aSToomas Soome * 1. Redistributions of source code must retain the above copyright 13*4a5d661aSToomas Soome * notice, this list of conditions and the following disclaimer. 14*4a5d661aSToomas Soome * 2. Redistributions in binary form must reproduce the above copyright 15*4a5d661aSToomas Soome * notice, this list of conditions and the following disclaimer in the 16*4a5d661aSToomas Soome * documentation and/or other materials provided with the distribution. 17*4a5d661aSToomas Soome * 4. Neither the name of the University nor the names of its contributors 18*4a5d661aSToomas Soome * may be used to endorse or promote products derived from this software 19*4a5d661aSToomas Soome * without specific prior written permission. 20*4a5d661aSToomas Soome * 21*4a5d661aSToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22*4a5d661aSToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23*4a5d661aSToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24*4a5d661aSToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25*4a5d661aSToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26*4a5d661aSToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27*4a5d661aSToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28*4a5d661aSToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29*4a5d661aSToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30*4a5d661aSToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*4a5d661aSToomas Soome * SUCH DAMAGE. 32*4a5d661aSToomas Soome * 33*4a5d661aSToomas Soome * @(#)quad.h 8.1 (Berkeley) 6/4/93 34*4a5d661aSToomas Soome * $FreeBSD$ 35*4a5d661aSToomas Soome */ 36*4a5d661aSToomas Soome 37*4a5d661aSToomas Soome /* 38*4a5d661aSToomas Soome * Quad arithmetic. 39*4a5d661aSToomas Soome * 40*4a5d661aSToomas Soome * This library makes the following assumptions: 41*4a5d661aSToomas Soome * 42*4a5d661aSToomas Soome * - The type long long (aka quad_t) exists. 43*4a5d661aSToomas Soome * 44*4a5d661aSToomas Soome * - A quad variable is exactly twice as long as `long'. 45*4a5d661aSToomas Soome * 46*4a5d661aSToomas Soome * - The machine's arithmetic is two's complement. 47*4a5d661aSToomas Soome * 48*4a5d661aSToomas Soome * This library can provide 128-bit arithmetic on a machine with 128-bit 49*4a5d661aSToomas Soome * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines 50*4a5d661aSToomas Soome * with 48-bit longs. 51*4a5d661aSToomas Soome */ 52*4a5d661aSToomas Soome 53*4a5d661aSToomas Soome #include <sys/cdefs.h> 54*4a5d661aSToomas Soome #include <sys/types.h> 55*4a5d661aSToomas Soome #include <limits.h> 56*4a5d661aSToomas Soome 57*4a5d661aSToomas Soome _Static_assert(sizeof(quad_t) == sizeof(int) * 2, 58*4a5d661aSToomas Soome "Bitwise function in libstand are broken on this architecture\n"); 59*4a5d661aSToomas Soome 60*4a5d661aSToomas Soome /* 61*4a5d661aSToomas Soome * Depending on the desired operation, we view a `long long' (aka quad_t) in 62*4a5d661aSToomas Soome * one or more of the following formats. 63*4a5d661aSToomas Soome */ 64*4a5d661aSToomas Soome union uu { 65*4a5d661aSToomas Soome quad_t q; /* as a (signed) quad */ 66*4a5d661aSToomas Soome quad_t uq; /* as an unsigned quad */ 67*4a5d661aSToomas Soome int sl[2]; /* as two signed ints */ 68*4a5d661aSToomas Soome u_int ul[2]; /* as two unsigned ints */ 69*4a5d661aSToomas Soome }; 70*4a5d661aSToomas Soome 71*4a5d661aSToomas Soome /* 72*4a5d661aSToomas Soome * Define high and low longwords. 73*4a5d661aSToomas Soome */ 74*4a5d661aSToomas Soome #define H _QUAD_HIGHWORD 75*4a5d661aSToomas Soome #define L _QUAD_LOWWORD 76*4a5d661aSToomas Soome 77*4a5d661aSToomas Soome /* 78*4a5d661aSToomas Soome * Total number of bits in a quad_t and in the pieces that make it up. 79*4a5d661aSToomas Soome * These are used for shifting, and also below for halfword extraction 80*4a5d661aSToomas Soome * and assembly. 81*4a5d661aSToomas Soome */ 82*4a5d661aSToomas Soome #define QUAD_BITS (sizeof(quad_t) * CHAR_BIT) 83*4a5d661aSToomas Soome #define HALF_BITS (sizeof(int) * CHAR_BIT / 2) 84*4a5d661aSToomas Soome 85*4a5d661aSToomas Soome /* 86*4a5d661aSToomas Soome * Extract high and low shortwords from longword, and move low shortword of 87*4a5d661aSToomas Soome * longword to upper half of long, i.e., produce the upper longword of 88*4a5d661aSToomas Soome * ((quad_t)(x) << (number_of_bits_in_long/2)). (`x' must actually be u_long.) 89*4a5d661aSToomas Soome * 90*4a5d661aSToomas Soome * These are used in the multiply code, to split a longword into upper 91*4a5d661aSToomas Soome * and lower halves, and to reassemble a product as a quad_t, shifted left 92*4a5d661aSToomas Soome * (sizeof(long)*CHAR_BIT/2). 93*4a5d661aSToomas Soome */ 94*4a5d661aSToomas Soome #define HHALF(x) ((x) >> HALF_BITS) 95*4a5d661aSToomas Soome #define LHALF(x) ((x) & ((1 << HALF_BITS) - 1)) 96*4a5d661aSToomas Soome #define LHUP(x) ((x) << HALF_BITS) 97*4a5d661aSToomas Soome 98*4a5d661aSToomas Soome quad_t __divdi3(quad_t a, quad_t b); 99*4a5d661aSToomas Soome quad_t __moddi3(quad_t a, quad_t b); 100*4a5d661aSToomas Soome u_quad_t __qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem); 101*4a5d661aSToomas Soome u_quad_t __udivdi3(u_quad_t a, u_quad_t b); 102*4a5d661aSToomas Soome u_quad_t __umoddi3(u_quad_t a, u_quad_t b); 103*4a5d661aSToomas Soome 104*4a5d661aSToomas Soome /* 105*4a5d661aSToomas Soome * XXX 106*4a5d661aSToomas Soome * Compensate for gcc 1 vs gcc 2. Gcc 1 defines ?sh?di3's second argument 107*4a5d661aSToomas Soome * as u_quad_t, while gcc 2 correctly uses int. Unfortunately, we still use 108*4a5d661aSToomas Soome * both compilers. 109*4a5d661aSToomas Soome */ 110*4a5d661aSToomas Soome #if __GNUC__ >= 2 111*4a5d661aSToomas Soome typedef unsigned int qshift_t; 112*4a5d661aSToomas Soome #else 113*4a5d661aSToomas Soome typedef u_quad_t qshift_t; 114*4a5d661aSToomas Soome #endif 115