summaryrefslogtreecommitdiffstats
path: root/raslib/endian.cc
blob: 45837a1dcc571289bfdd2e5e1faa48ddb422789c (plain)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * INCLUDE: endian.cc
 *
 * MODULE:  raslib
 * CLASS:   r_Endian
 *
 * COMMENTS:
 *		None
*/

#include <string.h>


#include "raslib/endian.hh"
#include "raslib/rmdebug.hh"
#include "raslib/minterval.hh"
#include "raslib/primitivetype.hh"
#include "raslib/structuretype.hh"
#include "raslib/miter.hh"




/*
 *  Inline code used in several places in the r_Endian class
 */

static inline r_Octet eswap( r_Octet val )
{
  return val;
}

static inline void eswap( r_Octet val, void *dest )
{
  r_Char *d = (r_Char *)dest;
  *d = val;
}

static inline r_Short eswap( r_Short val )
{
  return (r_Short)(((val & 0xff) << 8) | ((val >> 8) & 0xff));
}

static inline r_UShort eswap( r_UShort val )
{
  return (r_UShort)(((val & 0xff) << 8) | ((val >> 8) & 0xff));
}

static inline void eswap( r_Short val, void *dest )
{
  r_Short *d = (r_Short *)dest;
  *d = eswap(val);
}

static inline void eswap( r_UShort val, void *dest )
{
  r_UShort *d = (r_UShort *)dest;
  *d = eswap(val);
}

static inline r_Long eswap( r_Long val )
{
  return (r_Long)(((val & 0xff) << 24) | ((val & 0xff00) << 8) |
	       ((val >> 8) & 0xff00) | ((val >> 24) & 0xff));
}

static inline r_ULong eswap( r_ULong val )
{
  return (r_ULong)(((val & 0xff) << 24) | ((val & 0xff00) << 8) |
	       ((val >> 8) & 0xff00) | ((val >> 24) & 0xff));
}

static inline void eswap( r_Long val, void *dest )
{
  r_Long *d = (r_Long *)dest;
  *d = eswap(val);
}

static inline void eswap( r_ULong val, void *dest )
{
  r_ULong *d = (r_ULong *)dest;
  *d = eswap(val);
}

/*
these types are not supported
static inline long eswap( long val )
{
  return (long)(((val & 0xff) << 24) | ((val & 0xff00) << 8) |
		((val >> 8) & 0xff00) | ((val >> 24) & 0xff));
}

static inline r_ULong eswap( r_ULong val )
{
  return (r_ULong)(((val & 0xff) << 24) | ((val & 0xff00) << 8) |
		((val >> 8) & 0xff00) | ((val >> 24) & 0xff));
}

static inline void eswap( long val, void *dest )
{
  long *d = (long *)dest;
  *d = eswap(val);
}

static inline void eswap( r_ULong val, void *dest )
{
  r_ULong *d = (r_ULong *)dest;
  *d = eswap(val);
}
*/

static inline r_Float eswap( r_Float val )
{
  r_Long *ptr = (r_Long*)&val;
  r_Float result;

  eswap(*ptr, &result);

  return result;
}

static inline void eswap( r_Float val, void *dest )
{
  r_Long *ptr = (r_Long*)&val;

  eswap(*ptr, dest);
}


#ifndef __GNUG__
inline
#endif
static r_Double eswap( r_Double val )
{
  r_Long *ptr = (r_Long*)&val;
  r_Double result;
  eswap(ptr[0], ((r_Octet*)&result)+sizeof(r_Long));
  eswap(ptr[1], (r_Octet*)&result);
/*
#ifndef DECALPHA
  eswap(ptr[0], ((r_Octet*)&result)+sizeof(long));
  eswap(ptr[1], (r_Octet*)&result);
#else
  eswap(*ptr, &result);
#endif
*/
  return result;
}

#ifndef __GNUG__
inline
#endif
static void eswap( r_Double val, void *dest )
{
  r_Long *ptr = (r_Long*)&val;
  eswap(ptr[0], ((r_Octet*)dest)+sizeof(r_Long));
  eswap(ptr[1], dest);
/*
#ifndef DECALPHA
  eswap(ptr[0], ((r_Octet*)dest)+sizeof(long));
  eswap(ptr[1], dest);
#else
  eswap(*ptr, dest);
#endif
*/
}


/*
 *  Template functions used throughout the code
 */

// template for special linear iteration
template<class T>
void swap_array_templ( r_Bytes size, T *dest, const T *src)
{
  const T *sptr = src;
  T *dptr = dest;
  r_Bytes ctr;
  
  for (ctr = 0; ctr < size; ctr += sizeof(T), sptr++, dptr++)
    eswap(*sptr, dptr);
}


