home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / fortran / 3088 < prev    next >
Encoding:
Text File  |  1992-08-18  |  6.3 KB  |  229 lines

  1. Path: sparky!uunet!dtix!darwin.sura.net!zaphod.mps.ohio-state.edu!malgudi.oar.net!ucbeh.san.uc.edu!uceng.uc.edu!pviswana
  2. From: pviswana@uceng.UC.EDU (Prashanth V.)
  3. Newsgroups: comp.lang.fortran
  4. Subject: Low level routines to do logical shifts
  5. Keywords: lshft,rshft etc.
  6. Message-ID: <1992Aug18.174715.25954@uceng.UC.EDU>
  7. Date: 18 Aug 92 17:47:15 GMT
  8. Sender: pviswana@uceng.uc.edu
  9. Organization: College of Engineering, University of Cincinnati
  10. Lines: 217
  11.  
  12. Hello:
  13.  
  14.  
  15. I am in a desparate need to translate the following program
  16. into Fortran. The problem I am having is this:
  17.  
  18. THe parser that is available to translate the fortran code
  19. to the instruction set of my choice does not have the
  20. capability to understand any of the Fortran 90 commands like
  21. shiftleft and shift right mod and so on.. 
  22.  
  23. I also do not know how to handle while loops in fortran. ( How
  24. stupid of me :-)).
  25.  
  26. Is there anyone out there who can help me translate the following
  27. complex code to simple fortran77 compatible code. Yes, I am
  28. afraid we have to deal with pointers and stuff. I would also
  29. be happy with a simple solutions like having to initialize
  30. all datastructure elemets with identifiers.
  31.  
  32. I appreciate any help from any/all of you: The  code follows:
  33. ------------- TO BE CONVERTED TO FORTRAN ------------------
  34. #include <fcntl.h>
  35. #include <sys/time.h>
  36. #include <errno.h>
  37.  
  38. #define false 0
  39. #define true 1
  40.  
  41. #define ADD 0
  42. #define SUB 1
  43. #define MUL 2
  44. #define DIV 3
  45. #define AND 4
  46. #define BAL 5
  47. #define BEQ 6
  48. #define HALT 7
  49.  
  50. #define NUMARGS 3
  51.  
  52. #define MAXPROCESSORS 1024
  53. #define MAXINSTRUCTIONS 10
  54. #define MAXDATA 512
  55. #define MAXPROGRAMS MAXPROCESSORS
  56. #define MAXDATAVAL 4096
  57.  
  58. typedef short Boolean;
  59.  
  60. typedef int data_t;
  61.  
  62. typedef struct prog{
  63.   int num_inst;
  64.   int num_data;
  65.   int PC;
  66.   long instructions[2*MAXINSTRUCTIONS];
  67.   data_t data[MAXDATA];
  68. }prog_t;
  69.  
  70. typedef struct prog_struct{
  71.   int num_progs;
  72.   prog_t program[MAXPROCESSORS];
  73. }prog_struct_t;
  74.  
  75.  
  76.  
  77. Boolean sane(inst, arg, num_inst, num_data)
  78. int inst, arg, num_inst, num_data;
  79. {
  80. Boolean toreturn;
  81.  
  82.   if ((inst < BEQ) || (arg < NUMARGS - 1))
  83.     toreturn = ((arg >= 0) && (arg < num_data));
  84.   else
  85.     toreturn = ((arg >= 0) && (arg < num_inst));
  86.   return(toreturn);
  87. }
  88.  
  89. int RanInt(low, high)
  90. int low, high;
  91. {
  92. long ret;
  93. int toreturn;
  94.  
  95.   ret = my_random();
  96.   ret = ret % (high - low + 1);
  97.   ret = ret + low;
  98.   toreturn = (int)ret;
  99.   return(toreturn);
  100. }
  101.  
  102. void PutData()
  103. {
  104. prog_struct_t programs;
  105. Boolean worked;
  106. int num_progs;
  107. int fd;
  108. int prog;
  109. int inst, data, arg;
  110. int loop1,loop2;
  111. long instr,arg1,arg2,arg3;
  112. short mode1,mode2,mode3;
  113. long this_one;
  114. Boolean inviolate[MAXINSTRUCTIONS];
  115. #define current programs.program[prog]
  116.  
  117.   fd = open("programs",O_WRONLY|O_CREAT,0700);
  118.   if (fd < 0)
  119.     perror("open failed");
  120.   programs.num_progs = MAXPROGRAMS;
  121.   write(fd,&(programs.num_progs), sizeof(programs.num_progs));
  122.   num_progs = programs.num_progs;
  123.   for (prog = 0; prog < num_progs; prog++){
  124.     for(loop1=0;loop1<MAXINSTRUCTIONS;++loop1)
  125.       inviolate[loop1] = false;
  126.   /* printf("Writing program %d\n",prog); */
  127.     current.num_inst = MAXINSTRUCTIONS;
  128.     write(fd, &(current.num_inst), sizeof(current.num_inst));
  129.     current.num_data = MAXDATA;
  130.     write(fd, &(current.num_data), sizeof(current.num_data));
  131.     for (inst = 0; inst < current.num_inst; inst++){
  132.       if (inst < current.num_inst - 1){
  133.         instr = RanInt(ADD,BEQ);
  134.     mode1 = RanInt(0,2);
  135.     mode2 = RanInt(0,2);
  136.     mode3 = RanInt(0,2);
  137.         arg1  = RanInt(1,current.num_data - 1);
  138.         arg2  = RanInt(1,current.num_data - 1);
  139.         arg3  = RanInt(1,current.num_data - 1);
  140.                     
  141.         if (instr == BEQ) {
  142.           arg3 = inst + 1;
  143.       inviolate[inst+1] = true;
  144.         }
  145.      else if (instr == BAL) {
  146.           arg2 = inst + 1;
  147.           inviolate[inst+1] = true;
  148.      }
  149.       } else {
  150.           instr = HALT;
  151.       }
  152.     instr <<= 8;
  153.         instr |= mode1 << 4;
  154.         instr |= mode2 << 2;
  155.         instr |= mode3;
  156.         current.instructions[2*inst] =     ((instr & 0xffff) << 16) | 
  157.                                            (arg1  & 0xffff);
  158.         current.instructions[2*inst + 1] = ((arg2  & 0xffff) << 16) | 
  159.                                            (arg3  & 0xffff);
  160.       }
  161.       for (inst = 0; inst < current.num_inst; inst++){
  162.         if (inviolate[inst]) {
  163.           for(loop1=0;loop1<current.num_inst;loop1++) {
  164.             instr = (current.instructions[2*loop1] >> 16) & 0xffff;
  165.             arg1  = (current.instructions[2*loop1]) & 0xffff;
  166.             arg2  = (current.instructions[2*loop1+1] >> 16) & 0xffff;
  167.             arg3  = (current.instructions[2*loop1+1])       & 0xffff;
  168.             this_one = instr >> 8;
  169.         if ((arg3 == inst) &&
  170.             (this_one != BEQ)) {
  171.               arg3 = 0;
  172.             }
  173.             current.instructions[2*loop1] =     ((instr & 0xffff) << 16) | 
  174.                                                (arg1  & 0xffff);
  175.             current.instructions[2*loop1 + 1] = ((arg2  & 0xffff) << 16) | 
  176.                                                (arg3  & 0xffff);
  177.           }
  178.     }
  179.       }
  180.       write(fd, current.instructions, current.num_inst*2*sizeof(long));
  181.       for (data = 0; data < current.num_data; data++){
  182. /*        *data_ptr = RanInt(0,MAXDATAVAL);*/
  183.         current.data[data] = data;
  184.       }
  185.       write(fd, current.data,current.num_data*sizeof(data_t));
  186.     }
  187.     for (loop1 = 0;loop1 < 20;++loop1) {
  188.       printf("Program %d:\n",loop1);
  189.       for (loop2 = 0;loop2 < 10; ++loop2) {
  190.         instr = (programs.program[loop1].instructions[2*loop2] >> 16) & 0xffff;
  191.         arg1  = (programs.program[loop1].instructions[2*loop2]) & 0xffff;
  192.         arg2 = (programs.program[loop1].instructions[2*loop2+1] >> 16) & 0xffff;
  193.         arg3 = (programs.program[loop1].instructions[2*loop2+1])       & 0xffff;
  194.         this_one = instr >> 8;
  195.         mode1 = (instr & 0xff) >> 4;
  196.         mode2 = (instr & 0xf) >> 2;
  197.         mode3 = (instr & 0x3);
  198.         printf("inst = %d\n",this_one);
  199.         printf("mode1 = %d\n",mode1);
  200.         printf("mode2 = %d\n",mode2);
  201.         printf("mode3 = %d\n",mode3);
  202.         printf("arg1 = %d\n",arg1);
  203.         printf("arg2 = %d\n",arg2);
  204.         printf("arg3 = %d\n",arg3);        
  205.       }
  206.     }
  207. }
  208.  
  209.  
  210. void InitRanSeed()
  211. {
  212. struct timeval tval;
  213. struct timezone tzone;
  214.  
  215.   gettimeofday(&tval, &tzone);
  216.   srandom(tval.tv_sec);
  217. }
  218.  
  219. main() 
  220. {
  221. InitRanSeed();
  222. PutData();
  223. }
  224. -- 
  225. Prashanth Viswanath                 Elemental empathy
  226. pviswana@uceng.uc.edu                 A change of synergy
  227. pviswana@thor.ece.uc.edu             Music making contact, 
  228.                           Naturally
  229.