199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1992, 1993
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Regents of the University of California. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This software was developed by the Computer Systems Engineering group
199767f8919635c4928607450d9e0abb932109ceToomas Soome * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * contributed to Berkeley.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. Neither the name of the University nor the names of its contributors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * may be used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @(#)quad.h 8.1 (Berkeley) 6/4/93
199767f8919635c4928607450d9e0abb932109ceToomas Soome * $FreeBSD$
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Quad arithmetic.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This library makes the following assumptions:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - The type long long (aka quad_t) exists.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - A quad variable is exactly twice as long as `long'.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - The machine's arithmetic is two's complement.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This library can provide 128-bit arithmetic on a machine with 128-bit
199767f8919635c4928607450d9e0abb932109ceToomas Soome * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
199767f8919635c4928607450d9e0abb932109ceToomas Soome * with 48-bit longs.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/types.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <limits.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome_Static_assert(sizeof(quad_t) == sizeof(int) * 2,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Bitwise function in libstand are broken on this architecture\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Depending on the desired operation, we view a `long long' (aka quad_t) in
199767f8919635c4928607450d9e0abb932109ceToomas Soome * one or more of the following formats.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeunion uu {
199767f8919635c4928607450d9e0abb932109ceToomas Soome quad_t q; /* as a (signed) quad */
199767f8919635c4928607450d9e0abb932109ceToomas Soome quad_t uq; /* as an unsigned quad */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int sl[2]; /* as two signed ints */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int ul[2]; /* as two unsigned ints */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Define high and low longwords.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define H _QUAD_HIGHWORD
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define L _QUAD_LOWWORD
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Total number of bits in a quad_t and in the pieces that make it up.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * These are used for shifting, and also below for halfword extraction
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and assembly.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define QUAD_BITS (sizeof(quad_t) * CHAR_BIT)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define HALF_BITS (sizeof(int) * CHAR_BIT / 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Extract high and low shortwords from longword, and move low shortword of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * longword to upper half of long, i.e., produce the upper longword of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ((quad_t)(x) << (number_of_bits_in_long/2)). (`x' must actually be u_long.)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * These are used in the multiply code, to split a longword into upper
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and lower halves, and to reassemble a product as a quad_t, shifted left
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (sizeof(long)*CHAR_BIT/2).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define HHALF(x) ((x) >> HALF_BITS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define LHALF(x) ((x) & ((1 << HALF_BITS) - 1))
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define LHUP(x) ((x) << HALF_BITS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomequad_t __divdi3(quad_t a, quad_t b);
199767f8919635c4928607450d9e0abb932109ceToomas Soomequad_t __moddi3(quad_t a, quad_t b);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeu_quad_t __qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeu_quad_t __udivdi3(u_quad_t a, u_quad_t b);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeu_quad_t __umoddi3(u_quad_t a, u_quad_t b);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * XXX
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Compensate for gcc 1 vs gcc 2. Gcc 1 defines ?sh?di3's second argument
199767f8919635c4928607450d9e0abb932109ceToomas Soome * as u_quad_t, while gcc 2 correctly uses int. Unfortunately, we still use
199767f8919635c4928607450d9e0abb932109ceToomas Soome * both compilers.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if __GNUC__ >= 2
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef unsigned int qshift_t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef u_quad_t qshift_t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif