home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / UUCODE / UUPC / TEST / UPC12ES1.ZIP / LIB / arbmath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-11  |  4.7 KB  |  124 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    a r b m a t h . c                                               */
  3. /*                                                                    */
  4. /*    Arbitary length math routines for UUPC/extended                 */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  9. /*       Wonderworks.                                                 */
  10. /*                                                                    */
  11. /*       All rights reserved except those explicitly granted by       */
  12. /*       the UUPC/extended license agreement.                         */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: arbmath.c 1.2 1993/10/09 15:46:15 rhg Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: arbmath.c $
  24.  *     Revision 1.2  1993/10/09  15:46:15  rhg
  25.  *     ANSIify the source
  26.  *
  27.  */
  28.  
  29. /*--------------------------------------------------------------------*/
  30. /*                     Standard library includes                      */
  31. /*--------------------------------------------------------------------*/
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35.  
  36. /*--------------------------------------------------------------------*/
  37. /*                    UUPC/extended include files                     */
  38. /*--------------------------------------------------------------------*/
  39.  
  40. #include "lib.h"
  41. #include "arbmath.h"
  42.  
  43. /*--------------------------------------------------------------------*/
  44. /*                          Global variables                          */
  45. /*--------------------------------------------------------------------*/
  46.  
  47. currentfile();
  48.  
  49. /*--------------------------------------------------------------------*/
  50. /*    a d i v                                                         */
  51. /*                                                                    */
  52. /*    Perform arbitary length division                                */
  53. /*                                                                    */
  54. /*    Returns true if input number was non-zero                       */
  55. /*--------------------------------------------------------------------*/
  56.  
  57. boolean adiv( unsigned char *number,
  58.              const unsigned divisor,
  59.              unsigned *remain,
  60.              const unsigned digits)
  61. {
  62.    unsigned subscript;
  63.    boolean nonzero = FALSE;
  64.    *remain = 0;
  65.  
  66.    for ( subscript = 0; subscript < digits; subscript++)
  67.    {
  68.       unsigned digit =  *remain * 0x100 + number[subscript];
  69.       nonzero = nonzero || number[subscript];
  70.       *remain = digit % divisor;
  71.       number[subscript] =  (unsigned char) (digit / divisor);
  72.    } /* for */
  73.    return nonzero;
  74. } /* div */
  75.  
  76. /*--------------------------------------------------------------------*/
  77. /*    m u l t                                                         */
  78. /*                                                                    */
  79. /*    Perform arbitary length multiplication                          */
  80. /*--------------------------------------------------------------------*/
  81.  
  82. void mult(unsigned char *number,
  83.       const unsigned range,
  84.       const unsigned digits)
  85. {
  86.    unsigned subscript = digits;
  87.    unsigned carry = 0;
  88.  
  89.    while( subscript-- > 0)
  90.    {
  91.       unsigned digit = number[subscript] * range + carry;
  92.       number[subscript] = (unsigned char) (digit % 0x100);
  93.       carry = digit / 0x100;
  94.    } /* while */
  95.  
  96.    if ( carry != 0 )     /* Big trouble if overflow occurs   */
  97.       panic();
  98. } /* mult */
  99.  
  100. /*--------------------------------------------------------------------*/
  101. /*    a d d                                                           */
  102. /*                                                                    */
  103. /*    Perform arbitiary length addition                               */
  104. /*--------------------------------------------------------------------*/
  105.  
  106. void add(unsigned char *number,
  107.       const unsigned range,
  108.       const unsigned digits)
  109. {
  110.    unsigned subscript = digits;
  111.    unsigned carry = range;
  112.  
  113.    while(( carry > 0) && ( subscript-- > 0))
  114.    {
  115.       unsigned digit = number[subscript] + carry;
  116.       number[subscript] = (unsigned char) (digit % 0x100);
  117.       carry = digit / 0x100;
  118.    } /* while */
  119.  
  120.    if ( carry != 0 )     /* Big trouble if overflow occurs   */
  121.       panic();
  122.  
  123. } /* add */
  124.