home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / Emulatory / AROS / utility / umult32.c < prev    next >
Encoding:
C/C++ Source or Header  |  1978-03-06  |  2.3 KB  |  99 lines

  1. /*
  2.     $Id: umult32.c,v 1.3 1996/10/24 22:51:47 aros Exp $
  3.     $Log: umult32.c,v $
  4.     Revision 1.3  1996/10/24 22:51:47  aros
  5.     Use proper Amiga datatypes (eg: ULONG not unsigned long)
  6.  
  7.     Revision 1.2  1996/10/24 15:51:39  aros
  8.     Use the official AROS macros over the __AROS versions.
  9.  
  10.     Revision 1.1  1996/08/31 12:58:13  aros
  11.     Merged in/modified for FreeBSD.
  12.  
  13.     Desc:
  14.     Lang: english
  15. */
  16. #include "utility_intern.h"
  17.  
  18. /*****************************************************************************
  19.  
  20.     NAME */
  21.         #include <clib/utility_protos.h>
  22.  
  23.         AROS_LH2(ULONG, UMult32,
  24.  
  25. /*  SYNOPSIS */
  26.         AROS_LHA(ULONG        , arg1, D0),
  27.         AROS_LHA(ULONG        , arg2, D1),
  28.  
  29. /*  LOCATION */
  30.         struct UtilityBase *, UtilityBase, 24, Utility)
  31.  
  32. /*  FUNCTION
  33.         Performs an unsigned 32-bit multiplication of arg1 * arg2 and
  34.         returns a 32 bit value.
  35.  
  36.     INPUTS
  37.         arg1, arg2  -   32 bit unsigned longs
  38.  
  39.     RESULT
  40.         arg1 * arg2
  41.  
  42.     NOTES
  43.         This can perform the multiplication either using the machines
  44.         native instructions (if they exist), or in software using a
  45.         simple algorithm (three multiplications, two shifts and
  46.         an addition.
  47.  
  48.     EXAMPLE
  49.  
  50.         LONG a = 352543;
  51.         LONG b = 52464;
  52.         LONG c = UMult32(a,b);
  53.         c == 1315946768
  54.  
  55.     BUGS
  56.  
  57.     SEE ALSO
  58.         utility/SMult32(), utility/UMult64(), utility/SMult64()
  59.  
  60.     INTERNALS
  61.         We are performing the operation:
  62.  
  63.  
  64.             (2^16 * a + b) * (2^16 * c + d)
  65.           = 2^32 * ab + 2^16 * ad + 2^16 * bc + bd
  66.           = 2^32 * ab + 2^16 ( ad + bc ) + bd
  67.  
  68.         Now since the result is a 32-bit number, the 2^32 term will have
  69.         no effect. (Since 2^32 > max (32-bit number).
  70.  
  71.         Therefore:
  72.         product = 2^16( ad + bc ) + bd
  73.  
  74.     HISTORY
  75.         29-10-95    digulla automatically created from
  76.                             utility_lib.fd and clib/utility_protos.h
  77.         18-08-96    iaint   Implemented as described above.
  78.  
  79. *****************************************************************************/
  80. {
  81.     AROS_LIBFUNC_INIT
  82.  
  83. #ifdef HAS_32BITMULU
  84.     return arg1 * arg2;
  85. #else
  86.  
  87.     UWORD a0, a1, b0, b1;
  88.  
  89.     a1 = (arg1 >> 16) & 0xffff;
  90.     a0 = arg1 & 0xffff;
  91.     b1 = (arg2 >> 16) & 0xffff;
  92.     b0 = arg2 & 0xffff;
  93.  
  94.     return (((a0 * b1) + (a1 * b0)) << 16) + (b0 * a0);
  95. #endif
  96.  
  97.     AROS_LIBFUNC_EXIT
  98. } /* UMult32 */
  99.