Home | History | Annotate | Line # | Download | only in spmath
divsim.c revision 1.1
      1 /*	$NetBSD: divsim.c,v 1.1 2002/06/05 01:04:24 fredette Exp $	*/
      2 
      3 /*	$OpenBSD: divsim.c,v 1.4 2001/03/29 03:58:18 mickey Exp $	*/
      4 
      5 /*
      6  * Copyright 1996 1995 by Open Software Foundation, Inc.
      7  *              All Rights Reserved
      8  *
      9  * Permission to use, copy, modify, and distribute this software and
     10  * its documentation for any purpose and without fee is hereby granted,
     11  * provided that the above copyright notice appears in all copies and
     12  * that both the copyright notice and this permission notice appear in
     13  * supporting documentation.
     14  *
     15  * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
     16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     17  * FOR A PARTICULAR PURPOSE.
     18  *
     19  * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
     20  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
     21  * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
     22  * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
     23  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     24  *
     25  */
     26 /*
     27  * pmk1.1
     28  */
     29 /*
     30  * (c) Copyright 1986 HEWLETT-PACKARD COMPANY
     31  *
     32  * To anyone who acknowledges that this file is provided "AS IS"
     33  * without any express or implied warranty:
     34  *     permission to use, copy, modify, and distribute this file
     35  * for any purpose is hereby granted without fee, provided that
     36  * the above copyright notice and this notice appears in all
     37  * copies, and that the name of Hewlett-Packard Company not be
     38  * used in advertising or publicity pertaining to distribution
     39  * of the software without specific, written prior permission.
     40  * Hewlett-Packard Company makes no representations about the
     41  * suitability of this software for any purpose.
     42  */
     43 
     44 
     45 #include "md.h"
     46 
     47 void
     48 divsim(opnd1,opnd2,result)
     49 
     50 int opnd1, opnd2;
     51 struct mdsfu_register *result;
     52 {
     53 	int sign, op1_sign;
     54 
     55 	/* check divisor for zero */
     56 	if (opnd2 == 0) {
     57 		overflow = TRUE;
     58 		return;
     59 	}
     60 
     61 	/* get sign of result */
     62 	sign = opnd1 ^ opnd2;
     63 
     64 	/* get absolute value of operands */
     65 	if (opnd1 < 0) {
     66 		opnd1 = -opnd1;
     67 		op1_sign = TRUE;
     68 	}
     69 	else op1_sign = FALSE;
     70 	if (opnd2 < 0) opnd2 = -opnd2;
     71 
     72 	/* check for opnd2 == -2**31 */
     73 	if (opnd2 < 0) {
     74 		if (opnd1 == opnd2) {
     75 			result_hi = 0;	/* remainder = 0 */
     76 			result_lo = 1;
     77 		}
     78 		else {
     79 			result_hi = opnd1;	/* remainder = opnd1 */
     80 			result_lo = 0;
     81 		}
     82 	}
     83 	else {
     84 		/* do the divide */
     85 		divu(0,opnd1,opnd2,result);
     86 
     87 		/*
     88 		 * check for overflow
     89 		 *
     90 		 * at this point, the only way we can get overflow
     91 		 * is with opnd1 = -2**31 and opnd2 = -1
     92 		 */
     93 		if (sign>0 && result_lo<0) {
     94 			overflow = TRUE;
     95 			return;
     96 		}
     97 	}
     98 	overflow = FALSE;
     99 
    100 	/* return positive residue */
    101 	if (op1_sign && result_hi) {
    102 		result_hi = opnd2 - result_hi;
    103 		if (++result_lo < 0) {
    104 			overflow = TRUE;
    105 			return;
    106 		}
    107 	}
    108 
    109 	/* return appropriately signed result */
    110 	if (sign<0) result_lo = -result_lo;
    111 	return;
    112 }
    113