153439Sbostic /*- 2*61160Sbostic * Copyright (c) 1992, 1993 3*61160Sbostic * The Regents of the University of California. All rights reserved. 453439Sbostic * 553794Sbostic * This software was developed by the Computer Systems Engineering group 653794Sbostic * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 753794Sbostic * contributed to Berkeley. 853794Sbostic * 953439Sbostic * %sccs.include.redist.c% 1053795Sbostic * 11*61160Sbostic * @(#)quad.h 8.1 (Berkeley) 06/04/93 1253794Sbostic */ 1353794Sbostic 1453794Sbostic /* 1553794Sbostic * Quad arithmetic. 1653439Sbostic * 1753794Sbostic * This library makes the following assumptions: 1853794Sbostic * 1954431Sbostic * - The type long long (aka quad_t) exists. 2053794Sbostic * 2153794Sbostic * - A quad variable is exactly twice as long as `long'. 2253794Sbostic * 2353794Sbostic * - The machine's arithmetic is two's complement. 2453794Sbostic * 2554431Sbostic * This library can provide 128-bit arithmetic on a machine with 128-bit 2654431Sbostic * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines 2754431Sbostic * with 48-bit longs. 2853439Sbostic */ 2953439Sbostic 3054431Sbostic #include <sys/types.h> 3153794Sbostic #include <limits.h> 3253459Sbostic 3353794Sbostic /* 3454431Sbostic * Depending on the desired operation, we view a `long long' (aka quad_t) in 3553794Sbostic * one or more of the following formats. 3653794Sbostic */ 3753794Sbostic union uu { 3854431Sbostic quad_t q; /* as a (signed) quad */ 3954431Sbostic quad_t uq; /* as an unsigned quad */ 4053794Sbostic long sl[2]; /* as two signed longs */ 4153794Sbostic u_long ul[2]; /* as two unsigned longs */ 4253794Sbostic }; 4353459Sbostic 4453794Sbostic /* 4553794Sbostic * Define high and low longwords. 4653794Sbostic */ 4753794Sbostic #define H _QUAD_HIGHWORD 4853794Sbostic #define L _QUAD_LOWWORD 4953459Sbostic 5053794Sbostic /* 5154431Sbostic * Total number of bits in a quad_t and in the pieces that make it up. 5253794Sbostic * These are used for shifting, and also below for halfword extraction 5353794Sbostic * and assembly. 5453794Sbostic */ 5554431Sbostic #define QUAD_BITS (sizeof(quad_t) * CHAR_BIT) 5653794Sbostic #define LONG_BITS (sizeof(long) * CHAR_BIT) 5753794Sbostic #define HALF_BITS (sizeof(long) * CHAR_BIT / 2) 5853459Sbostic 5953794Sbostic /* 6053794Sbostic * Extract high and low shortwords from longword, and move low shortword of 6153794Sbostic * longword to upper half of long, i.e., produce the upper longword of 6254431Sbostic * ((quad_t)(x) << (number_of_bits_in_long/2)). (`x' must actually be u_long.) 6353794Sbostic * 6453794Sbostic * These are used in the multiply code, to split a longword into upper 6554431Sbostic * and lower halves, and to reassemble a product as a quad_t, shifted left 6653794Sbostic * (sizeof(long)*CHAR_BIT/2). 6753794Sbostic */ 6853794Sbostic #define HHALF(x) ((x) >> HALF_BITS) 6953794Sbostic #define LHALF(x) ((x) & ((1 << HALF_BITS) - 1)) 7053794Sbostic #define LHUP(x) ((x) << HALF_BITS) 7153439Sbostic 7254431Sbostic extern u_quad_t __qdivrem __P((u_quad_t u, u_quad_t v, u_quad_t *rem)); 7354182Sbostic 7454182Sbostic /* 7554182Sbostic * XXX 7654182Sbostic * Compensate for gcc 1 vs gcc 2. Gcc 1 defines ?sh?di3's second argument 7754431Sbostic * as u_quad_t, while gcc 2 correctly uses int. Unfortunately, we still use 7854182Sbostic * both compilers. 7954182Sbostic */ 8055768Sbostic #if __GNUC__ >= 2 8154182Sbostic typedef unsigned int qshift_t; 8254182Sbostic #else 8354431Sbostic typedef u_quad_t qshift_t; 8454182Sbostic #endif 85