data_conversion.c

Go to the documentation of this file.
00001 
00012 /* $Id: data_conversion.c,v 1.6 2005/11/10 20:46:08 seymour Exp $ */
00013 /* $UTK_Copyright: $ */
00014 
00015 #include "utility.h"
00016 
00019 gs_int_swap_f gs_int_swap_func[4][4] = {
00020    {gs_swap_int_nop,   gs_swap_int_undef, gs_swap_int_undef, gs_swap_int},
00021    {gs_swap_int_undef, gs_swap_int_nop,   gs_swap_int_undef, gs_swap_int_undef},
00022    {gs_swap_int_undef, gs_swap_int_undef, gs_swap_int_nop,   gs_swap_int_undef},
00023    {gs_swap_int,       gs_swap_int_undef, gs_swap_int_undef, gs_swap_int_nop}};
00024 
00027 gs_float_swap_f gs_float_swap_func[4][4] = {
00028    {gs_swap_float_nop,   gs_swap_float_undef, gs_swap_float_undef, gs_swap_float},
00029    {gs_swap_float_undef, gs_swap_float_nop,   gs_swap_float_undef, gs_swap_float_undef},
00030    {gs_swap_float_undef, gs_swap_float_undef, gs_swap_float_nop,   gs_swap_float_undef},
00031    {gs_swap_float,       gs_swap_float_undef, gs_swap_float_undef, gs_swap_float_nop}};
00032 
00035 gs_double_swap_f gs_double_swap_func[4][4] = {
00036    {gs_swap_double_nop,   gs_swap_double_undef, gs_swap_double_undef, gs_swap_double},
00037    {gs_swap_double_undef, gs_swap_double_nop,   gs_swap_double_undef, gs_swap_double_undef},
00038    {gs_swap_double_undef, gs_swap_double_undef, gs_swap_double_nop,   gs_swap_double_undef},
00039    {gs_swap_double,       gs_swap_double_undef, gs_swap_double_undef, gs_swap_double_nop}};
00040 
00049 int
00050 gs_swap_int(int *x)
00051 {
00052   switch(sizeof(int)) {
00053     case 4:
00054       *x = ((*x & 0xFF) << 24) |
00055            ((*x & 0xFF00) << 8) |
00056            ((*x & 0xFF0000) >> 8) |
00057            ((*x & 0xFF000000) >> 24);
00058       return 0;
00059     case 2:
00060       *x = ((*x & 0xFF) << 8) | 
00061            ((*x & 0xFF00) >> 8);
00062       return 0;
00063     default:
00064       ERRPRINTF("Error: unexpected int size\n");
00065       return -1;
00066   }
00067 }
00068 
00080 int
00081 gs_swap_int_nop(int *x)
00082 {
00083   return 0;
00084 }
00085 
00097 int
00098 gs_swap_int_undef(int *x)
00099 {
00100   ERRPRINTF("Error: this conversion is undefined\n");
00101   return -1;
00102 }
00103 
00112 int
00113 gs_swap_float(float *f)
00114 {
00115   int i, size = sizeof(float)-1;
00116   char *p, tmp;
00117 
00118   p = (char*)f;
00119 
00120   for(i=size;i>size/2;i--) {
00121     tmp = p[i];
00122     p[i] = p[size-i];
00123     p[size-i] = tmp;
00124   }
00125 
00126   return 0;
00127 }
00128 
00140 int
00141 gs_swap_float_nop(float *x)
00142 {
00143   return 0;
00144 }
00145 
00157 int
00158 gs_swap_float_undef(float *x)
00159 {
00160   ERRPRINTF("Error: this conversion is undefined\n");
00161   return -1;
00162 }
00163 
00172 int
00173 gs_swap_double(double *d)
00174 {
00175   int i, size = sizeof(double)-1;
00176   char *p, tmp;
00177 
00178   p = (char*)d;
00179 
00180   for(i=size;i>size/2;i--) {
00181     tmp = p[i];
00182     p[i] = p[size-i];
00183     p[size-i] = tmp;
00184   }
00185 
00186   return 0;
00187 }
00188 
00200 int
00201 gs_swap_double_nop(double *d)
00202 {
00203   return 0;
00204 }
00205 
00217 int
00218 gs_swap_double_undef(double *d)
00219 {
00220   ERRPRINTF("Error: this conversion is undefined\n");
00221   return -1;
00222 }