/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: a_mul_.c,v 1.2 1997/09/25 16:52:29 ae59 Exp $ */
/*
$Log: a_mul_.c,v $
 * Revision 1.2  1997/09/25  16:52:29  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:20:47  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : a_mul_.c                              */
/*                                                              */
/*      Entries         : a_intg a_mul_(a,b)                    */
/*                        a_intg a;                             */
/*                        a_intg b;                             */
/*                                                              */
/*      Arguments       : a = factor of multiplication          */
/*                        b = factor of multiplication          */
/*                                                              */
/*      Description     : Multiplication of integer values      */
/*                                                              */
/*      Note            : 0 returned in case of error           */
/*                                                              */
/*      Date            : 1991-11-06                            */
/*      1991-11-06 : sign of product corrected                  */
/*      1991-01-29 : type of f changed to a_btyp                */
/*      1990-12-18 : explicit compare                           */
/*      1990-11-15 : E_TPUSH+E_TPOPP                            */
/*      1990-10-24 : NO_ERROR replaced by OVERFLOW              */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
#endif

#ifdef LINT_ARGS
local a_intg a_mul_(a_intg a,a_intg b)
#else
local a_intg a_mul_(a,b)

a_intg a;
a_intg b;
#endif
        {
        a_intg res,g;
        a_btyp f;

        E_TPUSH("a_mul_")

        if (a==0 || b==0) res = 0;
        else if (a==1) res = b;
        else if (b==1) res = a;
        else if (a!=MININT && b!=MININT)
           {
           res = ((a^b)<0) ? -1 : 1;
           g = (a<0) ? -a : a;
           f = (a_btyp)((b<0) ? -b : b);

           if (res==1)
              {
              res = (f & 1) ? g : 0;
              while ((f >>= 1)!=0)
                 {
                 if (MAXINT-g<g) break;
                 if (MAXINT-(g <<= 1)<res) break;
                 if (f & 1) res += g;
                 }
              }
           else
              {
              res = (f & 1) ? -g : 0;
              while ((f >>= 1)!=0)
                 {
                 if (MAXINT-g<g) break;
                 if (MININT+(g <<= 1)>res) break;
                 if (f & 1) res -= g;
                 }
              }
           if (f!=0)
              {
              e_trap(OVERFLOW,6,E_TMSG,15,E_TINT+E_TEXT(1),&a,
                                          E_TINT+E_TEXT(2),&b);
              res = 0;
              }
           }
        else
           {
           e_trap(OVERFLOW,6,E_TMSG,15,E_TINT+E_TEXT(1),&a,
                                       E_TINT+E_TEXT(2),&b);
           res = 0;
           }

        E_TPOPP("a_mul_")
        return(res);
        }

