varpack.c

Go to the documentation of this file.
00001 /*---------------------------------------------------------------
00002  * Copyright (c) 1998-2002 by Inria Lorraine.  All Rights Reserved 
00003  *    Serge Steer / Eric Fleury - Mar 13, 1998: Created.
00004  *
00005  * July 2002 : modified by merging lpak and varpak to 
00006  *             avoid code duplication 
00007  *             Chancelier Enpc 
00008  * 
00009  * A set of functions for packing Scilab data 
00010  *---------------------------------------------------------------*/ 
00011 
00012 #include <stdio.h>
00013 #include "machine.h"
00014 #include "stack-c.h"
00015 #include "sci_pvm.h"
00016 
00017 /* Table of constant values */
00018 
00019 
00020 static int cx4 = 4;
00021 static int cx2 = 2;
00022 
00023 #define ODD(x) ((x)+((x)%2))
00024 
00025 static void C2F(ipak)  (int *il, int *pack, int *n, int *nMax);
00026 static void F2C(bpak)  (int *il, int *pack, int *n, int *nmax);
00027 static void F2C(cpak)  (int *il, int *pack, int *n, int *nmax);
00028 static void F2C(ppak)  (int *il, int *pack, int *n, int *nmax);
00029 static void F2C(mpak)  (int *il, int *pack, int *n, int *nmax);
00030 static void F2C(spak)  (int *il, int *pack, int *n, int *nmax);
00031 static void F2C(sppak) (int *il, int *pack, int *n, int *nmax);
00032 static void F2C(bsppak)(int *il, int *pack, int *n, int *nmax);
00033 static void C2F(libpak)(int *il, int *pack, int *np, int *npMax);
00034 static int pak_object_info (int ilk,int stk_pos, int *pack, int *np, int *npmax);
00035 
00036 /*-----------------------------------------------------------------
00037  *
00038  *     Given a scilab variable, stored in the stack at the position k (in 
00039  *     lstk) this function returns a "packing" vector pack for pvm. 
00040  *     A scilab variable is a consecutive memory region formed by a 
00041  *     succession of int and double precision vectors. 
00042  *     pack contains a sequence of pairs of ints. 
00043  *     varpak returns a vector which contains the size of this int 
00044  *     and double precision vectors. pack contains a sequence of pairs of 
00045  *     ints:  pack=[i1,r1,i2,r2,...,in,rn] 
00046  *     il is a number of int words 
00047  *     rl is a number of double precision word 
00048  *     n    : is the returned length of pack 
00049  *     nMax : is the Maximum size allowed for pack  
00050  *-----------------------------------------------------------------*/ 
00051 
00052 int C2F(varpak)(int *k, int *pack, int *n, int *nMax, int *ierr) {
00053   *n = 0; /* n must be initialized before calling pak_object_info */
00054   /* ierr = 0 OK 
00055    * ierr = 1 nMax is not enough 
00056    * ierr = 2 unknown data type 
00057    */
00058   *ierr= pak_object_info(*k,1,pack,n,nMax);
00059   return 0;
00060 }
00061 
00062 
00063 static int  pak_object_info(int ilk,int stk_pos, int *pack, int *np, int *nMax)
00064 {
00065   int type,ne,il,ilp,i,li,ill,l,kp;
00066 
00067   if ( stk_pos == 1 ) 
00068     {
00069       /* object given by its stk position */ 
00070       il = iadr(*Lstk(ilk));
00071       if (*istk(il ) < 0) {
00072         il = iadr(*istk(il +1));
00073       }
00074     }
00075   else 
00076     {
00077       il = ilk;
00078     }
00079 
00080   type = *istk(il); 
00081 
00082   switch ( type ) {
00083   case sci_matrix:         C2F(spak)(&il, pack, np, nMax); break; 
00084   case sci_poly  :         C2F(ppak)(&il, pack, np, nMax); break;
00085   case sci_boolean:        C2F(bpak)(&il, pack, np, nMax); break;
00086   case sci_sparse:         C2F(sppak)(&il, pack, np, nMax); break;
00087   case sci_boolean_sparse: C2F(bsppak)(&il, pack, np, nMax);break;
00088   case sci_ints:           C2F(ipak)(&il, pack, np, nMax); break;
00089   case sci_strings:        C2F(cpak)(&il, pack, np, nMax);break;
00090   case sci_u_function:
00091   case sci_c_function:     C2F(mpak)(&il, pack, np, nMax); break;
00092   case sci_lib :           C2F(libpak)(&il, pack, np, nMax);break;
00093   case sci_list : 
00094   case sci_tlist : 
00095   case sci_mlist :  
00096     /* nb element of the list */
00097     ne = istk(il)[1];
00098     /* first pack the list header */
00099     kp = 2;
00100     *np += kp;
00101     if ((*np ) > (*nMax)) {
00102       return 1;
00103     }
00104     pack[0] = ODD(ne + 3);
00105     pack[1] = 0;
00106     /* loop on objects */
00107     ilp = il + 2;
00108     l = sadr(ilp + ne + 1);
00109     for (i = 1; i <= ne; ++i) { 
00110       int np1,nk,*ptr,p_size,p_i,padding,size,ierr;
00111       li  = istk(ilp)[i-1];
00112       ill = iadr(l + li -1);
00113       size = istk(ilp)[i] - istk(ilp)[i-1];
00114       /* recursive call but now with an istk position 
00115        * i.e stk_pos == 0 
00116        */ 
00117       np1 = *np;
00118       ierr= pak_object_info(ill,0,pack+kp,&np1,nMax);
00119       if ( ierr != 0) return ierr;
00120       /* Complete the padding */
00121       nk = np1 - *np;
00122       ptr = &pack[kp];
00123       p_size = 0;
00124       for (p_i = 0; p_i < nk; p_i += 2) {
00125         p_size += ptr[p_i]/2 + ptr[p_i] % 2;
00126         p_size += ptr[p_i+1];
00127       }
00128       padding = size - p_size;
00129       if (padding) {    
00130         ptr[nk-1] += padding;
00131       }
00132       /* Update the pack index */
00133       kp += nk;
00134       *np += nk;
00135       if( *np > *nMax) return 1;
00136     }
00137     break ; 
00138   default : return 2;
00139     break;
00140   }
00141   if ( *np > *nMax) return 1; 
00142   return 0;
00143 }
00144 
00145 
00146 /* utilities */ 
00147 
00148 int C2F(allignf)(int *n, int *m)
00149 {
00150   int rest= *n % *m ; 
00151   if ( rest == 0 ) 
00152     return *n ; 
00153   else 
00154     return *n - rest + *m ; 
00155 } 
00156 
00157 /* scalar matrix case */ 
00158 
00159 static void C2F(spak)(int *il, int *pack, int *n, int *nMax)
00160 {
00161   *n += 2;
00162   if (*n > *nMax) return ;
00163   pack[0] = 4;
00164   pack[1] = *istk(*il +1) * *istk(*il + 1 +1) * (*istk(*il + 2 +1) + 1);
00165 } 
00166 
00167 /*     matrix of interger */
00168 
00169 static void C2F(ipak)(int *il, int *pack, int *n, int *nMax)
00170 {
00171   int ix1, ix2;
00172   *n += 2;
00173   if (*n > *nMax)  return ; 
00174 
00175   pack[0] = 4;
00176   pack[1] = 0;
00177   /*     char */
00178   if (*istk(*il + 2 +1) == 1) {
00179     ix2 = *istk(*il +1) * *istk(*il + 1 +1);
00180     ix1 = pack[0] + C2F(allignf)(&ix2, &cx4) / 4;
00181     pack[0] = ODD(ix1);
00182   }
00183   /*     short */
00184   if (*istk(*il + 2 +1) == 2) {
00185     ix2 = *istk(*il +1) * *istk(*il + 1 +1);
00186     ix1 = pack[0] + C2F(allignf)(&ix2, &cx2) / 2;
00187     pack[0] = ODD(ix1);
00188   }
00189   /*     int */
00190   if (*istk(*il + 2 +1) == 4) {
00191     ix1 = pack[0] + *istk(*il +1) * *istk(*il + 1 +1);
00192     pack[0] = ODD(ix1);
00193   }
00194   /*     unsigned char */
00195   if (*istk(*il + 2 +1) == 11) {
00196     ix2 = *istk(*il +1) * *istk(*il + 1 +1);
00197     ix1 = pack[0] + C2F(allignf)(&ix2, &cx4) / 4;
00198     pack[0] = ODD(ix1);
00199   }
00200   /*     unsigned short */
00201   if (*istk(*il + 2 +1) == 12) {
00202     ix2 = *istk(*il +1) * *istk(*il + 1 +1);
00203     ix1 = pack[0] + C2F(allignf)(&ix2, &cx2) / 2;
00204     pack[0] = ODD(ix1);
00205     
00206   }
00207   /*     unsigned int */
00208   if (*istk(*il + 2 +1) == 14) {
00209     ix1 = pack[0] + *istk(*il +1) * *istk(*il + 1 +1);
00210     pack[0] = ODD(ix1);
00211   }
00212   return ;
00213 } 
00214 
00215 /*     matrix of polynomial case */
00216 
00217 static void C2F(ppak)(int *il, int *pack, int *np, int *npMax)
00218 {
00219   int ix1;
00220   static int id, mn;
00221 
00222   *np += 2;
00223   if (*np > *npMax) {
00224     return ;
00225   }
00226   id = *il + 8;
00227   mn = *istk(*il +1) * *istk(*il + 1 +1);
00228   ix1 = mn + 9;
00229   pack[0] = ODD(ix1);
00230   pack[1] = (*istk(id + mn ) - 1) * (*istk(*il + 2 +1) + 1);
00231   return ;
00232 } 
00233 
00234 /*     matrix of boolean case */
00235 
00236 static void C2F(bpak)(int *il, int *pack, int *np, int *npMax)
00237 {
00238   int ix1;
00239   static int id, mn;
00240   /* Function Body */
00241   *np += 2;
00242   if (*np > *npMax) {
00243     return ;
00244   }
00245   id = *il + 8;
00246   mn = *istk(*il +1) * *istk(*il + 1 +1);
00247   ix1 = mn + 3;
00248   pack[0] = ODD(ix1);
00249   pack[1] = 0;
00250   return ;
00251 } 
00252 
00253 /*     sparse matrix of numbers  case */
00254 
00255 static void C2F(sppak)(int *il, int *pack, int *np, int *npMax)
00256 {
00257   int ix1;
00258   static int m, n, it;
00259   static int nel;
00260   *np += 2;
00261   if (*np > *npMax) {
00262     return ;
00263   }
00264   nel = *istk(*il + 3 +1);
00265   m = *istk(*il +1);
00266   n = *istk(*il + 1 +1);
00267   it = *istk(*il + 2 +1);
00268   ix1 = m + 5 + nel;
00269   pack[0] = ODD(ix1);
00270   pack[1] = nel * (it + 1);
00271   return ;
00272 } 
00273 
00274 
00275 /*     sparse matrix of numbers  case */
00276 
00277 static void C2F(bsppak)(int *il, int *pack, int *np, int *npMax)
00278 {
00279   int ix1;
00280   static int m, n, it;
00281   static int nel;
00282 
00283   *np += 2;
00284     if (*np > *npMax) {
00285         return ;
00286     }
00287     nel = *istk(*il + 3 +1);
00288     m = *istk(*il +1);
00289     n = *istk(*il + 1 +1);
00290     it = *istk(*il + 2 +1);
00291     ix1 = m + 5 + nel;
00292     pack[0] = ODD(ix1);
00293     pack[1] = 0;
00294     return ;
00295 } 
00296 
00297 /*     matrix of strings  case */
00298 
00299 static void C2F(cpak)(int *il, int *pack, int *np, int *npMax)
00300 {
00301     int ix1;
00302     static int id, mn;
00303     static int vol;
00304     /* Function Body */
00305     *np += 2;
00306     if (*np > *npMax) {
00307         return ;
00308     }
00309     mn = *istk(*il +1) * *istk(*il + 1 +1);
00310     id = *il + 4;
00311     vol = *istk(*il + 4 + mn ) - 1;
00312     ix1 = mn + 5 + vol;
00313     pack[0] = ODD(ix1);
00314     pack[1] = 0;
00315     return ;
00316 } 
00317 
00318 
00319 /*     function  case (compiled or not) */
00320 
00321 static void C2F(mpak)(int *il, int *pack, int *np, int *npMax)
00322 {
00323   int ix1;
00324   static int nout;
00325   static int nt, il1;
00326   static int nin;
00327 
00328   *np += 2;
00329   if (*np > *npMax) {
00330     return ;
00331   }
00332   il1 = *il + 1;
00333   nin = *istk(il1 );
00334   il1 = il1 + nin * nsiz + 1;
00335   nout = *istk(il1 );
00336   il1 = il1 + nout * nsiz + 1;
00337   nt = *istk(il1 );
00338   ix1 = nin * nsiz + 4 + nout * nsiz + nt;
00339   pack[0] = ODD(ix1);
00340   pack[1] = 0;
00341   return ;
00342 } 
00343 
00344 /*    library   case (compiled or not) */
00345 
00346 static void C2F(libpak)(int *il, int *pack, int *np, int *npMax)
00347 {
00348   int ix1;
00349   static int nf, nh, nm;
00350   static int ilh, iln;
00351 
00352   *np += 2;
00353   if (*np > *npMax) {
00354         return ;
00355   }
00356   nf = *istk(*il +1);
00357   ilh = *istk(*il + 2 + nf );
00358   nh = *istk(ilh );
00359   iln = ilh + nh + 1;
00360   nm = *istk(iln );
00361   /*      write(*,*) nf, nh, nm, istk(iln+1) */
00362   ix1 = nf + 4 + nh + (nm << 1) + 100;
00363   pack[0] = ODD(ix1);
00364   pack[1] = 0;
00365   return ;
00366 } 
00367 
00368 

Generated on Sun Mar 4 15:03:59 2007 for Scilab [trunk] by  doxygen 1.5.1