[lib] Add float endianess converters

This commit is contained in:
Matteo Cypriani 2011-04-07 14:31:31 +02:00
parent 7499ef9ec5
commit 2ce238984c
2 changed files with 41 additions and 0 deletions

View File

@ -17,6 +17,8 @@
#include <net/if.h>
#include <iwlib.h>
#include <assert.h>
#define DEBUG
@ -237,6 +239,10 @@ owl_timestamp owl_time_elapsed(const owl_timestamp d1,
}
/* *** Endianess *** */
/*
* Converts a owl_timestamp from host endianess to network endianess.
*/
@ -261,6 +267,29 @@ owl_timestamp owl_ntoh_timestamp(const owl_timestamp d)
}
/*
* Swap bytes composing a float.
* You probably want to use the macros owl_htonf() and owl_ntohf()
* instead of this function.
*/
float owl_swap_float(const float f)
{
float ret ;
char
*f_bytes = (char*) &f,
*ret_bytes = (char*) &ret ;
assert(sizeof(float) == 4) ;
ret_bytes[0] = f_bytes[3] ;
ret_bytes[1] = f_bytes[2] ;
ret_bytes[2] = f_bytes[1] ;
ret_bytes[3] = f_bytes[0] ;
return ret ;
}
/* *** Network *** */

View File

@ -252,8 +252,20 @@ uint_fast32_t owl_time_elapsed_ms(const owl_timestamp d1,
const owl_timestamp d2) ;
owl_timestamp owl_time_elapsed(const owl_timestamp d1,
const owl_timestamp d2) ;
// Endianess
owl_timestamp owl_hton_timestamp(const owl_timestamp d) ;
owl_timestamp owl_ntoh_timestamp(const owl_timestamp d) ;
float owl_swap_float(const float f) ;
#if __BYTE_ORDER == __BIG_ENDIAN
# define owl_htonf(f) (f)
# define owl_ntohf(f) (f)
#else // __BYTE_ORDER == __BIG_ENDIAN
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define owl_htonf(f) owl_swap_float(f)
# define owl_ntohf(f) owl_swap_float(f)
# endif // __BYTE_ORDER == __LITTLE_ENDIAN
#endif // __BYTE_ORDER == __BIG_ENDIAN
// Network
int owl_create_udp_trx_socket(const char *const server_address,