home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume8 / libhoward / part05 / smp2ul.c < prev   
C/C++ Source or Header  |  1989-10-01  |  3KB  |  118 lines

  1. /*
  2.  * smp2ul - convert simple multiple precision number to unsigned long
  3.  */
  4.  
  5. #ifndef lint
  6. static char _cpyrgt[] = "Copyright 1989 Howard Lee Gayle";
  7. #endif lint
  8.  
  9. /*
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License version 1,
  12.  * as published by the Free Software Foundation.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <howard/port.h>
  26. #include <howard/version.h>
  27.  
  28. MODVER ("@(#)$Header: smp2ul.c,v 1.7 89/08/15 11:14:04 howard Exp $");
  29.  
  30. #include <errno.h>
  31. #include <limits.h>
  32. #include <howard/registers.i>
  33. #include <howard/simultipre.i>
  34. #include <howard/smp.h>
  35.  
  36. PUBLIC int smp2ul (sp, lp)
  37. R1 smpT   *sp; /* Points to SMP.*/
  38. R5 ulongT *lp; /* Points to where to store result.*/
  39.  
  40. /* Function:
  41.  *    
  42.  * Algorithm:
  43.  *    Step downward through higher order SMP elements that ought to
  44.  *    be zero, and make sure they are.  Make sure high order bits in
  45.  *    the highest order element are also zero.  Or each element into
  46.  *    the result.  Check for a negative result.  Store the result.
  47.  * Returns:
  48.  *    
  49.  * Notes:
  50.  *    
  51.  */
  52. {
  53. R4 ulongT  res = 0; /* Result.*/
  54. R2 smpElT *p1;      /* Steps through sp->smp_el[].*/
  55. R3 smpElT *p2;      /* Highest order smp_el used..*/
  56.  
  57. if (((smpT *) NULL) == sp) return (EINVAL);
  58. p2 = &sp->smp_el[SMPNUL - 1];
  59. for (p1 = &sp->smp_el[SMPNEL - 1]; p1 != p2;)
  60.    if (0 != *p1--) return (ERANGE);
  61. if (0 != (*p1 >> SMPULHB)) return (ERANGE);
  62. while (p1 >= sp->smp_el)
  63.    res = (res << SMPEB) | *p1--;
  64. if (sp->smp_neg && (0 != res)) return (ERANGE);
  65. if (((ulongT *) NULL) != lp) *lp = res;
  66. return (SUCCESS);
  67. }
  68.  
  69. #ifdef TEST
  70. #include <howard/usage.h>
  71.  
  72. MAINVER ("@(#)$Header: smp2ul.c,v 1.7 89/08/15 11:14:04 howard Exp $");
  73. USAGE ("");
  74.  
  75. #include <howard/malf.h>
  76.  
  77. PRIVATE void t (num, neg, hi, lo, xrc, xres)
  78. int num; /* Test number.*/
  79. boolT neg; /* smp_neg */
  80. smpElT hi; /* smp_el[1] */
  81. smpElT lo; /* smp_el[0] */
  82. int xrc; /* Expected return code.*/
  83. ulongT xres; /* Expected result.*/
  84. {
  85. int rc; /* Return code.*/
  86. ulongT res; /* Result stored here.*/
  87. smpT smp; /* SMP argument.*/
  88.  
  89. smp.smp_neg = neg;
  90. smp.smp_el[0] = lo;
  91. smp.smp_el[1] = hi;
  92. rc = smp2ul (&smp, &res);
  93. if (rc != xrc) PRINTF ("%d: rc %d expected %d\n", num, rc, xrc);
  94. if ((0 == rc) && (0 == xrc) && (res != xres))
  95.    PRINTF ("%d: res %lX expected %lX\n", num, res, xres);
  96. }
  97.  
  98. PUBLIC int main ()
  99. {
  100. /* num    sgn   hi      lo      rc         res   */
  101. t(__LINE__, 0,      0,      0,      0, (ulongT)           0);
  102. t(__LINE__, 1,      0,      0,      0, (ulongT)           0);
  103. t(__LINE__, 0,      0,      1,      0, (ulongT)           1);
  104. t(__LINE__, 0, 0x7FFF, 0xFFFF,      0, (ulongT)  0x7FFFFFFF);
  105. t(__LINE__, 0, 0x8000,      0,      0, (ulongT)  0x80000000);
  106. t(__LINE__, 0, 0xFFFF, 0xFFFF,      0, (ulongT)  0xFFFFFFFF);
  107. t(__LINE__, 0,0x10000, 0x0000, ERANGE, (ulongT)           0);
  108. t(__LINE__, 1,      0,      1, ERANGE, (ulongT)           0);
  109.  
  110. mfflush (stdout, "Standard Output");
  111. exit (SUCCESS);
  112.  
  113. #ifdef lint
  114. return (SUCCESS);
  115. #endif
  116. }
  117. #endif
  118.