// template for identical domains for src and dest
template<class T>
void swap_array_templ(r_Miter &iter, T *destBase, const T *srcBase)
{
  while (!iter.isDone())
  {
    const T *src = (const T*)iter.nextCell();
    eswap(*src, destBase + (src - srcBase));
  }
}


// template for generic iteration (src is just a dummy here)
template<class T>
void swap_array_templ(r_Miter &siter, r_Miter &diter, const T *srcBase)
{
  while (!siter.isDone())
  {
    const T *src = (const T*)siter.nextCell();
    T *dest = (T*)diter.nextCell();
    eswap(*src, dest);
  }
}

// force the instantiations; we only need the templates r_Longernally anyway
template void swap_array_templ(r_Bytes, r_Short *, const r_Short *);
template void swap_array_templ(r_Bytes, r_UShort *, const r_UShort *);
template void swap_array_templ(r_Bytes, r_Long *, const r_Long *);
template void swap_array_templ(r_Bytes, r_ULong *, const r_ULong *);
/*
template void swap_array_templ(r_Bytes, long *, const long *);
template void swap_array_templ(r_Bytes, r_ULong *, const r_ULong *);
*/
template void swap_array_templ(r_Bytes, r_Float *, const r_Float *);
template void swap_array_templ(r_Bytes, r_Double *, const r_Double *);
template void swap_array_templ(r_Miter &, r_Octet *, const r_Octet *);
template void swap_array_templ(r_Miter &, r_Short *, const r_Short *);
template void swap_array_templ(r_Miter &, r_Long *, const r_Long *);
template void swap_array_templ(r_Miter &, r_Float *, const r_Float *);
template void swap_array_templ(r_Miter &, r_Double *, const r_Double *);
template void swap_array_templ(r_Miter &, r_Miter &, const r_Octet *);
template void swap_array_templ(r_Miter &, r_Miter &, const r_Short *);
template void swap_array_templ(r_Miter &, r_Miter &, const r_Long *);
template void swap_array_templ(r_Miter &, r_Miter &, const r_Float *);
template void swap_array_templ(r_Miter &, r_Miter &, const r_Double *);





/*
 *  r_Endian members
 */

r_Short r_Endian::swap( r_Short val )
{
  return eswap(val);
}

r_UShort r_Endian::swap( r_UShort val )
{
  return eswap(val);
}

r_Long r_Endian::swap( r_Long val )
{
  return eswap(val);
}

r_ULong r_Endian::swap( r_ULong val )
{
  return eswap(val);
}
/*
long r_Endian::swap( long val )
{
  return eswap(val);
}

r_ULong r_Endian::swap( r_ULong val )
{
  return eswap(val);
}
*/
r_Float r_Endian::swap( r_Float val )
{
  return eswap(val);
}

r_Double r_Endian::swap( r_Double val )
{
  return eswap(val);
}


r_Endian::r_Endianness r_Endian::get_endianness( void )
{
#ifdef LITTLE_ENDIAN
  return r_Endian_Little;
#else
  return r_Endian_Big;
#endif
}



/*
 *  Simplest array swapping: linear buffer filled with atomic type
 */

void r_Endian::swap_array( const r_Primitive_Type *type, r_Bytes size, const void *src, void *dest )
{
  switch (type->type_id())
  {
    case r_Primitive_Type::BOOL:
    case r_Primitive_Type::CHAR:
    case r_Primitive_Type::OCTET:
      if (src != (const void*)dest)
      {
	// change of endianness on 1 byte data is a NULL operation.
	// if src and dest differ, we have to _copy_, though.
	memcpy(dest, src, size);
      }
      break;
    case r_Primitive_Type::SHORT:
      swap_array_templ(size, (r_Short*)dest, (const r_Short*)src);
      break;
    case r_Primitive_Type::USHORT:
      swap_array_templ(size, (r_UShort*)dest, (const r_UShort*)src);
      break;
    case r_Primitive_Type::LONG:
      swap_array_templ(size, (r_Long*)dest, (const r_Long*)src);
      break;
    case r_Primitive_Type::ULONG:
      swap_array_templ(size, (r_ULong*)dest, (const r_ULong*)src);
      break;
    case r_Primitive_Type::FLOAT:
    case r_Primitive_Type::COMPLEXTYPE1:
      swap_array_templ(size, (r_Float*)dest, (const r_Float*)src);
      break;
    case r_Primitive_Type::DOUBLE:
    case r_Primitive_Type::COMPLEXTYPE2:
      swap_array_templ(size, (r_Double*)dest, (const r_Double*)src);
      break;
    default:
      RMDBGONCE(3, RMDebug::module_raslib, "r_Endian", "swap_array(type, size, src, dest) bad typeId " << type->type_id());    
      break;
  }
}


/*
 *  Same functionality as function above, but with the types given implicitly
 *  by the parameters
 */

