1 1.4 perry /* $NetBSD: fpu_arith.h,v 1.4 2005/12/24 20:07:28 perry Exp $ */ 2 1.1 simonb 3 1.1 simonb /* 4 1.1 simonb * Copyright (c) 1992, 1993 5 1.1 simonb * The Regents of the University of California. All rights reserved. 6 1.1 simonb * 7 1.1 simonb * This software was developed by the Computer Systems Engineering group 8 1.1 simonb * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 1.1 simonb * contributed to Berkeley. 10 1.1 simonb * 11 1.1 simonb * All advertising materials mentioning features or use of this software 12 1.1 simonb * must display the following acknowledgement: 13 1.1 simonb * This product includes software developed by the University of 14 1.1 simonb * California, Lawrence Berkeley Laboratory. 15 1.1 simonb * 16 1.1 simonb * Redistribution and use in source and binary forms, with or without 17 1.1 simonb * modification, are permitted provided that the following conditions 18 1.1 simonb * are met: 19 1.1 simonb * 1. Redistributions of source code must retain the above copyright 20 1.1 simonb * notice, this list of conditions and the following disclaimer. 21 1.1 simonb * 2. Redistributions in binary form must reproduce the above copyright 22 1.1 simonb * notice, this list of conditions and the following disclaimer in the 23 1.1 simonb * documentation and/or other materials provided with the distribution. 24 1.2 agc * 3. Neither the name of the University nor the names of its contributors 25 1.1 simonb * may be used to endorse or promote products derived from this software 26 1.1 simonb * without specific prior written permission. 27 1.1 simonb * 28 1.1 simonb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 1.1 simonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 1.1 simonb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 1.1 simonb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 1.1 simonb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 1.1 simonb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 1.1 simonb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 1.1 simonb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 1.1 simonb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 1.1 simonb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 1.1 simonb * SUCH DAMAGE. 39 1.1 simonb * 40 1.1 simonb * @(#)fpu_arith.h 8.1 (Berkeley) 6/11/93 41 1.1 simonb */ 42 1.1 simonb 43 1.1 simonb /* 44 1.1 simonb * Extended-precision arithmetic. 45 1.1 simonb * 46 1.1 simonb * We hold the notion of a `carry register', which may or may not be a 47 1.1 simonb * machine carry bit or register. On the SPARC, it is just the machine's 48 1.1 simonb * carry bit. 49 1.1 simonb * 50 1.1 simonb * In the worst case, you can compute the carry from x+y as 51 1.1 simonb * (unsigned)(x + y) < (unsigned)x 52 1.1 simonb * and from x+y+c as 53 1.1 simonb * ((unsigned)(x + y + c) <= (unsigned)x && (y|c) != 0) 54 1.1 simonb * for example. 55 1.1 simonb */ 56 1.1 simonb 57 1.1 simonb 58 1.1 simonb #ifndef FPE_USE_ASM 59 1.1 simonb 60 1.1 simonb /* set up for extended-precision arithemtic */ 61 1.1 simonb #define FPU_DECL_CARRY quad_t fpu_carry, fpu_tmp; 62 1.1 simonb 63 1.1 simonb /* 64 1.1 simonb * We have three kinds of add: 65 1.1 simonb * add with carry: r = x + y + c 66 1.1 simonb * add (ignoring current carry) and set carry: c'r = x + y + 0 67 1.1 simonb * add with carry and set carry: c'r = x + y + c 68 1.1 simonb * The macros use `C' for `use carry' and `S' for `set carry'. 69 1.1 simonb * Note that the state of the carry is undefined after ADDC and SUBC, 70 1.1 simonb * so if all you have for these is `add with carry and set carry', 71 1.1 simonb * that is OK. 72 1.1 simonb * 73 1.1 simonb * The same goes for subtract, except that we compute x - y - c. 74 1.1 simonb * 75 1.1 simonb * Finally, we have a way to get the carry into a `regular' variable, 76 1.1 simonb * or set it from a value. SET_CARRY turns 0 into no-carry, nonzero 77 1.1 simonb * into carry; GET_CARRY sets its argument to 0 or 1. 78 1.1 simonb */ 79 1.1 simonb #define FPU_ADDC(r, x, y) \ 80 1.1 simonb (r) = (x) + (y) + (!!fpu_carry) 81 1.1 simonb #define FPU_ADDS(r, x, y) \ 82 1.1 simonb { \ 83 1.1 simonb fpu_tmp = (quad_t)(x) + (quad_t)(y); \ 84 1.1 simonb (r) = (u_int)fpu_tmp; \ 85 1.1 simonb fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \ 86 1.1 simonb } 87 1.1 simonb #define FPU_ADDCS(r, x, y) \ 88 1.1 simonb { \ 89 1.1 simonb fpu_tmp = (quad_t)(x) + (quad_t)(y) + (!!fpu_carry); \ 90 1.1 simonb (r) = (u_int)fpu_tmp; \ 91 1.1 simonb fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \ 92 1.1 simonb } 93 1.1 simonb #define FPU_SUBC(r, x, y) \ 94 1.1 simonb (r) = (x) - (y) - (!!fpu_carry) 95 1.1 simonb #define FPU_SUBS(r, x, y) \ 96 1.1 simonb { \ 97 1.1 simonb fpu_tmp = (quad_t)(x) - (quad_t)(y); \ 98 1.1 simonb (r) = (u_int)fpu_tmp; \ 99 1.1 simonb fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \ 100 1.1 simonb } 101 1.1 simonb #define FPU_SUBCS(r, x, y) \ 102 1.1 simonb { \ 103 1.1 simonb fpu_tmp = (quad_t)(x) - (quad_t)(y) - (!!fpu_carry); \ 104 1.1 simonb (r) = (u_int)fpu_tmp; \ 105 1.1 simonb fpu_carry = ((fpu_tmp & 0xffffffff00000000LL) != 0); \ 106 1.1 simonb } 107 1.1 simonb 108 1.1 simonb #define FPU_GET_CARRY(r) (r) = (!!fpu_carry) 109 1.1 simonb #define FPU_SET_CARRY(v) fpu_carry = ((v) != 0) 110 1.1 simonb 111 1.1 simonb #else 112 1.1 simonb /* set up for extended-precision arithemtic */ 113 1.1 simonb #define FPU_DECL_CARRY 114 1.1 simonb 115 1.1 simonb /* 116 1.1 simonb * We have three kinds of add: 117 1.1 simonb * add with carry: r = x + y + c 118 1.1 simonb * add (ignoring current carry) and set carry: c'r = x + y + 0 119 1.1 simonb * add with carry and set carry: c'r = x + y + c 120 1.1 simonb * The macros use `C' for `use carry' and `S' for `set carry'. 121 1.1 simonb * Note that the state of the carry is undefined after ADDC and SUBC, 122 1.1 simonb * so if all you have for these is `add with carry and set carry', 123 1.1 simonb * that is OK. 124 1.1 simonb * 125 1.1 simonb * The same goes for subtract, except that we compute x - y - c. 126 1.1 simonb * 127 1.1 simonb * Finally, we have a way to get the carry into a `regular' variable, 128 1.1 simonb * or set it from a value. SET_CARRY turns 0 into no-carry, nonzero 129 1.1 simonb * into carry; GET_CARRY sets its argument to 0 or 1. 130 1.1 simonb */ 131 1.1 simonb #define FPU_ADDC(r, x, y) \ 132 1.4 perry __asm volatile("adde %0,%1,%2" : "=r"(r) : "r"(x), "r"(y)) 133 1.1 simonb #define FPU_ADDS(r, x, y) \ 134 1.4 perry __asm volatile("addc %0,%1,%2" : "=r"(r) : "r"(x), "r"(y)) 135 1.1 simonb #define FPU_ADDCS(r, x, y) \ 136 1.4 perry __asm volatile("adde %0,%1,%2" : "=r"(r) : "r"(x), "r"(y)) 137 1.1 simonb #define FPU_SUBC(r, x, y) \ 138 1.4 perry __asm volatile("subfe %0,%2,%1" : "=r"(r) : "r"(x), "r"(y)) 139 1.1 simonb #define FPU_SUBS(r, x, y) \ 140 1.4 perry __asm volatile("subfc %0,%2,%1" : "=r"(r) : "r"(x), "r"(y)) 141 1.1 simonb #define FPU_SUBCS(r, x, y) \ 142 1.4 perry __asm volatile("subfe %0,%2,%1" : "=r"(r) : "r"(x), "r"(y)) 143 1.1 simonb 144 1.4 perry #define FPU_GET_CARRY(r) __asm volatile("li %0,0; addie %0,%0,0" : "=r"(r)) 145 1.1 simonb /* This one needs to destroy a temp register. */ 146 1.1 simonb #define FPU_SET_CARRY(v) do { int __tmp; \ 147 1.4 perry __asm volatile("addic %0,%0,-1" : "r"(__tmp) : "r"(v)); \ 148 1.1 simonb } while (0) 149 1.1 simonb 150 1.1 simonb #define FPU_SHL1_BY_ADD /* shift left 1 faster by ADDC than (a<<1)|(b>>31) */ 151 1.1 simonb #endif 152