invmap.cpp
Go to the documentation of this file.
1 /* $Id: invmap.cpp 577 2011-05-21 20:38:48Z pbelotti $
2  *
3  * Name: invmap.cpp
4  * Author: Pietro Belotti
5  * Purpose: create a bijection between ASL's efunc and integer to
6  * inversely map e->op fields into constant operators
7  *
8  * (C) Carnegie-Mellon University, 2006-11.
9  * This file is licensed under the Eclipse Public License (EPL)
10  */
11 
12 #include <stdlib.h>
13 
14 #include "asl.h"
15 #include "opcode.hd"
16 #include "nlp.h"
17 #include "r_opn.hd"
18 
19 /* couples an ASL function pointer with the relative operator constant */
20 
21 typedef struct {
22  efunc *fp;
23  int op;
24 } AslCouPair;
25 
26 
27 /* compare two AslCoupair's, used in qsort and bsearch below */
28 
29 /* AW: 2007-06-11: changed b/c of problems with MSVC++ */
30 /* inline int pair_compare (const void *p1, const void *p2) { */
31 static int pair_compare (const void *p1, const void *p2) {
32 
33  /* FIX! weak cast for 64 bit machines */
34 
35  register size_t f1 = Intcast (((AslCouPair *) p1) -> fp);
36  register size_t f2 = Intcast (((AslCouPair *) p2) -> fp);
37 
38  if (f1 < f2) return -1;
39  else if (f1 > f2) return 1;
40  else return 0;
41 }
42 
43 
44 /* array of pairs (efunc2*, int) that relates all operators */
45 
46 AslCouPair opmap [N_OPS];
47 
48 
49 /* binary search to get operator number from its efunc2* (the type of e->op) */
50 
51 size_t getOperator (efunc *f) {
52 
53  static char first_call = 1;
54  AslCouPair key, *res;
55 
56  /* FIX cast for 64 bit machines */
57 
58  if ((Intcast f < N_OPS) &&
59  (Intcast f > -N_OPS))
60  return Intcast f;
61 
62  key.fp = f;
63 
64  if (first_call) { /* opmap is still empty, fill it using values from r_ops [] */
65 
66  register int i=0;
67  register AslCouPair *ops = opmap;
68 
69  /* fill opmap vector with inverse correspondence pairs efunc -> int */
70  while (i<N_OPS) {
71  ops -> fp = r_ops [ops -> op = i++];
72  ops++;
73  }
74 
75  /* sort opmap for later use with bsearch */
76  qsort (opmap, N_OPS, sizeof (AslCouPair), pair_compare);
77  first_call = 0;
78  }
79 
80  /* find int operator through binary search */
81  res = (AslCouPair *) bsearch (&key, opmap, N_OPS, sizeof (AslCouPair), pair_compare);
82 
83  if (!res)
84  return -1;
85 
86  return res -> op;
87 }
efunc * fp
Definition: invmap.cpp:22
AslCouPair opmap[N_OPS]
Definition: invmap.cpp:46
void fint fint fint real fint real real real real * f
static int pair_compare(const void *p1, const void *p2)
Definition: invmap.cpp:31
int getOperator(efunc *)
Definition: invmap.cpp:51
#define Intcast
Definition: OSdtoa.cpp:38
int op
Definition: invmap.cpp:23