github.com/256dpi/max-go@v0.7.0/lib/max/common/ext_byteorder.c (about)

     1  
     2  float byteorder_swapf32(float f){
     3  	unsigned char c,*a;
     4  
     5  	a = (unsigned char *)&f;
     6  	c=a[3]; a[3]=a[0]; a[0]=c;
     7  	c=a[2]; a[2]=a[1]; a[1]=c;
     8  
     9  	return f;
    10  }
    11  
    12  double byteorder_swapf64(double f){
    13  	unsigned char c,*a;
    14  
    15  	a = (unsigned char *)&f;
    16  	c=a[7]; a[7]=a[0]; a[0]=c;
    17  	c=a[6]; a[6]=a[1]; a[1]=c;
    18  	c=a[5]; a[5]=a[2]; a[2]=c;
    19  	c=a[4]; a[4]=a[3]; a[3]=c;
    20  
    21  	return f;
    22  }	
    23  
    24  // these functions are inplace
    25  void byteorder_swap_pointer_16(unsigned char *p)
    26  {
    27  	unsigned char c;
    28  	c=p[1]; p[1]=p[0]; p[0]=c;
    29  }
    30  
    31  void byteorder_swap_pointer_32(unsigned char *p)
    32  {
    33  	unsigned char c;
    34  	c=p[3]; p[3]=p[0]; p[0]=c;
    35  	c=p[2]; p[2]=p[1]; p[1]=c;
    36  }
    37  
    38  void byteorder_swap_pointer_64(unsigned char *p)
    39  {
    40  	unsigned char c;
    41  	c=p[7]; p[7]=p[0]; p[0]=c;
    42  	c=p[6]; p[6]=p[1]; p[1]=c;
    43  	c=p[5]; p[5]=p[2]; p[2]=c;
    44  	c=p[4]; p[4]=p[3]; p[3]=c;
    45  }	
    46  
    47  // these function copy from one pointer to another (src!=dst)
    48  void byteorder_swap_pointer_16_copy(unsigned char *src, unsigned char *dst)
    49  {
    50  	dst[1] = src[0];
    51  	dst[0] = src[1];
    52  }
    53  
    54  void byteorder_swap_pointer_32_copy(unsigned char *src, unsigned char *dst)
    55  {
    56  	dst[3] = src[0];
    57  	dst[2] = src[1];
    58  	dst[1] = src[2];
    59  	dst[0] = src[3];
    60  }
    61  
    62  void byteorder_swap_pointer_64_copy(unsigned char *src, unsigned char *dst)
    63  {
    64  	dst[7] = src[0];
    65  	dst[6] = src[1];
    66  	dst[5] = src[2];
    67  	dst[4] = src[3];
    68  	dst[3] = src[4];
    69  	dst[2] = src[5];
    70  	dst[1] = src[6];
    71  	dst[0] = src[7];
    72  }
    73  
    74  // these functions return floating point values from a byteswapped pointer
    75  float byteorder_swap_pointer_32_to_float32(unsigned char *p)
    76  {
    77  	float f;
    78  	byteorder_swap_pointer_32_copy(p,(unsigned char *)&f);
    79  	return f;
    80  }
    81  
    82  double byteorder_swap_pointer_64_to_float64(unsigned char *p)
    83  {
    84  	double f;
    85  	byteorder_swap_pointer_64_copy(p,(unsigned char *)&f);
    86  	return f;
    87  }
    88