home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / arch / 10816 < prev    next >
Encoding:
Internet Message Format  |  1992-11-17  |  7.8 KB

  1. Xref: sparky comp.arch:10816 comp.lang.misc:3729
  2. Newsgroups: comp.arch,comp.lang.misc
  3. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!usc!sol.ctr.columbia.edu!ira.uka.de!math.fu-berlin.de!unidui!rrz.uni-koeln.de!Germany.EU.net!Urmel.Informatik.RWTH-Aachen.DE!martin
  4. From: martin@math.rwth-aachen.de (  Martin Schoenert)
  5. Subject: Re: how to advocate new software/hardware features (Re: Hardware Support for Numeric Algorithms)
  6. Message-ID: <martin.721995695@bert>
  7. Sender: news@Urmel.Informatik.RWTH-Aachen.DE (Newsfiles Owner)
  8. Nntp-Posting-Host: bert.math.rwth-aachen.de
  9. Organization: Rechnerbetrieb Informatik  /  RWTH Aachen
  10. References: <TMB.92Nov14145619@pollux.idiap.ch> <Bxq7y0.IJ@mentor.cc.purdue.edu> <mwm.2n6z@contessa.palo-alto.ca.us> <Bxr8vG.IpI@mentor.cc.purdue.edu>
  11. Date: 17 Nov 92 10:21:35 GMT
  12. Lines: 168
  13.  
  14. Bob Silverman wrote in his article:
  15.  
  16.    Quite a few modern microprocessors have hardware to do 32 x 32 bit 
  17.    multiplies and 64 bit / 32 bit divides.  I know of no HLL that will
  18.    allow me to write code to access these instructions. For example,
  19.    suppose I want to compute A*B/C exactly, where A,B, C are 32 bit
  20.    ints and C > A and C > B.  How do I do this in a HLL ?
  21.  
  22. Alan Watson responded in his article:
  23.  
  24.     Not that I wish to defend C, but a good compiler should be able to
  25.     eliminate unnecessary casts and use mixed 32/64-bit arithmetic in the
  26.     following expressions:
  27.  
  28.     long l, n, m;
  29.     int i, j, k;
  30.  
  31.     l = (long) i * (long) j;    /* emit 32x32 -> 64 */
  32.     n = m / i;            /* emit 64/32 -> 64 */
  33.  
  34.     by use of the `as if' rule.  Perhaps there is a subtlety I have
  35.     missed.  I agree that the casts are a nuisance, and one can argue that
  36.     C's default promotions are perhaps not the best choice, but in these
  37.     cases the language does provide the degree of control that Silverman
  38.     requires.
  39.  
  40. This of course assumes that 'long' is 64  bits  wide.  On most commercial
  41. systems this just isn't  the case.  If  'sizeof(long) = sizeof(int)  = 4'
  42. you only get the lower  half of the product this way (and the upper  half
  43. is much more difficult to get).
  44.  
  45. Besides,  I  have collected some  information  about  the  multiplication
  46. instructions of modern micro processors.  From this it appears to me that
  47. on those systems where 'sizeof(long)'  might be 8 (64 bits) you also have
  48. a 64x64 -> 128 instruction (or short sequence of instructions).  And then
  49. you loose  again because you  cannot get at the upper half  of such a 128
  50. bit product.
  51.  
  52. Thomas Breuel responded in his article:
  53.  
  54.     Well, then you simply haven't looked very far. Many implementations
  55.     of HLL let you access those instructions in a variety of ways.
  56.  
  57.     (1) Implementations like GNU C/C++ and implementations of CommonLisp
  58.         and Scheme often have very sophisticated and efficient assembly
  59.         language interfaces. Of course, those interfaces are not portable,
  60.         but neither is the assembly language (I myself would like to see 
  61.         vendors standardize on the GNU C/C++ assembly language interface).
  62.  
  63. Yes  that is  true, and I know of  at  least one large integer arithmetic
  64. which  uses  GNU C to  get at the  powerful  multiplication instructions.
  65. However, the assembler  interface  is  not  part of the  definition of  C
  66. (neither ANSI  or K&R say something  about  it).  So it is true that  the
  67. *language*   does  allow  you   to   do   this   (even   though   special
  68. *implementations* might).
  69.  
  70. Thomas Breuel continues:
  71.  
  72.     (3) Many programming languages let you declare 32 and 64bit
  73.         integers explicitly and good compilers will select the right
  74.         instruction to go with the types (Ada and CommonLisp are
  75.         among those languages).
  76.  
  77.         Even the C language has provisions for just the case that you are
  78.         concerned with in the case of 16bit integers, 32bit longs:
  79.  
  80.         int a = ...;
  81.             int b = ...;
  82.             int c = ...;
  83.         long product = a*b;
  84.         int result = product/c;
  85.  
  86.         In fact, on some machines, int is 32bit and long is 64bit,
  87.         so this code will do what you want.
  88.  
  89. Ahh, but the 16x16->32 bit multiplication is not as good.  Suppose that I
  90. want to multiply two large integers, each 32 bytes long.  Using 16x16->32
  91. bit multiplication I  need 256 such  instructions.   Using 32x32->64  bit
  92. multiplication I  only need 64 such instructions.  Since on  most  modern
  93. processors both  instructions take  about  the same  amount  of  time the
  94. latter is about 4 times as fast.  (If you have an application  that  runs
  95. for MIPS years, a factor of 4 is quite a big deal ;-)
  96.  
  97. Thomas Breuel continues:
  98.  
  99.         Furthermore, C is being extended to allow access to 64bit types,
  100.         so that you can write:
  101.  
  102.         int a = ...;
  103.         int b = ...;
  104.         long long product = a*b;
  105.         int result = product/c;
  106.  
  107. This  may  be  so,  but it  isn't  really  relevant.   Bob  talked  about
  108. *languages*,  you  talk about  *implementation* or  possible *extension*.
  109. This is not the same thing.
  110.  
  111. Herman Rubin writes in his article:
  112.  
  113.    In addition, this has architectural implications.  Hardware
  114.    designers seem to avoid asking users what instructions they want in
  115.    hardware, but look at the current HLLs.  Having nxn ->2n [32bit x
  116.    32bit multiplication yielding a 64bit result] in the language, and
  117.    2n/n in the language, is far more likely to keep it in the
  118.    architecture than Bob using it in his programs.
  119.  
  120. Well  I  don't  know.   From  my  information  about  the  multiplication
  121. instructions it appears  that  almost all modern  processors have nxn->2n
  122. instructions, even though they *cannot* be used from C (which seems to be
  123. the HLL that architects are most concerned with).
  124.  
  125. Herman Rubin writes in another article:
  126.  
  127.     >(3) Many programming languages let you declare 32 and 64bit
  128.     >    integers explicitly and good compilers will select the right
  129.     >    instruction to go with the types (Ada and CommonLisp are
  130.     >    among those languages).
  131.  
  132.     >    Even the C language has provisions for just the case that you are
  133.     >    concerned with in the case of 16bit integers, 32bit longs:
  134.  
  135.     >    int a = ...;
  136.     >    int b = ...;
  137.     >    int c = ...;
  138.     >    long product = a*b;
  139.     >    int result = product/c;
  140.  
  141.     As I read the language description, types are cast either before
  142.     or after the operation.  I know of no operations in the C language
  143.     which explicitly allow for different types of arguments/results
  144.     except for comparisons, with Boolean results.  And those Booleans
  145.     are not very flexible.  As Bob Silverman wrote, he would like to
  146.     use a different operation symbol for the situation.
  147.  
  148. Let us use a slightly different example (so  that we avoid the  issues of
  149. overflow and sign handling).
  150.  
  151.     unsigned short a, b;        /* assume 16 bits */
  152.     unsigned long  c;           /* assume 32 bits */
  153.     c = (unsigned long) a * (unsigned long) b;
  154.  
  155. The ANSI definition specifies that the  result (c) shall be the value one
  156. gets when one does the following
  157.  
  158.     extend a to 32 bits
  159.     extend b to 32 bits
  160.     multiply a and b to give a 64 bit result with a 32x32->64 instruction
  161.     shorten the result to 32 bits
  162.  
  163. However,  ANSI does  *not* specify how  the produced  object  code  shall
  164. obtain this result.  (As far as ANSI is concerned it  would be all  right
  165. to e-mail the operands to another machine, have the other machine compute
  166. the result and e-mail it back ;-) So because the following sequence would
  167. give the same result as the one above, it would be absolutly legal for an
  168. ANSI compiler to produce this code
  169.  
  170.     multiply a and b to give a 32 bit result with a 16x16->32 instruction
  171.  
  172. And yes, I have seen compilers produce such code.  (It doesn't help us of
  173. course, because we  really want  to use the 32x32->64 bit instruction and
  174. are usually missing the 64 bit integer type to hold the result).
  175.  
  176. Martin.
  177.  
  178. -- .- .-. - .. -.  .-.. --- ...- . ...  .- -. -. .. -.- .-
  179. Martin Sch"onert,   Martin.Schoenert@Math.RWTH-Aachen.DE,  +49 241 804551
  180. Lehrstuhl D f"ur Mathematik, Templergraben 64, RWTH, D 51 Aachen, Germany
  181.  
  182.