fpu_arith.h revision 1.1
11.1Sbriggs/*	$NetBSD: fpu_arith.h,v 1.1 1995/11/03 04:46:59 briggs Exp $ */
21.1Sbriggs
31.1Sbriggs/*
41.1Sbriggs * Copyright (c) 1992, 1993
51.1Sbriggs *	The Regents of the University of California.  All rights reserved.
61.1Sbriggs *
71.1Sbriggs * This software was developed by the Computer Systems Engineering group
81.1Sbriggs * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
91.1Sbriggs * contributed to Berkeley.
101.1Sbriggs *
111.1Sbriggs * All advertising materials mentioning features or use of this software
121.1Sbriggs * must display the following acknowledgement:
131.1Sbriggs *	This product includes software developed by the University of
141.1Sbriggs *	California, Lawrence Berkeley Laboratory.
151.1Sbriggs *
161.1Sbriggs * Redistribution and use in source and binary forms, with or without
171.1Sbriggs * modification, are permitted provided that the following conditions
181.1Sbriggs * are met:
191.1Sbriggs * 1. Redistributions of source code must retain the above copyright
201.1Sbriggs *    notice, this list of conditions and the following disclaimer.
211.1Sbriggs * 2. Redistributions in binary form must reproduce the above copyright
221.1Sbriggs *    notice, this list of conditions and the following disclaimer in the
231.1Sbriggs *    documentation and/or other materials provided with the distribution.
241.1Sbriggs * 3. All advertising materials mentioning features or use of this software
251.1Sbriggs *    must display the following acknowledgement:
261.1Sbriggs *	This product includes software developed by the University of
271.1Sbriggs *	California, Berkeley and its contributors.
281.1Sbriggs * 4. Neither the name of the University nor the names of its contributors
291.1Sbriggs *    may be used to endorse or promote products derived from this software
301.1Sbriggs *    without specific prior written permission.
311.1Sbriggs *
321.1Sbriggs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
331.1Sbriggs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
341.1Sbriggs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
351.1Sbriggs * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
361.1Sbriggs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
371.1Sbriggs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
381.1Sbriggs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
391.1Sbriggs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
401.1Sbriggs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
411.1Sbriggs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
421.1Sbriggs * SUCH DAMAGE.
431.1Sbriggs *
441.1Sbriggs *	@(#)fpu_arith.h	8.1 (Berkeley) 6/11/93
451.1Sbriggs */
461.1Sbriggs
471.1Sbriggs/*
481.1Sbriggs * Extended-precision arithmetic.
491.1Sbriggs *
501.1Sbriggs * We hold the notion of a `carry register', which may or may not be a
511.1Sbriggs * machine carry bit or register.  On the SPARC, it is just the machine's
521.1Sbriggs * carry bit.
531.1Sbriggs *
541.1Sbriggs * In the worst case, you can compute the carry from x+y as
551.1Sbriggs *	(unsigned)(x + y) < (unsigned)x
561.1Sbriggs * and from x+y+c as
571.1Sbriggs *	((unsigned)(x + y + c) <= (unsigned)x && (y|c) != 0)
581.1Sbriggs * for example.
591.1Sbriggs */
601.1Sbriggs
611.1Sbriggs#ifdef sparc
621.1Sbriggs
631.1Sbriggs/* set up for extended-precision arithemtic */
641.1Sbriggs#define	FPU_DECL_CARRY
651.1Sbriggs
661.1Sbriggs/*
671.1Sbriggs * We have three kinds of add:
681.1Sbriggs *	add with carry:					  r = x + y + c
691.1Sbriggs *	add (ignoring current carry) and set carry:	c'r = x + y + 0
701.1Sbriggs *	add with carry and set carry:			c'r = x + y + c
711.1Sbriggs * The macros use `C' for `use carry' and `S' for `set carry'.
721.1Sbriggs * Note that the state of the carry is undefined after ADDC and SUBC,
731.1Sbriggs * so if all you have for these is `add with carry and set carry',
741.1Sbriggs * that is OK.
751.1Sbriggs *
761.1Sbriggs * The same goes for subtract, except that we compute x - y - c.
771.1Sbriggs *
781.1Sbriggs * Finally, we have a way to get the carry into a `regular' variable,
791.1Sbriggs * or set it from a value.  SET_CARRY turns 0 into no-carry, nonzero
801.1Sbriggs * into carry; GET_CARRY sets its argument to 0 or 1.
811.1Sbriggs */
821.1Sbriggs#define	FPU_ADDC(r, x, y) \
831.1Sbriggs	asm volatile("addx %1,%2,%0" : "=r"(r) : "r"(x), "r"(y))
841.1Sbriggs#define	FPU_ADDS(r, x, y) \
851.1Sbriggs	asm volatile("addcc %1,%2,%0" : "=r"(r) : "r"(x), "r"(y))
861.1Sbriggs#define	FPU_ADDCS(r, x, y) \
871.1Sbriggs	asm volatile("addxcc %1,%2,%0" : "=r"(r) : "r"(x), "r"(y))
881.1Sbriggs#define	FPU_SUBC(r, x, y) \
891.1Sbriggs	asm volatile("subx %1,%2,%0" : "=r"(r) : "r"(x), "r"(y))
901.1Sbriggs#define	FPU_SUBS(r, x, y) \
911.1Sbriggs	asm volatile("subcc %1,%2,%0" : "=r"(r) : "r"(x), "r"(y))
921.1Sbriggs#define	FPU_SUBCS(r, x, y) \
931.1Sbriggs	asm volatile("subxcc %1,%2,%0" : "=r"(r) : "r"(x), "r"(y))
941.1Sbriggs
951.1Sbriggs#define	FPU_GET_CARRY(r) asm volatile("addx %%g0,%%g0,%0" : "=r"(r))
961.1Sbriggs#define	FPU_SET_CARRY(v) asm volatile("addcc %0,-1,%%g0" : : "r"(v))
971.1Sbriggs
981.1Sbriggs#define	FPU_SHL1_BY_ADD	/* shift left 1 faster by ADDC than (a<<1)|(b>>31) */
991.1Sbriggs
1001.1Sbriggs#else /* non sparc */
1011.1Sbriggs
1021.1Sbriggs/* set up for extended-precision arithemtic */
1031.1Sbriggs#define	FPU_DECL_CARRY quad_t fpu_carry, fpu_tmp;
1041.1Sbriggs
1051.1Sbriggs/*
1061.1Sbriggs * We have three kinds of add:
1071.1Sbriggs *	add with carry:					  r = x + y + c
1081.1Sbriggs *	add (ignoring current carry) and set carry:	c'r = x + y + 0
1091.1Sbriggs *	add with carry and set carry:			c'r = x + y + c
1101.1Sbriggs * The macros use `C' for `use carry' and `S' for `set carry'.
1111.1Sbriggs * Note that the state of the carry is undefined after ADDC and SUBC,
1121.1Sbriggs * so if all you have for these is `add with carry and set carry',
1131.1Sbriggs * that is OK.
1141.1Sbriggs *
1151.1Sbriggs * The same goes for subtract, except that we compute x - y - c.
1161.1Sbriggs *
1171.1Sbriggs * Finally, we have a way to get the carry into a `regular' variable,
1181.1Sbriggs * or set it from a value.  SET_CARRY turns 0 into no-carry, nonzero
1191.1Sbriggs * into carry; GET_CARRY sets its argument to 0 or 1.
1201.1Sbriggs */
1211.1Sbriggs#define	FPU_ADDC(r, x, y) \
1221.1Sbriggs	(r) = (x) + (y) + (!!fpu_carry)
1231.1Sbriggs#define	FPU_ADDS(r, x, y) \
1241.1Sbriggs	{ \
1251.1Sbriggs		fpu_tmp = (quad_t)(x) + (quad_t)(y); \
1261.1Sbriggs		(r) = (u_int)fpu_tmp; \
1271.1Sbriggs		fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \
1281.1Sbriggs	}
1291.1Sbriggs#define	FPU_ADDCS(r, x, y) \
1301.1Sbriggs	{ \
1311.1Sbriggs		fpu_tmp = (quad_t)(x) + (quad_t)(y) + (!!fpu_carry); \
1321.1Sbriggs		(r) = (u_int)fpu_tmp; \
1331.1Sbriggs		fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \
1341.1Sbriggs	}
1351.1Sbriggs#define	FPU_SUBC(r, x, y) \
1361.1Sbriggs	(r) = (x) - (y) - (!!fpu_carry)
1371.1Sbriggs#define	FPU_SUBS(r, x, y) \
1381.1Sbriggs	{ \
1391.1Sbriggs		fpu_tmp = (quad_t)(x) - (quad_t)(y); \
1401.1Sbriggs		(r) = (u_int)fpu_tmp; \
1411.1Sbriggs		fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \
1421.1Sbriggs	}
1431.1Sbriggs#define	FPU_SUBCS(r, x, y) \
1441.1Sbriggs	{ \
1451.1Sbriggs		fpu_tmp = (quad_t)(x) - (quad_t)(y) - (!!fpu_carry); \
1461.1Sbriggs		(r) = (u_int)fpu_tmp; \
1471.1Sbriggs		fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \
1481.1Sbriggs	}
1491.1Sbriggs
1501.1Sbriggs#define	FPU_GET_CARRY(r) (r) = (!!fpu_carry)
1511.1Sbriggs#define	FPU_SET_CARRY(v) fpu_carry = ((v) != 0)
1521.1Sbriggs
1531.1Sbriggs#endif
154