home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / msdos / programm / 8733 < prev    next >
Encoding:
Text File  |  1992-08-23  |  2.2 KB  |  79 lines

  1. Xref: sparky comp.os.msdos.programmer:8733 comp.lang.c:12687
  2. Path: sparky!uunet!stanford.edu!unix!MPLVAX.SRI.COM!HUESTIS
  3. From: HUESTIS@MPLVAX.SRI.COM (David L. Huestis)
  4. Newsgroups: comp.os.msdos.programmer,comp.lang.c
  5. Subject: MSC 6.0 has trouble with 32-bit switch/case
  6. Message-ID: <Q7DCWI@MPLVAX.SRI.COM>
  7. Date: 24 Aug 92 00:38:37 GMT
  8. Sender: news@unix.SRI.COM
  9. Followup-To: comp.os.msdos.programmer
  10. Organization: Molecular Physics Laboratory, SRI International, Menlo Park, CA
  11. Lines: 66
  12.  
  13. /* When Microsoft C 6.0 compiles the following simple program 
  14.    the compiler gets confused in constructing the comparisons
  15.    for the 32-bit switch/case statements.  It appears to use
  16.    the fact that B1, B2, and B3 all have the same value in the
  17.    high 16-bits, leading potentially to shorter faster code,
  18.    but it loses track of the logic/branching thread by subtracting
  19.    values from 'dx' rather than comparing.  Compile with /Fc
  20.    Replacing the switch/case with if() statements leads to correct
  21.    execution.
  22.    
  23.    The program gives the following results:
  24.    
  25.       for A1 expected 0 got 0
  26.       for A2 expected 1 got 1
  27.       for A3 expected 2 got 2
  28.       for B1 expected 2 got 2
  29.       for B2 expected 1 got 3
  30.       for B3 expected 0 got 3
  31.  
  32.    ------------------------
  33.       
  34.    David L. Huestis
  35.    SRI International
  36.    <huestis@mplvax.sri.com>
  37.  
  38.    --------------------------------------------------------------------
  39. */
  40.  
  41. #include <stdio.h>
  42.  
  43. #define A1 0x0A0001UL
  44. #define A2 0x0A0002UL
  45. #define A3 0x0A0003UL
  46. #define B1 0x0B0001UL
  47. #define B2 0x0B0002UL
  48. #define B3 0x0B0003UL
  49.  
  50. int tsw(unsigned long sw)
  51. {
  52.       switch(sw)
  53.         {
  54.           case A1 : case B3 :
  55.             return( 0 ) ;
  56.             break ;
  57.           case A2 : case B2 :
  58.             return( 1 ) ;
  59.             break ;
  60.           case A3 : case B1 :
  61.             return( 2 ) ;
  62.             break ;
  63.             
  64.           default :
  65.             break ;
  66.         }
  67.       return(3) ;
  68. }
  69.  
  70. main()
  71. {
  72.       printf("for A1 expected 0 got %d\n", tsw(A1) ) ;
  73.       printf("for A2 expected 1 got %d\n", tsw(A2) ) ;
  74.       printf("for A3 expected 2 got %d\n", tsw(A3) ) ;
  75.       printf("for B1 expected 2 got %d\n", tsw(B1) ) ;
  76.       printf("for B2 expected 1 got %d\n", tsw(B2) ) ;
  77.       printf("for B3 expected 0 got %d\n", tsw(B3) ) ;
  78. }
  79.