Fast Auxiliary Space Preconditioning  1.8.4 Feb/15/2016
convert.c
Go to the documentation of this file.
1 
6 #include "fasp.h"
7 #include "fasp_functs.h"
8 
9 /*---------------------------------*/
10 /*-- Public Functions --*/
11 /*---------------------------------*/
12 
25 unsigned long fasp_aux_change_endian4 (unsigned long x)
26 {
27  unsigned char *ptr = (unsigned char *)&x;
28  return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
29 }
30 
43 double fasp_aux_change_endian8 (double x)
44 {
45  double dbl;
46  unsigned char *bytes, *buffer;
47 
48  buffer=(unsigned char *)&dbl;
49  bytes=(unsigned char *)&x;
50 
51  buffer[0]=bytes[7];
52  buffer[1]=bytes[6];
53  buffer[2]=bytes[5];
54  buffer[3]=bytes[4];
55  buffer[4]=bytes[3];
56  buffer[5]=bytes[2];
57  buffer[6]=bytes[1];
58  buffer[7]=bytes[0];
59  return dbl;
60 }
61 
74 double fasp_aux_bbyteToldouble (unsigned char bytes[])
75 {
76  double dbl;
77  unsigned char *buffer;
78  buffer=(unsigned char *)&dbl;
79  buffer[0]=bytes[7];
80  buffer[1]=bytes[6];
81  buffer[2]=bytes[5];
82  buffer[3]=bytes[4];
83  buffer[4]=bytes[3];
84  buffer[5]=bytes[2];
85  buffer[6]=bytes[1];
86  buffer[7]=bytes[0];
87  return dbl;
88 }
89 
106  const INT ilength,
107  const INT endianflag)
108 {
109  INT iretVal,i;
110  char *intToConvert = ( char* ) & inum;
111  char *returnInt = ( char* ) & iretVal;
112 
113  if (endianflag==1) return inum;
114  else {
115  for (i = 0; i < ilength; i++) {
116  returnInt[i] = intToConvert[ilength-i-1];
117  }
118  return iretVal;
119  }
120 }
121 
138  const INT vlength,
139  const INT endianflag)
140 {
141  REAL dretVal;
142  char *realToConvert = (char *) & rnum;
143  char *returnReal = (char *) & dretVal;
144  INT i;
145 
146  if (endianflag==1) return rnum;
147  else {
148  for (i = 0; i < vlength; i++) {
149  returnReal[i] = realToConvert[vlength-i-1];
150  }
151  return dretVal;
152  }
153 }
154 
155 /*---------------------------------*/
156 /*-- End of File --*/
157 /*---------------------------------*/
#define REAL
Definition: fasp.h:67
unsigned long fasp_aux_change_endian4(unsigned long x)
Swap order for different endian systems.
Definition: convert.c:25
double fasp_aux_change_endian8(double x)
Swap order for different endian systems.
Definition: convert.c:43
INT endian_convert_int(const INT inum, const INT ilength, const INT endianflag)
Swap order of an INT number.
Definition: convert.c:105
#define INT
Definition: fasp.h:64
Main header file for FASP.
INT ilength
Definition: io.c:13
REAL endian_convert_real(const REAL rnum, const INT vlength, const INT endianflag)
Swap order of a REAL number.
Definition: convert.c:137
double fasp_aux_bbyteToldouble(unsigned char bytes[])
Swap order of double-precision float for different endian systems.
Definition: convert.c:74