fstringdouble.c
Go to the documentation of this file.
1 /*
2  FLOAT-VECTOR <---> DOUBLE-VECTOR conversion in x86_64
3 */
4 
5 #include "eus.h"
6 #if (WORD_SIZE == 64)
7 extern pointer makefvector();
8 /*
9  string converted by DV2FSTRING convert to float-vector(vector of 64bit float)
10  float-bytestring2dvector str [ fvec ]
11  str : string or address of float[]
12  fvec : float-vector. If str is address, fvec should be set
13 */
14 static pointer FSTRING2DV(ctx,n,argv)
15 context *ctx;
16 int n;
17 pointer argv[];
18 { pointer f=argv[0],d;
19  double *fp;
20  float *src=NULL;
21  register int i,len=-1;
22 
23  ckarg2(1,2);
24  if (isint (f)) {
25  src = (float *)(intval(f));
26  } else {
27  if (!isstring(f)) error(E_NOSTRING);
28  len = strlength(f)/4;
29  src = (float *)f->c.str.chars;
30  }
31 
32  if (n == 1) {
33  if(len < 0) error(E_FLOATVECTOR);
34  d = makefvector(len);
35  vpush(d);
36  } else {
37  d = argv[1];
38  if (len < 0) len = vecsize(d);
39  if (!isfltvector(d)) error(E_FLOATVECTOR);
40  if (vecsize(d)<len) error(E_ARRAYINDEX);
41  }
42  //for (i=0; i<len; i++) d->c.fvec.fv[i] = 0.0;
43  fp = d->c.fvec.fv;
44  for (i=0; i<len; i++) {
45  *fp++ = (double)src[i];
46  }
47  if(n != 1) return (d);
48  return (vpop());
49 }
50 /*
51  float-vector in x86_64 (vector of 64bit float) convert to float[] as string
52  dvector2float-bytestring fvec [ str ]
53  fvec : float-vector
54  str : string or address of float[]
55 */
56 static pointer DV2FSTRING(ctx,n,argv)
57 context *ctx;
58 int n;
59 pointer argv[];
60 { pointer d=argv[0],f;
61  double *fp;
62  char *buf=NULL;
63  register int i,len;
64  union doublefloat {
65  struct {char a,b,c,d;} cval;
66  float fval;
67  } f2d;
68 
69  ckarg2(1,2);
70  if (!isfltvector(d)) error(E_FLOATVECTOR);
71  len=vecsize(d)*4;
72  fp = d->c.fvec.fv;
73 
74  if (n != 1) {
75  f=argv[1];
76  if (isint(f)) {
77  buf = (char *)intval(f);
78  } else {
79  if (!isstring(f)) error(E_NOSTRING);
80  if (strlength(f)<len) error(E_ARRAYINDEX);
81  }
82  } else {
83  // makestring
84  f=alloc((len+2*sizeof(eusinteger_t))>>WORDSHIFT, ELM_CHAR,
85  stringcp.cix, (len+2*sizeof(eusinteger_t))>>WORDSHIFT );
86  vpush(f);
87  f->c.str.length=makeint(len);
88  f->c.ivec.iv[len/sizeof(eusinteger_t)] = 0; /*terminator*/
89  // memcpy((void *)p->c.str.chars, (void *)s, l);
90  }
91 
92  if(buf == NULL) buf = f->c.str.chars;
93  for (i=0; i<len/4; i++) {
94  f2d.fval = (float)*fp++;
95  *buf++ = f2d.cval.a;
96  *buf++ = f2d.cval.b;
97  *buf++ = f2d.cval.c;
98  *buf++ = f2d.cval.d;
99  }
100  if(n != 1) return(f);
101  return(vpop());
102 }
103 /*
104  string converted by LV2ISTRING convert to integer-vector(vector of 64bit integer)
105  integer-bytestring2lvector str [ ivec ]
106  str : string or address of int[]
107  ivec : integer-vector. If str is address, ivec should be set
108 */
109 static pointer ISTRING2LV(ctx,n,argv)
110 context *ctx;
111 int n;
112 pointer argv[];
113 { pointer f=argv[0],d;
114  long *fp;
115  int *src=NULL;
116  register int i,len=-1;
117 
118  ckarg2(1,2);
119  if (isint (f)) {
120  src = (int *)(intval(f));
121  } else {
122  if (!isstring(f)) error(E_NOSTRING);
123  len = strlength(f)/4;
124  src = (int *)f->c.str.chars;
125  }
126 
127  if (n == 1) {
128  if(len < 0) error(E_NOINTVECTOR);
129  d = makevector(C_INTVECTOR,len);
130  vpush(d);
131  } else {
132  d = argv[1];
133  if (len < 0) len = vecsize(d);
134  if (!isintvector(d)) error(E_NOINTVECTOR);
135  if (vecsize(d)<len) error(E_ARRAYINDEX);
136  }
137  //for (i=0; i<len; i++) d->c.fvec.fv[i] = 0.0;
138  fp = d->c.ivec.iv;
139  for (i=0; i<len; i++) {
140  *fp++ = (long)src[i];
141  }
142  if(n != 1) return d;
143  return(vpop());
144 }
145 /*
146  integer-vector in x86_64 (vector of 64bit integer) convert to int[] as string
147  lvector2integer-bytestring ivec [ str ]
148  ivec : integer-vector
149  str : string or address of int[]
150 */
151 static pointer LV2ISTRING(ctx,n,argv)
152 context *ctx;
153 int n;
154 pointer argv[];
155 { pointer d=argv[0],f;
156  long *fp;
157  char *buf = NULL;
158  register int i,len;
159  union intchar {
160  struct {char a,b,c,d;} cval;
161  int ival;
162  } i2l;
163 
164  ckarg2(1,2);
165  if (!isintvector(d)) error(E_NOINTVECTOR);
166  len=vecsize(d)*4;
167  fp = d->c.ivec.iv;
168 
169  if (n != 1) {
170  f=argv[1];
171  if (isint(f)) {
172  buf = (char *)intval(f);
173  } else {
174  if (!isstring(f)) error(E_NOSTRING);
175  if (strlength(f)<len) error(E_ARRAYINDEX);
176  }
177  } else {
178  // makestring
179  f=alloc((len+2*sizeof(eusinteger_t))>>WORDSHIFT, ELM_CHAR,
180  stringcp.cix, (len+2*sizeof(eusinteger_t))>>WORDSHIFT );
181  vpush(f);
182  f->c.str.length=makeint(len);
183  f->c.ivec.iv[len/sizeof(eusinteger_t)] = 0; /*terminator*/
184  // memcpy((void *)p->c.str.chars, (void *)s, l);
185  }
186  if(buf == NULL) buf = f->c.str.chars;
187  for (i=0; i<len/4; i++) {
188  i2l.ival = (int)*fp++;
189  *buf++ = i2l.cval.a;
190  *buf++ = i2l.cval.b;
191  *buf++ = i2l.cval.c;
192  *buf++ = i2l.cval.d;
193  }
194  if(n != 1) return f;
195  return(vpop());
196 }
197 #endif
198 #ifdef i386
199 extern pointer makefvector();
200 /*
201  string converted by FV2DSTRING convert to float-vector(vector of 32bit float)
202  double-bytestring2fvector str [ fvec ]
203  str : string or address of double[]
204  fvec : float-vector. If str is address, fvec should be set.
205 */
206 static pointer DSTRING2FV(ctx,n,argv)
207 context *ctx;
208 int n;
209 pointer argv[];
210 { pointer f=argv[0], d;
211  float *fp;
212  double *src=NULL;
213  register int i, len=-1;
214 
215  ckarg2(1,2);
216  if (isint (f)) {
217  src = (double *)(intval(f));
218  } else {
219  if (!isstring(f)) error(E_NOSTRING);
220  len = strlength(f)/8;
221  src = (double *)f->c.str.chars;
222  }
223 
224  if (n == 1) {
225  if(len < 0) error(E_FLOATVECTOR);
226  d = makefvector(len);
227  vpush (d);
228  } else {
229  d = argv[1];
230  if (len < 0) len = vecsize(d);
231  if (!isfltvector(d)) error(E_FLOATVECTOR);
232  if (vecsize(d)<len) error(E_ARRAYINDEX);
233  }
234  //for (i=0; i<len; i++) d->c.fvec.fv[i] = 0.0;
235  fp = d->c.fvec.fv;
236  for (i = 0; i < len; i++) {
237  *fp++ = (float)src[i];
238  }
239  if(n != 1) return (d);
240  return (vpop());
241 }
242 /*
243  float-vector in i386 (vector of 32bit float) convert to double[] as string
244  fvector2double-bytestring fvec [ str ]
245  fvec : float-vector
246  str : string or address of double[]
247 */
248 static pointer FV2DSTRING(ctx,n,argv)
249 context *ctx;
250 int n;
251 pointer argv[];
252 { pointer d=argv[0], f;
253  float *fp;
254  char *buf = NULL;
255  register int i, len;
256  union doublefloat {
257  struct {char a,b,c,d,e,f,g,h;} cval;
258  double fval;
259  } f2d;
260 
261  ckarg2(1,2);
262  if (!isfltvector(d)) error(E_FLOATVECTOR);
263  len = vecsize(d) * 8;
264  fp = d->c.fvec.fv;
265 
266  if (n != 1) {
267  f=argv[1];
268  if (isint(f)) {
269  buf = (char *)intval(f);
270  } else {
271  if (!isstring(f)) error(E_NOSTRING);
272  if (strlength(f)<len) error(E_ARRAYINDEX);
273  }
274  } else {
275  f=alloc((len+2*sizeof(eusinteger_t))>>WORDSHIFT, ELM_CHAR,
276  stringcp.cix, (len+2*sizeof(eusinteger_t))>>WORDSHIFT);
277  vpush(f);
278  f->c.str.length = makeint(len);
279  f->c.ivec.iv[len/sizeof(eusinteger_t)] = 0; /*terminator*/
280  // memcpy((void *)p->c.str.chars, (void *)s, l);
281  }
282 
283  if(buf == NULL) buf = f->c.str.chars;
284  for (i=0; i < len/8; i++) {
285  f2d.fval = (double)*fp++;
286  *buf++ = f2d.cval.a;
287  *buf++ = f2d.cval.b;
288  *buf++ = f2d.cval.c;
289  *buf++ = f2d.cval.d;
290  *buf++ = f2d.cval.e;
291  *buf++ = f2d.cval.f;
292  *buf++ = f2d.cval.g;
293  *buf++ = f2d.cval.h;
294  }
295  if(n != 1) return(f);
296  return(vpop());
297 }
298 /*
299  string converted by IV2LSTRING convert to integer-vector(vector of 32bit integer)
300  long-long-bytestring2ivector str [ ivec ]
301  str : string or address of int[]
302  ivec : integer-vector. If str is address, ivec should be set
303 */
304 static pointer LSTRING2IV(ctx,n,argv)
305 context *ctx;
306 int n;
307 pointer argv[];
308 { pointer f=argv[0],d;
309  int *fp;
310  long long *src=NULL;
311  register int i, len=-1;
312 
313  ckarg2(1,2);
314  if (isint (f)) {
315  src = (long long *)(intval(f));
316  } else {
317  if (!isstring(f)) error(E_NOSTRING);
318  len = strlength(f) / 8;
319  src = (long long *)f->c.str.chars;
320  }
321 
322  if (n == 1) {
323  if(len < 0) error(E_NOINTVECTOR);
324  d = makevector(C_INTVECTOR, len);
325  vpush(d);
326  } else {
327  d = argv[1];
328  if (len < 0) len = vecsize(d);
329  if (!isintvector(d)) error(E_NOINTVECTOR);
330  if (vecsize(d)<len) error(E_ARRAYINDEX);
331  }
332  fp = d->c.ivec.iv;
333  for (i = 0; i < len; i++) {
334  *fp++ = (int)src[i];
335  }
336  if(n != 1) return d;
337  return (vpop());
338 }
339 /*
340  integer-vector in i386 (vector of 32bit integer) convert to long long[] as string
341  ivector2long-long-bytestring ivec [ str ]
342  ivec : integer-vector
343  str : string or address of long long[]
344 */
345 static pointer IV2LSTRING(ctx,n,argv)
346 context *ctx;
347 int n;
348 pointer argv[];
349 { pointer d = argv[0],f;
350  int *fp;
351  char *buf = NULL;
352  register int i, len;
353  union intchar {
354  struct {char a,b,c,d,e,f,g,h;} cval;
355  long long ival;
356  } i2l;
357 
358  ckarg2(1,2);
359  if (!isintvector(d)) error(E_NOINTVECTOR);
360  len = vecsize(d) * 8;
361  fp = d->c.ivec.iv;
362 
363  if (n != 1) {
364  f = argv[1];
365  if (isint(f)) {
366  buf = (char *)intval(f);
367  } else {
368  if (!isstring(f)) error(E_NOSTRING);
369  if (strlength(f)<len) error(E_ARRAYINDEX);
370  }
371  } else {
372  // makestring
373  f=alloc((len+2*sizeof(eusinteger_t))>>WORDSHIFT, ELM_CHAR,
374  stringcp.cix, (len+2*sizeof(eusinteger_t))>>WORDSHIFT);
375  vpush(f);
376  f->c.str.length = makeint(len);
377  f->c.ivec.iv[len/sizeof(eusinteger_t)] = 0; /*terminator*/
378  // memcpy((void *)p->c.str.chars, (void *)s, l);
379  }
380  if(buf == NULL) buf = f->c.str.chars;
381  for (i=0; i<len/4; i++) {
382  i2l.ival = (int)*fp++;
383  *buf++ = i2l.cval.a;
384  *buf++ = i2l.cval.b;
385  *buf++ = i2l.cval.c;
386  *buf++ = i2l.cval.d;
387  *buf++ = i2l.cval.e;
388  *buf++ = i2l.cval.f;
389  *buf++ = i2l.cval.g;
390  *buf++ = i2l.cval.h;
391  }
392  if(n != 1) return f;
393  return (vpop());
394 }
395 #endif
396 int fstringdouble(ctx,n,argv)
397 context *ctx;
398 int n;
399 pointer argv[];
400 { pointer mod=argv[0];
401 #if (WORD_SIZE == 64)
402  defun(ctx,"FLOAT-BYTESTRING2DVECTOR",mod,FSTRING2DV,NULL);
403  defun(ctx,"DVECTOR2FLOAT-BYTESTRING",mod,DV2FSTRING,NULL);
404  defun(ctx,"INTEGER-BYTESTRING2LVECTOR",mod,ISTRING2LV,NULL);
405  defun(ctx,"LVECTOR2INTEGER-BYTESTRING",mod,LV2ISTRING,NULL);
406 #endif
407 #ifdef i386
408  defun(ctx,"DOUBLE-BYTESTRING2FVECTOR",mod,DSTRING2FV,NULL);
409  defun(ctx,"FVECTOR2DOUBLE-BYTESTRING",mod,FV2DSTRING,NULL);
410  defun(ctx,"LONG-LONG-BYTESTRING2IVECTOR",mod,LSTRING2IV,NULL);
411  defun(ctx,"IVECTOR2LONG-LONG-BYTESTRING",mod,IV2LSTRING,NULL);
412 #endif
413  }
eusinteger_t iv[1]
Definition: eus.h:303
d
f
#define makeint(v)
Definition: sfttest.c:2
Definition: eus.h:522
static long(* g)()
Definition: test_foreign.c:158
struct string str
Definition: eus.h:400
byte chars[1]
Definition: eus.h:210
GLfloat n[6][3]
Definition: cube.c:15
#define intval(p)
Definition: sfttest.c:1
defun("ADR_TO_STRING", mod, ADR_TO_STRING)
pointer makevector(pointer, int)
Definition: makes.c:417
struct intvector ivec
Definition: eus.h:414
union cell::cellunion c
Definition: eus.h:379
pointer alloc(int, int, int, int)
Definition: memory.mutex.c:241
pointer error(enum errorcode ec,...) pointer error(va_alist) va_dcl
Definition: eus.c:297
long eusinteger_t
Definition: eus.h:19
cixpair stringcp
Definition: eus.c:91
int fstringdouble(context *ctx, int n, argv)
#define NULL
Definition: transargv.c:8
pointer C_INTVECTOR
Definition: eus.c:146
eusfloat_t fv[1]
Definition: eus.h:307
short cix
Definition: eus.h:451
static char buf[CHAR_SIZE]
Definition: helpsub.c:23
pointer makefvector()
char a[26]
Definition: freq.c:4
struct floatvector fvec
Definition: eus.h:413


euslisp
Author(s): Toshihiro Matsui
autogenerated on Fri Feb 21 2020 03:20:54