home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / rom / utility / smult64.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-03  |  3.1 KB  |  125 lines

  1. /*
  2.     Copyright (C) 1995-1997 AROS - The Amiga Replacement OS
  3.     $Id: smult64.c,v 1.7 1997/02/03 02:58:32 ldp Exp $
  4.  
  5.     Desc: Signed 64 bit multiplication function.
  6.     Lang: english
  7. */
  8. #include "utility_intern.h"
  9.  
  10. /*****************************************************************************
  11.  
  12.     NAME */
  13. #include <proto/utility.h>
  14.  
  15.         AROS_LH2(QUAD, SMult64,
  16.  
  17. /*  SYNOPSIS */
  18.         AROS_LHA(LONG, arg1, D0),
  19.         AROS_LHA(LONG, arg2, D1),
  20.  
  21. /*  LOCATION */
  22.         struct UtilityBase *, UtilityBase, 33, Utility)
  23.  
  24. /*  FUNCTION
  25.         Compute the signed 64-bit product of arg1 * arg2.
  26.  
  27.     INPUTS
  28.         arg1, arg2  -   32 bit signed numbers.
  29.  
  30.     RESULT
  31.         arg1 * arg2
  32.  
  33.     NOTES
  34.         For m68k assembly programmers, QUADs are returned in D0:D1 (with
  35.         the high 32 bits in D0).
  36.  
  37.         The utility.library math functions are unlike all other utility
  38.         functions in that they don't require the library base to be
  39.         loaded in register A6, and they also save the values of the
  40.         address registers A0/A1.
  41.  
  42.         This function is mainly to support assembly programers, and is
  43.         probably of limited use to higher-level language programmers.
  44.  
  45.     EXAMPLE
  46.  
  47.     BUGS
  48.  
  49.     SEE ALSO
  50.         utility/SMult32(), utility/UMult32(), utility/UMult64()
  51.  
  52.     INTERNALS
  53.         Actually handled in config/$(KERNEL)/utility_math.s
  54.  
  55.         This is essentially SMult32(), but with the code to calculate
  56.         the product of the high 32 bits of the multiplicands.
  57.  
  58.         In fact all that is added is the 2^32 * ac term (see docs for
  59.             SMult32().)
  60.  
  61.     HISTORY
  62.         29-10-95    digulla automatically created from
  63.                             utility_lib.fd and clib/utility_protos.h
  64.         18-08-96    iaint   Modified SMult32().
  65.  
  66. *****************************************************************************/
  67. {
  68.     AROS_LIBFUNC_INIT
  69.  
  70.     /* If we have native support for 32 * 32 -> 64, use that.
  71.        The compiler will complain if it cannot support QUAD's
  72.        natively (GNU C can)
  73.      */
  74.  
  75.     return arg1 * arg2;
  76.  
  77. #if 0
  78.     /* This is partially the algoritm that is used, however for a
  79.        more complete version see config/m68k-native/smult64.s
  80.  
  81.        This version has problems with:
  82.         - adding the partial products together
  83.         - setting the value of QUADs
  84.     */
  85.  
  86.     QUAD product;
  87.     WORD a0, a1, b0, b1;
  88.     LONG part_prod;
  89.     BOOL neg;
  90.  
  91.     /* Fix everything up so that -ve signs don't vanish */
  92.     if(arg1 < 0)
  93.     {
  94.         neg = 1; arg1 = -arg1;
  95.     }
  96.     else
  97.         neg = 0;
  98.  
  99.     if(arg2 <= 0)
  100.     {
  101.         neg ^= 1; arg2 = -arg2;
  102.     }
  103.  
  104.     a0 = arg1 & 0xFFFF;
  105.     a1 = (arg1 >> 16) & 0xFFFF;
  106.  
  107.     b0 = arg2 & 0xFFFF;
  108.     b1 = (arg2 >> 16) & 0xFFFF;
  109.  
  110.     part_prod = (a0 * b1) + (a1 * b0);
  111.  
  112.     /* In case numbers are small - note, does NOT compile */
  113.     if(a1 && b1)
  114.         SET_HIGH32OF64(product, a1 * b1 + (part_prod >> 16));
  115.     else
  116.         SET_HIGH32OF64(product, (part_prod >> 16));
  117.  
  118.     SET_LOW32OF64(product, (part_prod & 0xFFFF) + (a0 * b0));
  119.  
  120.     return (neg ? NEG64(product) : product);
  121. #endif
  122.  
  123.     AROS_LIBFUNC_EXIT
  124. } /* SMult64 */
  125.