SURFEX v8.1
General documentation of Surfex
swapbytes_ifsaux.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <signal.h>
3 #include <math.h>
4 
5 #include "swapbytes_ifsaux.h"
6 
7 PUBLIC void
8 swap1bytes_ifsaux_(void *v, const int *vlen)
9 {
10  /* does nothing; for convenience */
11 }
12 
13 PUBLIC void
14 swap2bytes_ifsaux_(void *v, const int *vlen)
15 {
16  unsigned short int *u = v;
17  int j,ulen = vlen ? *vlen : 0;
18  for (j=0; j<ulen; j++) {
19  u[j] = bswap16bits(u[j]);
20  }
21 }
22 
23 PUBLIC void
24 swap2bytes_ifsaux_via_copy_(void *v, const void *vin, const int *vlen)
25 {
26  unsigned short int *u = v;
27  const unsigned short int *uin = vin;
28  int j,ulen = vlen ? *vlen : 0;
29  for (j=0; j<ulen; j++) {
30  u[j] = bswap16bits(uin[j]);
31  }
32 }
33 
34 PUBLIC void
35 swap4bytes_ifsaux_(void *v, const int *vlen)
36 {
37  unsigned int *u = v;
38  int j,ulen = vlen ? *vlen : 0;
39  for (j=0; j<ulen; j++) {
40  u[j] = bswap32bits(u[j]);
41  }
42 }
43 
44 PUBLIC void
45 swap4bytes_ifsaux_via_copy_(void *v, const void *vin, const int *vlen)
46 {
47  unsigned int *u = v;
48  const unsigned int *uin = vin;
49  int j,ulen = vlen ? *vlen : 0;
50  for (j=0; j<ulen; j++) {
51  u[j] = bswap32bits(uin[j]);
52  }
53 }
54 
55 PUBLIC void
56 swap8bytes_ifsaux_(void *v, const int *vlen)
57 {
58  u_ll_t *u = v;
59  int j,ulen = vlen ? *vlen : 0;
60  for (j=0; j<ulen; j++) {
61  u[j] = bswap64bits(u[j]);
62  }
63 }
64 
65 PUBLIC void
66 swap8bytes_ifsaux_via_copy_(void *v, const void *vin, const int *vlen)
67 {
68  u_ll_t *u = v;
69  const u_ll_t *uin = vin;
70  int j,ulen = vlen ? *vlen : 0;
71  for (j=0; j<ulen; j++) {
72  u[j] = bswap64bits(uin[j]);
73  }
74 }
75 
76 PUBLIC void
77 swapANYbytes_ifsaux_(void *v, int elsize, int n, unsigned int dtype,
78  const char *dtype_name, const char *varname)
79 {
80  union {
81  odb_types_t t;
82  unsigned int ui;
83  } u;
84  u.ui = (dtype << 8); /* shift over the pmethod-member */
85 #ifdef DEBUG
86  fprintf(stderr,
87  "swapANYbytes_ifsaux(%s:%s): elsize=%d, n=%d, dtype=%u (0x%x)\n",
88  dtype_name, varname, elsize, n, dtype, dtype);
89  fprintf(stderr,
90  "\tpmethod=%u,sign=%u,bswaple=%u,prec=%u,base=%u,other=%u (0x%x)\n",
91  u.t.pmethod,u.t.signbit,u.t.byte_swappable,u.t.precision_bits,
92  u.t.base_type,u.t.other_type);
93 #endif
94  if (u.t.byte_swappable) {
95  double bits = (1 << u.t.precision_bits); /* pow(2,u.t.precision_bits); */
96 #ifdef DEBUG
97  fprintf(stderr,"\tbits=%.20g := 2^%u\n",bits,u.t.precision_bits);
98 #endif
99  if (bits == 16 && elsize == 2) {
100  swap2bytes_ifsaux_(v, &n);
101  }
102  else if (bits == 32 && elsize == 4) {
103  swap4bytes_ifsaux_(v, &n);
104  }
105  else if (bits == 64 && elsize == 8) {
106  swap8bytes_ifsaux_(v, &n);
107  }
108  else {
109  fprintf(stderr,
110  " ***Error in swapANYbytes_ifsaux() [elsize=%d]: Can't swap bytes for dtype=0x%x (type=%s, var='%s')\n",
111  elsize, dtype, dtype_name, varname);
112  fprintf(stderr,
113  "signbit=%u, byte_swappable=%u, precision_bits=%u, base_type=%u, other_type=0x%x (%u)\n",
114  u.t.signbit, u.t.byte_swappable, u.t.precision_bits,
115  u.t.base_type, u.t.other_type, u.t.other_type);
116  RAISE(SIGABRT);
117  }
118  }
119 }
120 
PUBLIC void swap1bytes_ifsaux_(void *v, const int *vlen)
unsigned long long int u_ll_t
Definition: privpub.h:295
PUBLIC void swap4bytes_ifsaux_via_copy_(void *v, const void *vin, const int *vlen)
PUBLIC void swap8bytes_ifsaux_via_copy_(void *v, const void *vin, const int *vlen)
PUBLIC void swap2bytes_ifsaux_via_copy_(void *v, const void *vin, const int *vlen)
ERROR in n
Definition: ecsort_shared.h:90
PUBLIC void swap8bytes_ifsaux_(void *v, const int *vlen)
PUBLIC void swap4bytes_ifsaux_(void *v, const int *vlen)
PUBLIC void swapANYbytes_ifsaux_(void *v, int elsize, int n, unsigned int dtype, const char *dtype_name, const char *varname)
PUBLIC void swap2bytes_ifsaux_(void *v, const int *vlen)
subroutine t(CDPREF, CDSUFF, KCODPA, LDNIVA, PMULTI)
Definition: faicor.F90:567