1 /*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This software was developed by the Computer Systems Engineering group 6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 7 * contributed to Berkeley. 8 * 9 * %sccs.include.redist.c% 10 * 11 * @(#)quad.h 8.1 (Berkeley) 06/04/93 12 */ 13 14 /* 15 * Quad arithmetic. 16 * 17 * This library makes the following assumptions: 18 * 19 * - The type long long (aka quad_t) exists. 20 * 21 * - A quad variable is exactly twice as long as `long'. 22 * 23 * - The machine's arithmetic is two's complement. 24 * 25 * This library can provide 128-bit arithmetic on a machine with 128-bit 26 * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines 27 * with 48-bit longs. 28 */ 29 30 #include <sys/types.h> 31 #include <limits.h> 32 33 /* 34 * Depending on the desired operation, we view a `long long' (aka quad_t) in 35 * one or more of the following formats. 36 */ 37 union uu { 38 quad_t q; /* as a (signed) quad */ 39 quad_t uq; /* as an unsigned quad */ 40 long sl[2]; /* as two signed longs */ 41 u_long ul[2]; /* as two unsigned longs */ 42 }; 43 44 /* 45 * Define high and low longwords. 46 */ 47 #define H _QUAD_HIGHWORD 48 #define L _QUAD_LOWWORD 49 50 /* 51 * Total number of bits in a quad_t and in the pieces that make it up. 52 * These are used for shifting, and also below for halfword extraction 53 * and assembly. 54 */ 55 #define QUAD_BITS (sizeof(quad_t) * CHAR_BIT) 56 #define LONG_BITS (sizeof(long) * CHAR_BIT) 57 #define HALF_BITS (sizeof(long) * CHAR_BIT / 2) 58 59 /* 60 * Extract high and low shortwords from longword, and move low shortword of 61 * longword to upper half of long, i.e., produce the upper longword of 62 * ((quad_t)(x) << (number_of_bits_in_long/2)). (`x' must actually be u_long.) 63 * 64 * These are used in the multiply code, to split a longword into upper 65 * and lower halves, and to reassemble a product as a quad_t, shifted left 66 * (sizeof(long)*CHAR_BIT/2). 67 */ 68 #define HHALF(x) ((x) >> HALF_BITS) 69 #define LHALF(x) ((x) & ((1 << HALF_BITS) - 1)) 70 #define LHUP(x) ((x) << HALF_BITS) 71 72 extern u_quad_t __qdivrem __P((u_quad_t u, u_quad_t v, u_quad_t *rem)); 73 74 /* 75 * XXX 76 * Compensate for gcc 1 vs gcc 2. Gcc 1 defines ?sh?di3's second argument 77 * as u_quad_t, while gcc 2 correctly uses int. Unfortunately, we still use 78 * both compilers. 79 */ 80 #if __GNUC__ >= 2 81 typedef unsigned int qshift_t; 82 #else 83 typedef u_quad_t qshift_t; 84 #endif 85