// Dummies, but useful when templates are used
void r_Endian::swap_array( r_Bytes size, const r_Octet *src, r_Octet *dest )
{
}

void r_Endian::swap_array( r_Bytes size, const r_Char *src, r_Char *dest )
{
}

// Here go the real ones...
void r_Endian::swap_array( r_Bytes size, const r_Short *src, r_Short *dest )
{
  swap_array_templ(size, dest, src);
}

void r_Endian::swap_array( r_Bytes size, const r_UShort *src, r_UShort *dest )
{
  swap_array_templ(size, dest, src);
}

void r_Endian::swap_array( r_Bytes size, const r_Long *src, r_Long *dest )
{
  swap_array_templ(size, dest, src);
}

void r_Endian::swap_array( r_Bytes size, const r_ULong *src, r_ULong *dest )
{
  swap_array_templ(size, dest, src);
}
/*
void r_Endian::swap_array( r_Bytes size, const long *src, long *dest )
{
  swap_array_templ(size, dest, src);
}

void r_Endian::swap_array( r_Bytes size, const r_ULong *src, r_ULong *dest )
{
  swap_array_templ(size, dest, src);
}
*/
void r_Endian::swap_array( r_Bytes size, const r_Float *src, r_Float *dest )
{
  swap_array_templ(size, dest, src);
}

void r_Endian::swap_array( r_Bytes size, const r_Double *src, r_Double *dest )
{
  swap_array_templ(size, dest, src);
}


/*
 *  Same functionality as above, but with the type size given as parameter
 */

void r_Endian::swap_array( r_Bytes size, r_Bytes tsize, const void *src, void *dest )
{
  switch (tsize)
  {
    case 2:
      swap_array_templ(size, (r_Short*)dest, (const r_Short*)src);
      break;
    case 4:
      swap_array_templ(size, (r_Long*)dest, (const r_Long*)src);
      break;
    case 8:
    case 16:	// complexd
      swap_array_templ(size, (r_Double*)dest, (const r_Double*)src);
      break;
    default:
      break;
  }
}



/*
 *  ``Half-generic'': array with arbitrary total domain and iteration
 *  domain, but same domains for src and dest
 */
void r_Endian::swap_array( const r_Primitive_Type *type, const r_Minterval &srcDom, const r_Minterval &srcIterDom, const void *src, void *dest, r_ULong step )
{
  if ((srcDom == srcIterDom) && (step == type->size()))
  {
    r_ULong size = step * srcDom.cell_count();

    swap_array(type, size, src, dest);
  }
  else
  {
    r_Miter iter(&srcIterDom, &srcDom, step, (const char*)src);

    switch (type->type_id())
    {
      case r_Primitive_Type::BOOL:
      case r_Primitive_Type::CHAR:
      case r_Primitive_Type::OCTET:
	if (src != (const void*)dest)
	{
	  swap_array_templ(iter, (r_Octet*)dest, (const r_Octet*)src);
	}
	break;
      case r_Primitive_Type::SHORT:   
	swap_array_templ(iter, (r_Short*)dest, (const r_Short*)src);
	break;
      case r_Primitive_Type::USHORT:
	swap_array_templ(iter, (r_UShort*)dest, (const r_UShort*)src);
	break;
      case r_Primitive_Type::LONG:
	swap_array_templ(iter, (r_Long*)dest, (const r_Long*)src);
	break;
      case r_Primitive_Type::ULONG:
	swap_array_templ(iter, (r_ULong*)dest, (const r_ULong*)src);
	break;
      case r_Primitive_Type::FLOAT:
      case r_Primitive_Type::COMPLEXTYPE1:
	swap_array_templ(iter, (r_Float*)dest, (const r_Float*)src);
	break;
      case r_Primitive_Type::DOUBLE:
      case r_Primitive_Type::COMPLEXTYPE2:
	swap_array_templ(iter, (r_Double*)dest, (const r_Double*)src);
	break;
     default:
        RMDBGONCE(3, RMDebug::module_raslib, "r_Endian", "swap_array(type, srcdom, srciterdom, src,  dest, step) bad typeId " << type->type_id());    
        break;	
    }
  }
}


static void swap_array_struct( const r_Structure_Type *structType, const r_Minterval &srcDom, const r_Minterval &srcIterDom, const void *src, void *dest, r_ULong step )
{
  r_Structure_Type::attribute_iterator iter(structType->defines_attribute_begin());

  while (iter != structType->defines_attribute_end())
  {
    r_Type *newType = (*iter).type_of().clone();
    const void *srcPtr = (const void*)(((const r_Octet*)src) + (*iter).offset());
    void *destPtr = (void*)(((r_Octet*)dest) + (*iter).offset());
    if (newType->isStructType())
      swap_array_struct((const r_Structure_Type*)newType, srcDom, srcIterDom, srcPtr, destPtr, step);
    else
      r_Endian::swap_array((const r_Primitive_Type*)newType, srcDom, srcIterDom, srcPtr, destPtr, step);
    delete newType;
    iter++;
  }
}


