home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / os / vms / 21956 < prev    next >
Encoding:
Internet Message Format  |  1993-01-25  |  2.3 KB

  1. Path: sparky!uunet!olivea!decwrl!pa.dec.com!engage.pko.dec.com!nntpd.lkg.dec.com!ryn.mro4.dec.com!news
  2. From: blair@snogum.enet.dec.com (Blair Phillips - Digital)
  3. Newsgroups: comp.os.vms
  4. Subject: Re: VAXC divide problems (V3.2)
  5. Message-ID: <1993Jan26.012657.5983@ryn.mro4.dec.com>
  6. Date: 26 Jan 93 01:26:57 GMT
  7. References: <1993Jan22.060311.5226@cs.rit.edu> <1993Jan22.231842.4799@eco.twg.com>
  8. Sender: news@ryn.mro4.dec.com (USENET News System)
  9. Reply-To: Blair@snogum.enet.dec.com
  10. Organization: Digital Equipment Corporation
  11. Lines: 71
  12.  
  13.  
  14. In article <1993Jan22.231842.4799@eco.twg.com>, reece@eco.twg.com (Reece R. Pollack) writes:
  15.  
  16. >In article <1993Jan22.060311.5226@cs.rit.edu>, whd0675@cs.rit.edu (Walter H Dick, III) writes:
  17. >|>A statement in VAXC such as:
  18. >|>
  19. >|>   i = (x + 511) / 512;
  20. >|>
  21. >|>generates ten or so instructions.  Is there anyway to get it to generate:
  22. >|>
  23. >|>   ADDL3  #511,x,R0
  24. >|>   DIVL3  R0,#512,i
  25. >
  26. >That's funny, when I compiled this function:
  27. >
  28. >func( int x )
  29. >{
  30. >    int         i;
  31. >
  32. >    i = (x + 511) / 512;
  33. >    return( i );
  34. >}
  35. >
  36. >VAX C V3.2-044 generated this code:
  37. >
  38. >    .entry    func,^m<>
  39. >    subl2    #4,sp
  40. >    addl3    #511,4(ap),r0
  41. >    divl2    #512,r0
  42. >    ret
  43.  
  44. If x is unsigned int rather than int, you get an unholy mess of code.
  45.  
  46. There are two reasons for this:
  47. 1. The VAX doesn't have an unsigned divide operation, so the compiler generates
  48.    code to cater for different combinations of signs in the dividend & divisor.
  49. 2. The compiler does a lousy job of optimizing unsigned arithmetic. In
  50.    particular, it treats a constant divisor exactly the same as a variable,
  51.    generating lots of useless conditional branches and dead code.
  52.  
  53. The fix is simple - use a shift op instead. 
  54.  
  55. func( unsigned int x )
  56. {
  57.     int         i;
  58.  
  59.     i = (x + 511) >> 9;
  60.     return( i );
  61. }
  62.  
  63. generates
  64.  
  65. func:
  66.         .entry  func,^m<>
  67.         subl2   #4,sp
  68.  
  69.         addl3   #511,4(ap),r0
  70.         extzv   #9,#23,r0,r1
  71.         movl    r1,r0
  72.         ret     
  73.  
  74. >
  75. >Don't ask me why it allocated space on the stack without using it, 
  76.  
  77. I think the stack space is for the condition handler.
  78. --
  79. ----------
  80. Blair Phillips                    Blair@snogum.enet.dec.com
  81. Digital Equipment Corp (Aust) P/L        Phone: (+61 6) 2754874
  82. Canberra, Australia                FAX  : (+61 6) 2473654
  83. {Any opinions expressed are my own, not those of Digital Equipment Corporation}
  84.