home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / fortran / 3113 < prev    next >
Encoding:
Internet Message Format  |  1992-08-22  |  5.6 KB

  1. Path: sparky!uunet!cs.utexas.edu!sdd.hp.com!elroy.jpl.nasa.gov!ames!agate!overload.lbl.gov!lll-winken!phoenix!stevep
  2. From: stevep@phoenix.ocf.llnl.gov (Steve J Patenaude)
  3. Newsgroups: comp.lang.fortran
  4. Subject: Re: Low level routines to do logical shifts
  5. Keywords: lshft,rshft etc.
  6. Message-ID: <134179@lll-winken.LLNL.GOV>
  7. Date: 22 Aug 92 19:44:01 GMT
  8. References: <1992Aug18.174715.25954@uceng.UC.EDU>
  9. Sender: usenet@lll-winken.LLNL.GOV
  10. Organization: Lawrence Livermore National Laboratory
  11. Lines: 170
  12. Nntp-Posting-Host: phoenix.ocf.llnl.gov
  13.  
  14.  
  15. I read with interest your problem to convert from C to Fortran a
  16. program that apparently generates random programs for an
  17. imaginary computer.  My first inclination is to say "Don't, it's
  18. better done in C".  However, since this comes from a university,
  19. it is probably an assignment from a particularly diabolical
  20. professor.  So, I'll try to give you some hints.
  21.  
  22. First, to do a <while> loop in Fortran 77, you must use the
  23. dreaded -gasp- GOTO!
  24.  
  25. C FORTRAN FOR 'while (foo < finished) foo += incr;'
  26. 1     IF (FOO .GE. FINISHED) GOTO 2
  27.       FOO = FOO + INCR
  28.       GOTO 1
  29. C END OF WHILE LOOP
  30. 2   ...
  31.  
  32. Now, for your program.  You are apparently generating random
  33. programs for an imaginary computer with 64 bit word length using
  34. a 32 bit word length computer.  Since there are no shift
  35. instructions in FORTRAN 77, I would suggest that you address the
  36. various p arts of the word using a type of CHARACTER*4, which
  37. allows you to individually reference each byte of the 32 bit
  38. word, or CHARACTER*8 which will pack 8 bytes into two 32-bit
  39. words.  For example,
  40.  
  41. CHARACTER*4 WORD < WORD(1:1) = "X" > stores an 'X' character in
  42. the 1st or uppermost byte position in the word.
  43.  
  44. You are generating 16 bit random instructions of the following
  45. form:
  46.     XXXXXIIIXXAABBCC
  47. where X = unused bit
  48. I = 3 bits of instr
  49. A = 2 bits of mode1
  50. B = 2 bits of mode2
  51. C = 2 bits of mode3
  52.  
  53. by generating random numbers in this range and shifting and
  54. or'ing them into the instruction part of the program.  The instr
  55. part can be stored directly into the upper character position,
  56. The modes will need to be defined as preshifted values.  Since
  57. none of your values overlap and would cause a carry, you can add
  58. rather than <or> the values together.  (The PARAMETER statement
  59. is a good substitute for #define. ) To store the arguments with
  60. mismatch types, you will probably need to equivalence 16-bit
  61. intege rs and 16-bit character variables.  Therefore replace:
  62.  
  63. >#define MAXDATA 512
  64. >#define ADD 0
  65. >...
  66. >#define BEQ 6
  67. >    instr = RanInt(ADD,BEQ);
  68. >    mode1 = RanInt(0,2);
  69. >    mode2 = RanInt(0,2);
  70. >    mode3 = RanInt(0,2);
  71. >    arg1 = RanInt(0,MAXDATA);
  72. >    arg2 = RanInt(0,MAXDATA);
  73. >    arg3 = RanInt(0,MAXDATA);
  74. >...
  75. >    instr <<= 8;
  76. >    instr |= mode1 << 4;
  77. >    instr |= mode2 << 2;
  78. >    instr |= mode3;
  79.  
  80. with:
  81.  
  82.       PARAMETER (MAXDATA=512)
  83.       PARAMETER (ADD=0, ... BEQ=6)
  84.       CHARACTER*8 WORD
  85.       CHARACTER*1 VMODE1(3), VMODE2(3), VMODE3(3)
  86.       INTEGER MODE1, MODE2, MODE3
  87.  
  88.       INTERGER*2 ARG1, ARG2, ARG3
  89.       CHARACTER*2 CARG1, CARG2, CARG3
  90.       EQUIVALENCE (CARG1, ARG1), (CARG2,ARG2), (CARG3,ARG3)
  91. C  DEFINE PRESHIFTED VALUES FOR MODE1, MODE2, AND MODE3 WITH HEX VALUES
  92.       VMODE1(1) = #H00
  93.       VMODE1(2) = #H10
  94.       VMODE1(3) = #H20
  95.       VMODE2(1) = #H00
  96.       VMODE2(2) = #H04
  97.       VMODE2(3) = #H08
  98.       VMODE3(1) = #H00
  99.       VMODE3(2) = #H01
  100.       VMODE3(3) = #H02
  101.    ...
  102. C GENERATE RANDOM INSTRUCTIONS
  103.       WORD(1:1) = RANINT(ADD,BEQ)
  104.       MODE1 = RANINT(0,2)+1
  105.       MODE2 = RANINT(0,2)+1
  106.       MODE3 = RANINT(0,2)+1
  107.       WORD(2:2) = VMODE1(MODE1) + VMODE2(MODE2) + VMODE3(MODE3)
  108.  
  109. and store the arguments
  110.       ARG1 = RANINT(0,MAXDATA)
  111.       ARG2 = RANINT(0,MAXDATA)
  112.       ARG3 = RANINT(0,MAXDATA)
  113.       WORD(3:4) = CARG1(1:2)
  114.       WORD(5:6) = CARG2(1:2)
  115.       WORD(7:8) = CARG3(1:2)
  116.  
  117. You have no real need for pointers in this program, just
  118. dimension the data.
  119.  
  120. >typedef int data_t;
  121. >typedef struct prog {
  122. >   int num_inst;
  123. >   int num_data;
  124. >   int Pd;
  125. >   long instructions[2*MAXINSTRUCTIONS];
  126. >   data_t data[MAXDATA];
  127. >} prog_t;
  128. >
  129. >typedef struct prog_struct{
  130. >   int num_progs;
  131. >   prog_t program[MAXPROCESSORS];
  132. >}prog_struct_t;
  133. >...
  134. > prog_struct_t programs;
  135.  
  136. >#define MAXPROCESSORS 1024
  137. >#define MAXINSTRUCTIONS 10
  138. >#define MAXDATA 512
  139. >#define MAXPROGRAMS MAXPROCESSORS
  140. >#define MAXDATAVAL 4096
  141.  
  142.       PARAMETER (MAXPROCESSORS=1024, MAXINSTRUCTIONS=10)
  143.       PARAMETER (MAXDATA = 512, MAXPROGRAMS = MAXPROCESSORS)
  144.       PARAMETER (MAXDATAVAL = 4096)
  145.  
  146.       INTEGER NUM_PROGS
  147.       INTEGER NUM_INST(MAXPROCESSORS), NUM_DATA(MAXPROCESSORS)
  148.       INTEGER PD(MAXPROCESSORS)
  149.       CHARACTER*8 INSTRUCTIONS(2*MAXINSTRUCTIONS,MAXPROCESSORS)
  150.       INTEGER DATA(MAXDATA,MAXPROCESSORS)
  151.  
  152. and reference it directly
  153.  
  154. >#define current programs.program[prog]
  155. >...
  156. > for (prog=0;  prog< MAXPROGRAMS; ++prog) {
  157. > ...
  158. >    for (inst=0;  inst  < MAXINSTRUCTIONS; ++inst) {
  159. >...
  160. >             current.instructions[2*inst] = (( instr & 0xffff) << 16 |
  161. >                                                       (arg1 & 0xffff);
  162. >             current.instructions[2*inst] = (( arg2 & oxffff) << 16 |
  163. >                                                       (arg3 & 0xffff);
  164. > ...
  165.  
  166.       DO 10 PROG=1,MAXPROGRAMS
  167.           ...
  168.           DO 20 INST=1,MAXINSTRUCTIONS
  169.              ...
  170.              INSTRUCTIONS( INST,  PROG) = WORD
  171.              ...
  172. 10        CONTINUE
  173. 20    CONTINUE
  174.  
  175. I hope this, helps.  Of course your success with this technique
  176. depends on which FORTRAN you are using.  Newer FORTRANs try to
  177. help you with strong type casting, while older, friendlier
  178. FORTRANS let you shoot yourself in the foot as well as any modern
  179. C.
  180.  
  181. Cherie Jo Patenaude, email PATENAUDEC@LLNL.GOV
  182.  
  183.  
  184.