home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / arch / 9359 < prev    next >
Encoding:
Internet Message Format  |  1992-09-11  |  2.3 KB

  1. Xref: sparky comp.arch:9359 sci.math:11264 comp.lang.misc:3024 comp.lang.fortran:3518 comp.edu:1624
  2. Path: sparky!uunet!mcsun!sunic!dkuug!diku!thorinn
  3. From: thorinn@diku.dk (Lars Henrik Mathiesen)
  4. Newsgroups: comp.arch,sci.math,comp.lang.misc,comp.lang.fortran,comp.edu
  5. Subject: Re: Scientists as Programmers (was Re: Small Language Wanted)
  6. Message-ID: <1992Sep9.215657.18399@odin.diku.dk>
  7. Date: 9 Sep 92 21:56:57 GMT
  8. References: <92Sep1.205116edt.669@neuron.ai.toronto.edu> <1992Sep4.162938.29237@dscomsf.desy.de> <Bu2r42.5xr@mentor.cc.purdue.edu> <id.I12T.GP4@ferranti.com>
  9. Sender: thorinn@tyr.diku.dk
  10. Organization: Department of Computer Science, U of Copenhagen
  11. Lines: 44
  12.  
  13. >hrubin@pop.stat.purdue.edu (Herman Rubin) writes:
  14. >> There has been discussion on some of these groups about the lack of nxn -> 2n
  15. >> multiplication, where n is the integer word size;
  16. >> But I know of no HLL which allows writing this.
  17.  
  18. peter@ferranti.com (peter da silva) writes:
  19. >C:
  20. >    int a, b;
  21. >    long c;
  22.  
  23. >    c = a * b;
  24.  
  25. >I've used [this] on the PDP-11, and gotten the expected 2x2->4
  26. >multiply in the generated code.
  27.  
  28. >> The use of these features is portable.
  29.  
  30. >Pretty much.
  31.  
  32. Peter, didn't you follow comp.lang.c when this was last hashed out?
  33. In C, multiplication of two integer values gives a result of type
  34. integer, and many compilers will be oh-so-careful and produce a
  35. ``correct'' integer-size result, which is then carefully sign-extended
  36. to a long.
  37.  
  38. (Note that the 2x2->4 multiply will give the correct result if it fits
  39. in an int, and since an integer overflow causes undefined behaviour, a
  40. Standard C implementation is _allowed_ to use the 2x2->4 to implement
  41. the code above. However, I think that many implementors will choose to
  42. define that overflow produces the low-order bits of true result, and
  43. so they will have use 2x2->2 and sign extend.)
  44.  
  45. On the other hand,
  46.     c = (long)a * b;
  47. is guaranteed to give the same result as 2x2->4. Complain to your
  48. vendor if such an instruction exists and the C ``optimizer'' does not
  49. use it.
  50.  
  51. [All this presupposes that long is twice as long as int. As you noted,
  52. that is a very non-portable assumption in C --- it's not even certain,
  53. I think, that there are integers of more than one size. For some uses
  54. it would be nice if thing were different ... but they aren't.]
  55.  
  56. Lars Mathiesen (U of Copenhagen CS Dep) <thorinn@diku.dk> (Humour NOT marked)
  57.