/home/coin/SVN-release/OS-2.4.1/Couenne/src/readnl/invmap.cpp

Go to the documentation of this file.
00001 /* $Id: invmap.cpp 577 2011-05-21 20:38:48Z pbelotti $
00002  *
00003  * Name:    invmap.cpp
00004  * Author:  Pietro Belotti
00005  * Purpose: create a bijection between ASL's efunc and integer to
00006  *          inversely map e->op fields into constant operators
00007  *
00008  * (C) Carnegie-Mellon University, 2006-11.
00009  * This file is licensed under the Eclipse Public License (EPL)
00010  */
00011 
00012 #include <stdlib.h>
00013 
00014 #include "asl.h"
00015 #include "opcode.hd"
00016 #include "nlp.h"
00017 #include "r_opn.hd"
00018 
00019 /* couples an ASL function pointer with the relative operator constant */
00020 
00021 typedef struct {
00022   efunc *fp;
00023   int    op;
00024 } AslCouPair;
00025 
00026 
00027 /* compare two AslCoupair's, used in qsort and bsearch below */
00028 
00029 /* AW: 2007-06-11: changed b/c of problems with MSVC++ */
00030 /* inline int pair_compare (const void *p1, const void *p2) { */
00031 static int pair_compare (const void *p1, const void *p2) {
00032 
00033   /* FIX! weak cast for 64 bit machines */
00034 
00035   register size_t f1 = Intcast (((AslCouPair *) p1) -> fp); 
00036   register size_t f2 = Intcast (((AslCouPair *) p2) -> fp); 
00037 
00038   if      (f1 < f2) return -1;
00039   else if (f1 > f2) return  1;
00040   else return 0;
00041 }
00042 
00043 
00044 /* array of pairs (efunc2*, int) that relates all operators */
00045 
00046 AslCouPair opmap [N_OPS];
00047 
00048 
00049 /* binary search to get operator number from its efunc2* (the type of e->op) */
00050 
00051 size_t getOperator (efunc *f) {
00052 
00053   static char first_call = 1;
00054   AslCouPair key, *res;
00055 
00056   /* FIX cast for 64 bit machines */
00057 
00058   if ((Intcast f <  N_OPS) && 
00059       (Intcast f > -N_OPS))
00060     return Intcast f;
00061 
00062   key.fp = f;
00063 
00064   if (first_call) { /* opmap is still empty, fill it using values from r_ops [] */
00065 
00066     register int i=0;
00067     register AslCouPair *ops = opmap;
00068 
00069     /* fill opmap vector with inverse correspondence pairs efunc -> int */
00070     while (i<N_OPS) {
00071       ops -> fp = r_ops [ops -> op = i++];
00072       ops++;
00073     }
00074 
00075     /* sort opmap for later use with bsearch */
00076     qsort (opmap, N_OPS, sizeof (AslCouPair), pair_compare);
00077     first_call = 0;
00078   }
00079 
00080   /* find int operator through binary search */
00081   res = (AslCouPair *) bsearch (&key, opmap, N_OPS, sizeof (AslCouPair), pair_compare);
00082 
00083   if (!res) 
00084     return -1;
00085 
00086   return res -> op;
00087 }

Generated on Thu Nov 10 03:05:46 2011 by  doxygen 1.4.7