fpu_arith.h revision 1.8
11.8Sisaki/*	$NetBSD: fpu_arith.h,v 1.8 2013/04/01 13:59:21 isaki 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.3Sagc * 3. Neither the name of the University nor the names of its contributors
251.1Sbriggs *    may be used to endorse or promote products derived from this software
261.1Sbriggs *    without specific prior written permission.
271.1Sbriggs *
281.1Sbriggs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
291.1Sbriggs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
301.1Sbriggs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
311.1Sbriggs * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
321.1Sbriggs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
331.1Sbriggs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
341.1Sbriggs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
351.1Sbriggs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
361.1Sbriggs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
371.1Sbriggs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381.1Sbriggs * SUCH DAMAGE.
391.1Sbriggs *
401.1Sbriggs *	@(#)fpu_arith.h	8.1 (Berkeley) 6/11/93
411.1Sbriggs */
421.1Sbriggs
431.1Sbriggs/*
441.1Sbriggs * Extended-precision arithmetic.
451.1Sbriggs *
461.1Sbriggs * We hold the notion of a `carry register', which may or may not be a
471.1Sbriggs * machine carry bit or register.  On the SPARC, it is just the machine's
481.1Sbriggs * carry bit.
491.1Sbriggs *
501.1Sbriggs * In the worst case, you can compute the carry from x+y as
511.1Sbriggs *	(unsigned)(x + y) < (unsigned)x
521.1Sbriggs * and from x+y+c as
531.1Sbriggs *	((unsigned)(x + y + c) <= (unsigned)x && (y|c) != 0)
541.1Sbriggs * for example.
551.1Sbriggs */
561.1Sbriggs
571.2Sbriggs#ifndef FPE_USE_ASM
581.1Sbriggs
591.1Sbriggs/* set up for extended-precision arithemtic */
601.8Sisaki#define	FPU_DECL_CARRY uint64_t fpu_carry, fpu_tmp;
611.1Sbriggs
621.1Sbriggs/*
631.1Sbriggs * We have three kinds of add:
641.1Sbriggs *	add with carry:					  r = x + y + c
651.1Sbriggs *	add (ignoring current carry) and set carry:	c'r = x + y + 0
661.1Sbriggs *	add with carry and set carry:			c'r = x + y + c
671.1Sbriggs * The macros use `C' for `use carry' and `S' for `set carry'.
681.1Sbriggs * Note that the state of the carry is undefined after ADDC and SUBC,
691.1Sbriggs * so if all you have for these is `add with carry and set carry',
701.1Sbriggs * that is OK.
711.1Sbriggs *
721.1Sbriggs * The same goes for subtract, except that we compute x - y - c.
731.1Sbriggs *
741.1Sbriggs * Finally, we have a way to get the carry into a `regular' variable,
751.1Sbriggs * or set it from a value.  SET_CARRY turns 0 into no-carry, nonzero
761.1Sbriggs * into carry; GET_CARRY sets its argument to 0 or 1.
771.1Sbriggs */
781.1Sbriggs#define	FPU_ADDC(r, x, y) \
791.2Sbriggs	(r) = (x) + (y) + (!!fpu_carry)
801.1Sbriggs#define	FPU_ADDS(r, x, y) \
811.2Sbriggs	{ \
821.8Sisaki		fpu_tmp = (uint64_t)(x) + (uint64_t)(y); \
831.7Sisaki		(r) = (uint32_t)fpu_tmp; \
841.2Sbriggs		fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \
851.2Sbriggs	}
861.1Sbriggs#define	FPU_ADDCS(r, x, y) \
871.2Sbriggs	{ \
881.8Sisaki		fpu_tmp = (uint64_t)(x) + (uint64_t)(y) + (!!fpu_carry); \
891.7Sisaki		(r) = (uint32_t)fpu_tmp; \
901.2Sbriggs		fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \
911.2Sbriggs	}
921.1Sbriggs#define	FPU_SUBC(r, x, y) \
931.2Sbriggs	(r) = (x) - (y) - (!!fpu_carry)
941.1Sbriggs#define	FPU_SUBS(r, x, y) \
951.2Sbriggs	{ \
961.8Sisaki		fpu_tmp = (uint64_t)(x) - (uint64_t)(y); \
971.7Sisaki		(r) = (uint32_t)fpu_tmp; \
981.2Sbriggs		fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \
991.2Sbriggs	}
1001.1Sbriggs#define	FPU_SUBCS(r, x, y) \
1011.2Sbriggs	{ \
1021.8Sisaki		fpu_tmp = (uint64_t)(x) - (uint64_t)(y) - (!!fpu_carry); \
1031.7Sisaki		(r) = (uint32_t)fpu_tmp; \
1041.2Sbriggs		fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \
1051.2Sbriggs	}
1061.1Sbriggs
1071.2Sbriggs#define	FPU_GET_CARRY(r) (r) = (!!fpu_carry)
1081.2Sbriggs#define	FPU_SET_CARRY(v) fpu_carry = ((v) != 0)
1091.1Sbriggs
1101.2Sbriggs#else
1111.1Sbriggs
1121.1Sbriggs/* set up for extended-precision arithemtic */
1131.6Sisaki#define	FPU_DECL_CARRY int fpu_tmp;
1141.1Sbriggs
1151.1Sbriggs/*
1161.1Sbriggs * We have three kinds of add:
1171.1Sbriggs *	add with carry:					  r = x + y + c
1181.1Sbriggs *	add (ignoring current carry) and set carry:	c'r = x + y + 0
1191.1Sbriggs *	add with carry and set carry:			c'r = x + y + c
1201.1Sbriggs * The macros use `C' for `use carry' and `S' for `set carry'.
1211.1Sbriggs * Note that the state of the carry is undefined after ADDC and SUBC,
1221.1Sbriggs * so if all you have for these is `add with carry and set carry',
1231.1Sbriggs * that is OK.
1241.1Sbriggs *
1251.1Sbriggs * The same goes for subtract, except that we compute x - y - c.
1261.1Sbriggs *
1271.1Sbriggs * Finally, we have a way to get the carry into a `regular' variable,
1281.1Sbriggs * or set it from a value.  SET_CARRY turns 0 into no-carry, nonzero
1291.1Sbriggs * into carry; GET_CARRY sets its argument to 0 or 1.
1301.1Sbriggs */
1311.2Sbriggs#define	FPU_ADDC(r, x, y)						\
1321.2Sbriggs	{								\
1331.5Sperry		__asm volatile("movel %1,%0" : "=d"(fpu_tmp) : "g"(x));	\
1341.5Sperry		__asm volatile("addxl %1,%0" : "=d"(fpu_tmp) : "d"(y));	\
1351.5Sperry		__asm volatile("movel %1,%0" : "=g"(r) : "r"(fpu_tmp));	\
1361.1Sbriggs	}
1371.2Sbriggs#define	FPU_ADDS(r, x, y)						\
1381.2Sbriggs	{								\
1391.5Sperry		__asm volatile("movel %1,%0" : "=d"(fpu_tmp) : "g"(x));	\
1401.5Sperry		__asm volatile("addl %1,%0" : "=d"(fpu_tmp) : "g"(y));	\
1411.5Sperry		__asm volatile("movel %1,%0" : "=g"(r) : "r"(fpu_tmp));	\
1421.1Sbriggs	}
1431.2Sbriggs#define	FPU_ADDCS(r, x, y) FPU_ADDC(r, x, y)
1441.2Sbriggs
1451.2Sbriggs#define	FPU_SUBC(r, x, y)						\
1461.2Sbriggs	{								\
1471.5Sperry		__asm volatile("movel %1,%0" : "=d"(fpu_tmp) : "g"(x));	\
1481.5Sperry		__asm volatile("subxl %1,%0" : "=d"(fpu_tmp) : "d"(y));	\
1491.5Sperry		__asm volatile("movel %1,%0" : "=g"(r) : "r"(fpu_tmp));	\
1501.1Sbriggs	}
1511.2Sbriggs#define	FPU_SUBS(r, x, y)						\
1521.2Sbriggs	{								\
1531.5Sperry		__asm volatile("movel %1,%0" : "=d"(fpu_tmp) : "g"(x));	\
1541.5Sperry		__asm volatile("subl %1,%0" : "=d"(fpu_tmp) : "g"(y));	\
1551.5Sperry		__asm volatile("movel %1,%0" : "=g"(r) : "r"(fpu_tmp));	\
1561.1Sbriggs	}
1571.2Sbriggs#define	FPU_SUBCS(r, x, y) FPU_SUBC(r, x, y)
1581.1Sbriggs
1591.2Sbriggs#define	FPU_GET_CARRY(r)				\
1601.2Sbriggs	{						\
1611.5Sperry		__asm volatile("moveq #0,%0" : "=d"(r));	\
1621.5Sperry		__asm volatile("addxl %0,%0" : "+d"(r));	\
1631.2Sbriggs	}
1641.2Sbriggs#define	FPU_SET_CARRY(v)						\
1651.2Sbriggs	{								\
1661.5Sperry		__asm volatile("moveq #0,%0" : "=d"(fpu_tmp));		\
1671.5Sperry		__asm volatile("subl %1,%0" : "=d"(fpu_tmp) : "g"(v));	\
1681.2Sbriggs	}
1691.1Sbriggs
1701.2Sbriggs#endif /* FPE_USE_ASM */
171