11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1992, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This software was developed by the Computer Systems Engineering group
61541Srgrimes * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
71541Srgrimes * contributed to Berkeley.
81541Srgrimes *
91541Srgrimes * Redistribution and use in source and binary forms, with or without
101541Srgrimes * modification, are permitted provided that the following conditions
111541Srgrimes * are met:
121541Srgrimes * 1. Redistributions of source code must retain the above copyright
131541Srgrimes *    notice, this list of conditions and the following disclaimer.
141541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer in the
161541Srgrimes *    documentation and/or other materials provided with the distribution.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)quad.h	8.1 (Berkeley) 6/4/93
3450477Speter * $FreeBSD$
351541Srgrimes */
361541Srgrimes
37128965Sbde#ifndef _LIBKERN_QUAD_H_
38128965Sbde#define	_LIBKERN_QUAD_H_
39104667Sphk
401541Srgrimes/*
411541Srgrimes * Quad arithmetic.
421541Srgrimes *
431541Srgrimes * This library makes the following assumptions:
441541Srgrimes *
451541Srgrimes *  - The type long long (aka quad_t) exists.
461541Srgrimes *
471541Srgrimes *  - A quad variable is exactly twice as long as `long'.
481541Srgrimes *
491541Srgrimes *  - The machine's arithmetic is two's complement.
501541Srgrimes *
511541Srgrimes * This library can provide 128-bit arithmetic on a machine with 128-bit
521541Srgrimes * quads and 64-bit longs, for instance, or 96-bit arithmetic on machines
531541Srgrimes * with 48-bit longs.
541541Srgrimes */
551541Srgrimes
5615311Sbde#include <sys/cdefs.h>
571541Srgrimes#include <sys/types.h>
58114216Skan#include <sys/limits.h>
5979418Sjulian#include <sys/syslimits.h>
601541Srgrimes
611541Srgrimes/*
621541Srgrimes * Depending on the desired operation, we view a `long long' (aka quad_t) in
631541Srgrimes * one or more of the following formats.
641541Srgrimes */
651541Srgrimesunion uu {
661541Srgrimes	quad_t	q;		/* as a (signed) quad */
671541Srgrimes	quad_t	uq;		/* as an unsigned quad */
681541Srgrimes	long	sl[2];		/* as two signed longs */
691541Srgrimes	u_long	ul[2];		/* as two unsigned longs */
701541Srgrimes};
711541Srgrimes
721541Srgrimes/*
731541Srgrimes * Define high and low longwords.
741541Srgrimes */
751541Srgrimes#define	H		_QUAD_HIGHWORD
761541Srgrimes#define	L		_QUAD_LOWWORD
771541Srgrimes
781541Srgrimes/*
791541Srgrimes * Total number of bits in a quad_t and in the pieces that make it up.
801541Srgrimes * These are used for shifting, and also below for halfword extraction
811541Srgrimes * and assembly.
821541Srgrimes */
831541Srgrimes#define	QUAD_BITS	(sizeof(quad_t) * CHAR_BIT)
841541Srgrimes#define	LONG_BITS	(sizeof(long) * CHAR_BIT)
851541Srgrimes#define	HALF_BITS	(sizeof(long) * CHAR_BIT / 2)
861541Srgrimes
871541Srgrimes/*
881541Srgrimes * Extract high and low shortwords from longword, and move low shortword of
891541Srgrimes * longword to upper half of long, i.e., produce the upper longword of
901541Srgrimes * ((quad_t)(x) << (number_of_bits_in_long/2)).  (`x' must actually be u_long.)
911541Srgrimes *
921541Srgrimes * These are used in the multiply code, to split a longword into upper
931541Srgrimes * and lower halves, and to reassemble a product as a quad_t, shifted left
941541Srgrimes * (sizeof(long)*CHAR_BIT/2).
951541Srgrimes */
961541Srgrimes#define	HHALF(x)	((x) >> HALF_BITS)
971541Srgrimes#define	LHALF(x)	((x) & ((1 << HALF_BITS) - 1))
981541Srgrimes#define	LHUP(x)		((x) << HALF_BITS)
991541Srgrimes
100128965Sbdetypedef unsigned int	qshift_t;
101128965Sbde
102128965Sbdequad_t		__ashldi3(quad_t, qshift_t);
103128965Sbdequad_t		__ashrdi3(quad_t, qshift_t);
104176496Smarcelint		__cmpdi2(quad_t a, quad_t b);
10592741Salfredquad_t		__divdi3(quad_t a, quad_t b);
106128965Sbdequad_t		__lshrdi3(quad_t, qshift_t);
10792741Salfredquad_t		__moddi3(quad_t a, quad_t b);
10892741Salfredu_quad_t	__qdivrem(u_quad_t u, u_quad_t v, u_quad_t *rem);
10992741Salfredu_quad_t	__udivdi3(u_quad_t a, u_quad_t b);
11092741Salfredu_quad_t	__umoddi3(u_quad_t a, u_quad_t b);
11192741Salfredint		__ucmpdi2(u_quad_t a, u_quad_t b);
1121541Srgrimes
113245840Sandrew/* ARM EABI support functions. */
114245840Sandrew#ifdef __ARM_EABI__
115245840Sandrewint		__aeabi_ulcmp(unsigned long long, unsigned long long);
116245840Sandrew#endif
117245840Sandrew
118128965Sbde#endif /* !_LIBKERN_QUAD_H_ */
119