1 /*- 2 * Copyright (c) 1992 The Regents of the University of California. 3 * 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 5.6 (Berkeley) 06/02/92 12 */ 13 14 /* 15 * Quad arithmetic. 16 * 17 * This library makes the following assumptions: 18 * 19 * - The type long long (aka quad) 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 * All other machine parameters are encapsulated here. This library can 26 * provide 128-bit arithmetic on a machine with 128-bit quads and 64-bit 27 * longs, for instance, or 96-bit arithmetic on machines with 48-bit longs. 28 */ 29 30 #ifndef SPARC_XXX 31 #include <machine/endian.h> /* see #else case */ 32 #else 33 /* 34 * These are for testing and for illustration: we expect <machine/endian.h> 35 * to define these. Actually, these match most big-endian machines; for 36 * most little-endian machines, all you need do is exchange _QUAD_HIGHWORD 37 * and _QUAD_LOWWORD. 38 */ 39 #define _QUAD_HIGHWORD 0 40 #define _QUAD_LOWWORD 1 41 #endif 42 43 typedef long long quad; 44 typedef unsigned long long u_quad; 45 typedef unsigned long u_long; 46 47 #include <limits.h> 48 /* 49 * We expect something like these from <limits.h>, which should be provided on 50 * any ANSI C system. 51 #define USHRT_MAX 0xffff 52 #define CHAR_BIT 8 53 */ 54 55 /* 56 * Depending on the desired operation, we view a `long long' (aka quad) in 57 * one or more of the following formats. 58 */ 59 union uu { 60 quad q; /* as a (signed) quad */ 61 quad uq; /* as an unsigned quad */ 62 long sl[2]; /* as two signed longs */ 63 u_long ul[2]; /* as two unsigned longs */ 64 }; 65 66 /* 67 * Define high and low longwords. 68 */ 69 #define H _QUAD_HIGHWORD 70 #define L _QUAD_LOWWORD 71 72 /* 73 * Total number of bits in a quad and in the pieces that make it up. 74 * These are used for shifting, and also below for halfword extraction 75 * and assembly. 76 */ 77 #define QUAD_BITS (sizeof(quad) * CHAR_BIT) 78 #define LONG_BITS (sizeof(long) * CHAR_BIT) 79 #define HALF_BITS (sizeof(long) * CHAR_BIT / 2) 80 81 /* 82 * Extract high and low shortwords from longword, and move low shortword of 83 * longword to upper half of long, i.e., produce the upper longword of 84 * ((quad)(x) << (number_of_bits_in_long/2)). (`x' must actually be u_long.) 85 * 86 * These are used in the multiply code, to split a longword into upper 87 * and lower halves, and to reassemble a product as a quad, shifted left 88 * (sizeof(long)*CHAR_BIT/2). 89 */ 90 #define HHALF(x) ((x) >> HALF_BITS) 91 #define LHALF(x) ((x) & ((1 << HALF_BITS) - 1)) 92 #define LHUP(x) ((x) << HALF_BITS) 93 94 extern u_quad __qdivrem(u_quad u, u_quad v, u_quad *rem); 95