1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/*---------------------------------------------------------------
* Copyright (c) 1998-2002 by Inria Lorraine. All Rights Reserved
* Serge Steer / Eric Fleury - Mar 13, 1998: Created.
*
* July 2002 : modified by merging lpak and varpak to
* avoid code duplication
* Chancelier Enpc
*
* A set of functions for packing Scilab data
*---------------------------------------------------------------*/
#include <stdio.h>
#include "../machine.h"
#include "../stack-c.h"
#include "sci_pvm.h"
/* Table of constant values */
static int cx4 = 4;
static int cx2 = 2;
#define ODD(x) ((x)+((x)%2))
static void C2F(ipak) (int *il, int *pack, int *n, int *nMax);
static void F2C(bpak) (int *il, int *pack, int *n, int *nmax);
static void F2C(cpak) (int *il, int *pack, int *n, int *nmax);
static void F2C(ppak) (int *il, int *pack, int *n, int *nmax);
static void F2C(mpak) (int *il, int *pack, int *n, int *nmax);
static void F2C(spak) (int *il, int *pack, int *n, int *nmax);
static void F2C(sppak) (int *il, int *pack, int *n, int *nmax);
static void F2C(bsppak)(int *il, int *pack, int *n, int *nmax);
static void C2F(libpak)(int *il, int *pack, int *np, int *npMax);
static int pak_object_info (int ilk,int stk_pos, int *pack, int *np, int *npmax);
/*-----------------------------------------------------------------
*
* Given a scilab variable, stored in the stack at the position k (in
* lstk) this function returns a "packing" vector pack for pvm.
* A scilab variable is a consecutive memory region formed by a
* succession of int and double precision vectors.
* pack contains a sequence of pairs of ints.
* varpak returns a vector which contains the size of this int
* and double precision vectors. pack contains a sequence of pairs of
* ints: pack=[i1,r1,i2,r2,...,in,rn]
* il is a number of int words
* rl is a number of double precision word
* n : is the returned length of pack
* nMax : is the Maximum size allowed for pack
*-----------------------------------------------------------------*/
int C2F(varpak)(int *k, int *pack, int *n, int *nMax, int *ierr) {
*n = 0; /* n must be initialized before calling pak_object_info */
/* ierr = 0 OK
* ierr = 1 nMax is not enough
* ierr = 2 unknown data type
*/
*ierr= pak_object_info(*k,1,pack,n,nMax);
return 0;
}
static int pak_object_info(int ilk,int stk_pos, int *pack, int *np, int *nMax)
{
int type,ne,il,ilp,i,li,ill,l,kp;
if ( stk_pos == 1 )
{
/* object given by its stk position */
il = iadr(*Lstk(ilk));
if (*istk(il ) < 0) {
il = iadr(*istk(il +1));
}
}
else
{
il = ilk;
}
type = *istk(il);
switch ( type ) {
case sci_matrix: C2F(spak)(&il, pack, np, nMax); break;
case sci_poly : C2F(ppak)(&il, pack, np, nMax); break;
case sci_boolean: C2F(bpak)(&il, pack, np, nMax); break;
case sci_sparse: C2F(sppak)(&il, pack, np, nMax); break;
case sci_boolean_sparse: C2F(bsppak)(&il, pack, np, nMax);break;
case sci_ints: C2F(ipak)(&il, pack, np, nMax); break;
case sci_strings: C2F(cpak)(&il, pack, np, nMax);break;
case sci_u_function:
case sci_c_function: C2F(mpak)(&il, pack, np, nMax); break;
case sci_lib : C2F(libpak)(&il, pack, np, nMax);break;
case sci_list :
case sci_tlist :
case sci_mlist :
/* nb element of the list */
ne = istk(il)[1];
/* first pack the list header */
kp = 2;
*np += kp;
if ((*np ) > (*nMax)) {
return 1;
}
pack[0] = ODD(ne + 3);
pack[1] = 0;
/* loop on objects */
ilp = il + 2;
l = sadr(ilp + ne + 1);
for (i = 1; i <= ne; ++i) {
int np1,nk,*ptr,p_size,p_i,padding,size,ierr;
li = istk(ilp)[i-1];
ill = iadr(l + li -1);
size = istk(ilp)[i] - istk(ilp)[i-1];
/* recursive call but now with an istk position
* i.e stk_pos == 0
*/
np1 = *np;
ierr= pak_object_info(ill,0,pack+kp,&np1,nMax);
if ( ierr != 0) return ierr;
/* Complete the padding */
nk = np1 - *np;
ptr = &pack[kp];
p_size = 0;
for (p_i = 0; p_i < nk; p_i += 2) {
p_size += ptr[p_i]/2 + ptr[p_i] % 2;
p_size += ptr[p_i+1];
}
padding = size - p_size;
if (padding) {
ptr[nk-1] += padding;
}
/* Update the pack index */
kp += nk;
*np += nk;
if( *np > *nMax) return 1;
}
break ;
default : return 2;
break;
}
if ( *np > *nMax) return 1;
return 0;
}
/* utilities */
int C2F(allignf)(int *n, int *m)
{
int rest= *n % *m ;
if ( rest == 0 )
return *n ;
else
return *n - rest + *m ;
}
/* scalar matrix case */
static void C2F(spak)(int *il, int *pack, int *n, int *nMax)
{
*n += 2;
if (*n > *nMax) return ;
pack[0] = 4;
pack[1] = *istk(*il +1) * *istk(*il + 1 +1) * (*istk(*il + 2 +1) + 1);
}
/* matrix of interger */
static void C2F(ipak)(int *il, int *pack, int *n, int *nMax)
{
int ix1, ix2;
*n += 2;
if (*n > *nMax) return ;
pack[0] = 4;
pack[1] = 0;
/* char */
if (*istk(*il + 2 +1) == 1) {
ix2 = *istk(*il +1) * *istk(*il + 1 +1);
ix1 = pack[0] + C2F(allignf)(&ix2, &cx4) / 4;
pack[0] = ODD(ix1);
}
/* short */
if (*istk(*il + 2 +1) == 2) {
ix2 = *istk(*il +1) * *istk(*il + 1 +1);
ix1 = pack[0] + C2F(allignf)(&ix2, &cx2) / 2;
pack[0] = ODD(ix1);
}
/* int */
if (*istk(*il + 2 +1) == 4) {
ix1 = pack[0] + *istk(*il +1) * *istk(*il + 1 +1);
pack[0] = ODD(ix1);
}
/* unsigned char */
if (*istk(*il + 2 +1) == 11) {
ix2 = *istk(*il +1) * *istk(*il + 1 +1);
ix1 = pack[0] + C2F(allignf)(&ix2, &cx4) / 4;
pack[0] = ODD(ix1);
}
/* unsigned short */
if (*istk(*il + 2 +1) == 12) {
ix2 = *istk(*il +1) * *istk(*il + 1 +1);
ix1 = pack[0] + C2F(allignf)(&ix2, &cx2) / 2;
pack[0] = ODD(ix1);
}
/* unsigned int */
if (*istk(*il + 2 +1) == 14) {
ix1 = pack[0] + *istk(*il +1) * *istk(*il + 1 +1);
pack[0] = ODD(ix1);
}
return ;
}
/* matrix of polynomial case */
static void C2F(ppak)(int *il, int *pack, int *np, int *npMax)
{
int ix1;
static int id, mn;
*np += 2;
if (*np > *npMax) {
return ;
}
id = *il + 8;
mn = *istk(*il +1) * *istk(*il + 1 +1);
ix1 = mn + 9;
pack[0] = ODD(ix1);
pack[1] = (*istk(id + mn ) - 1) * (*istk(*il + 2 +1) + 1);
return ;
}
/* matrix of boolean case */
static void C2F(bpak)(int *il, int *pack, int *np, int *npMax)
{
int ix1;
static int id, mn;
/* Function Body */
*np += 2;
if (*np > *npMax) {
return ;
}
id = *il + 8;
mn = *istk(*il +1) * *istk(*il + 1 +1);
ix1 = mn + 3;
pack[0] = ODD(ix1);
pack[1] = 0;
return ;
}
/* sparse matrix of numbers case */
static void C2F(sppak)(int *il, int *pack, int *np, int *npMax)
{
int ix1;
static int m, n, it;
static int nel;
*np += 2;
if (*np > *npMax) {
return ;
}
nel = *istk(*il + 3 +1);
m = *istk(*il +1);
n = *istk(*il + 1 +1);
it = *istk(*il + 2 +1);
ix1 = m + 5 + nel;
pack[0] = ODD(ix1);
pack[1] = nel * (it + 1);
return ;
}
/* sparse matrix of numbers case */
static void C2F(bsppak)(int *il, int *pack, int *np, int *npMax)
{
int ix1;
static int m, n, it;
static int nel;
*np += 2;
if (*np > *npMax) {
return ;
}
nel = *istk(*il + 3 +1);
m = *istk(*il +1);
n = *istk(*il + 1 +1);
it = *istk(*il + 2 +1);
ix1 = m + 5 + nel;
pack[0] = ODD(ix1);
pack[1] = 0;
return ;
}
/* matrix of strings case */
static void C2F(cpak)(int *il, int *pack, int *np, int *npMax)
{
int ix1;
static int id, mn;
static int vol;
/* Function Body */
*np += 2;
if (*np > *npMax) {
return ;
}
mn = *istk(*il +1) * *istk(*il + 1 +1);
id = *il + 4;
vol = *istk(*il + 4 + mn ) - 1;
ix1 = mn + 5 + vol;
pack[0] = ODD(ix1);
pack[1] = 0;
return ;
}
/* function case (compiled or not) */
static void C2F(mpak)(int *il, int *pack, int *np, int *npMax)
{
int ix1;
static int nout;
static int nt, il1;
static int nin;
*np += 2;
if (*np > *npMax) {
return ;
}
il1 = *il + 1;
nin = *istk(il1 );
il1 = il1 + nin * nsiz + 1;
nout = *istk(il1 );
il1 = il1 + nout * nsiz + 1;
nt = *istk(il1 );
ix1 = nin * nsiz + 4 + nout * nsiz + nt;
pack[0] = ODD(ix1);
pack[1] = 0;
return ;
}
/* library case (compiled or not) */
static void C2F(libpak)(int *il, int *pack, int *np, int *npMax)
{
int ix1;
static int nf, nh, nm;
static int ilh, iln;
*np += 2;
if (*np > *npMax) {
return ;
}
nf = *istk(*il +1);
ilh = *istk(*il + 2 + nf );
nh = *istk(ilh );
iln = ilh + nh + 1;
nm = *istk(iln );
/* write(*,*) nf, nh, nm, istk(iln+1) */
ix1 = nf + 4 + nh + (nm << 1) + 100;
pack[0] = ODD(ix1);
pack[1] = 0;
return ;
}
|