void r_Endian::swap_array( const r_Base_Type *type, const r_Minterval &srcDom, const r_Minterval &srcIterDom, const void *src, void *dest )
{
  if (type->isStructType())
    swap_array_struct((const r_Structure_Type*)type, srcDom, srcIterDom, src, dest, type->size());
  else
    swap_array((const r_Primitive_Type*)type, srcDom, srcIterDom, src, dest, type->size());
}




/*
 *  Fully generic: different total domain and iteration domain for both
 *  src and dest. Beware that the number of cells in the iteration domains
 *  for src and dest must be identical!
 */

void r_Endian::swap_array( const r_Primitive_Type *type, const r_Minterval &srcDom, const r_Minterval &srcIterDom, const r_Minterval &destDom, const r_Minterval &destIterDom, const void *src, void *dest, r_ULong step )
{
  /// check if the whole thing reduces to a linear scan
  if ((srcDom == srcIterDom) && (step == type->size()))
  {
    r_ULong size = step * srcDom.cell_count();

    swap_array(type, size, src, dest);
  }
  /// no, generic code...
  else
  {
    r_Miter siter(&srcIterDom, &srcDom, (r_Long)step, (const char*)src);
    r_Miter diter(&destIterDom, &destDom, (r_Long)step, (const char*)dest);

    switch (type->type_id())
    {
      case r_Primitive_Type::BOOL:
      case r_Primitive_Type::CHAR:
      case r_Primitive_Type::OCTET:
	if (src != (const void*)dest)
	{
	  swap_array_templ(siter, diter, (const r_Octet*)src);
	}
	break;
      case r_Primitive_Type::SHORT:
	swap_array_templ(siter, diter, (const r_Short*)src);
	break;
      case r_Primitive_Type::USHORT:
	swap_array_templ(siter, diter, (const r_UShort*)src);
	break;
      case r_Primitive_Type::LONG:
	swap_array_templ(siter, diter, (const r_Long*)src);
	break;
      case r_Primitive_Type::ULONG:
	swap_array_templ(siter, diter, (const r_ULong*)src);
	break;
      case r_Primitive_Type::FLOAT:
      case r_Primitive_Type::COMPLEXTYPE1:
	swap_array_templ(siter, diter, (const r_Float*)src);
	break;
      case r_Primitive_Type::DOUBLE:
      case r_Primitive_Type::COMPLEXTYPE2:
	swap_array_templ(siter, diter, (const r_Double*)src);
	break;
      default:
        RMDBGONCE(3, RMDebug::module_raslib, "r_Endian", "swap_array(type, srcdom, srciterdom, destdom, destiterdom, src,  dest, step) bad typeId " << type->type_id());    
        break;		
    }
  }
}


static void swap_array_struct( const r_Structure_Type *structType, const r_Minterval &srcDom, const r_Minterval &srcIterDom, const r_Minterval &destDom, const r_Minterval &destIterDom, const void *src, void *dest, r_ULong step)
{
  r_Structure_Type::attribute_iterator iter(structType->defines_attribute_begin());

  while (iter != structType->defines_attribute_end())
  {
    r_Type *newType = (*iter).type_of().clone();
    const void *srcPtr = (const void*)(((const r_Octet*)src) + (*iter).offset());
    void *destPtr = (void*)(((r_Octet*)dest) + (*iter).offset());
    if (newType->isStructType())
      swap_array_struct((const r_Structure_Type*)newType, srcDom, srcIterDom, destDom, destIterDom, srcPtr, destPtr, step);
    else
      r_Endian::swap_array((const r_Primitive_Type*)newType, srcDom, srcIterDom, destDom, destIterDom, srcPtr, destPtr, step);
    delete newType;
    iter++;
  }
}

void r_Endian::swap_array( const r_Base_Type *type, const r_Minterval &srcDom, const r_Minterval &srcIterDom, const r_Minterval &destDom, const r_Minterval &destIterDom, const void *src, void *dest )
{
  if (type->isStructType())
    swap_array_struct((const r_Structure_Type*)type, srcDom, srcIterDom, destDom, destIterDom, src, dest, type->size());
  else
    swap_array((const r_Primitive_Type*)type, srcDom, srcIterDom, destDom, destIterDom, src, dest, type->size());
}

std::ostream& operator<<(std::ostream& s, r_Endian::r_Endianness& e)
{
	 switch(e) {
	   case r_Endian::r_Endian_Little:
	   	s << "Little_Endian";
	   	break;
	   case r_Endian::r_Endian_Big:
	 	s << "Big_Endian";	 
	 	break;
	   default:
	  	s << "Unkown r_Endiannes";
	  	break;
	 }
	return s;
}