1/* $OpenBSD: mul.S,v 1.6 2005/08/07 16:40:15 espie Exp $ */ 2/* 3 * Copyright (c) 1992, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This software was developed by the Computer Systems Engineering group 7 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 8 * contributed to Berkeley. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35#include <machine/asm.h> 36 37RCSID("$MirOS: src/kern/c/sparc/mul.S,v 1.1 2008/08/01 18:31:03 tg Exp $") 38 39 .text 40 41/* 42 * Signed multiply, from Appendix E of the Sparc Version 8 43 * Architecture Manual. 44 * 45 * Returns %o0 * %o1 in %o1%o0 (i.e., %o1 holds the upper 32 bits of 46 * the 64-bit product). 47 * 48 * This code optimizes short (less than 13-bit) multiplies. 49 */ 50 51#ifndef STRONG_SPARC 52.weak .mul 53#else 54FUNC(__mul) 55#endif 56FUNC(.mul) 57 mov %o0, %y ! multiplier -> Y 58 andncc %o0, 0xfff, %g0 ! test bits 12..31 59 be Lmul_shortway ! if zero, can do it the short way 60 andcc %g0, %g0, %o4 ! zero the partial product and clear N and V 61 62 /* 63 * Long multiply. 32 steps, followed by a final shift step. 64 */ 65 mulscc %o4, %o1, %o4 ! 1 66 mulscc %o4, %o1, %o4 ! 2 67 mulscc %o4, %o1, %o4 ! 3 68 mulscc %o4, %o1, %o4 ! 4 69 mulscc %o4, %o1, %o4 ! 5 70 mulscc %o4, %o1, %o4 ! 6 71 mulscc %o4, %o1, %o4 ! 7 72 mulscc %o4, %o1, %o4 ! 8 73 mulscc %o4, %o1, %o4 ! 9 74 mulscc %o4, %o1, %o4 ! 10 75 mulscc %o4, %o1, %o4 ! 11 76 mulscc %o4, %o1, %o4 ! 12 77 mulscc %o4, %o1, %o4 ! 13 78 mulscc %o4, %o1, %o4 ! 14 79 mulscc %o4, %o1, %o4 ! 15 80 mulscc %o4, %o1, %o4 ! 16 81 mulscc %o4, %o1, %o4 ! 17 82 mulscc %o4, %o1, %o4 ! 18 83 mulscc %o4, %o1, %o4 ! 19 84 mulscc %o4, %o1, %o4 ! 20 85 mulscc %o4, %o1, %o4 ! 21 86 mulscc %o4, %o1, %o4 ! 22 87 mulscc %o4, %o1, %o4 ! 23 88 mulscc %o4, %o1, %o4 ! 24 89 mulscc %o4, %o1, %o4 ! 25 90 mulscc %o4, %o1, %o4 ! 26 91 mulscc %o4, %o1, %o4 ! 27 92 mulscc %o4, %o1, %o4 ! 28 93 mulscc %o4, %o1, %o4 ! 29 94 mulscc %o4, %o1, %o4 ! 30 95 mulscc %o4, %o1, %o4 ! 31 96 mulscc %o4, %o1, %o4 ! 32 97 mulscc %o4, %g0, %o4 ! final shift 98 99 ! If %o0 was negative, the result is 100 ! (%o0 * %o1) + (%o1 << 32)) 101 ! We fix that here. 102 103 tst %o0 104 bge 1f 105 rd %y, %o0 106 107 ! %o0 was indeed negative; fix upper 32 bits of result by subtracting 108 ! %o1 (i.e., return %o4 - %o1 in %o1). 109 retl 110 sub %o4, %o1, %o1 111 1121: 113 retl 114 mov %o4, %o1 115 116Lmul_shortway: 117 /* 118 * Short multiply. 12 steps, followed by a final shift step. 119 * The resulting bits are off by 12 and (32-12) = 20 bit positions, 120 * but there is no problem with %o0 being negative (unlike above). 121 */ 122 mulscc %o4, %o1, %o4 ! 1 123 mulscc %o4, %o1, %o4 ! 2 124 mulscc %o4, %o1, %o4 ! 3 125 mulscc %o4, %o1, %o4 ! 4 126 mulscc %o4, %o1, %o4 ! 5 127 mulscc %o4, %o1, %o4 ! 6 128 mulscc %o4, %o1, %o4 ! 7 129 mulscc %o4, %o1, %o4 ! 8 130 mulscc %o4, %o1, %o4 ! 9 131 mulscc %o4, %o1, %o4 ! 10 132 mulscc %o4, %o1, %o4 ! 11 133 mulscc %o4, %o1, %o4 ! 12 134 mulscc %o4, %g0, %o4 ! final shift 135 136 /* 137 * %o4 has 20 of the bits that should be in the low part of the 138 * result; %y has the bottom 12 (as %y's top 12). That is: 139 * 140 * %o4 %y 141 * +----------------+----------------+ 142 * | -12- | -20- | -12- | -20- | 143 * +------(---------+------)---------+ 144 * --hi-- ----low-part---- 145 * 146 * The upper 12 bits of %o4 should be sign-extended to form the 147 * high part of the product (i.e., highpart = %o4 >> 20). 148 */ 149 150 rd %y, %o5 151 sll %o4, 12, %o0 ! shift middle bits left 12 152 srl %o5, 20, %o5 ! shift low bits right 20, zero fill at left 153 or %o5, %o0, %o0 ! construct low part of result 154 retl 155 sra %o4, 20, %o1 ! ... and extract high part of result 156