Add convert.h/convert.c

This commit is contained in:
Hugues Delorme 2014-01-22 18:28:58 +01:00
parent 4ce5795b0d
commit 4750e2c989
5 changed files with 41 additions and 26 deletions

View File

@ -4,7 +4,7 @@ CONFIG += warn_on stl rtti exceptions
QT -= core gui
CONFIG(debug, debug|release) {
DEFINES *= _DEBUG_CONFIG_
DEFINES += _DEBUG_CONFIG_
CONFIG += console
}
else {

View File

@ -28,12 +28,14 @@ HEADERS += \
../src/stream.h \
../src/task_control.h \
../src/transfer.h \
../src/convert.h
SOURCES += \
../src/endian.c \
../src/error.c \
../src/stream.c \
../src/task_control.c
../src/task_control.c \
../src/convert.c
*-g++*:QMAKE_CFLAGS += -ansi -pedantic-errors
*-msvc*:QMAKE_CFLAGS += -TC
@ -46,6 +48,7 @@ INSTALLS += global_inc
contains(DATAX, stl) {
dll:DEFINES += FOUG_DATAX_LIBSTL_DLL \
FOUG_DATAX_LIBSTL_MAKE_DLL
#DEFINES += FOUG_STLB_READWRITE_ALIGNSAFE
HEADERS += \
../src/libstl/stl_global.h \

21
src/convert.c Normal file
View File

@ -0,0 +1,21 @@
#include "convert.h"
typedef union
{
uint32_t as_integer;
foug_real32_t as_float;
} _internal_foug_int_real_32_convert_t;
foug_real32_t foug_convert_real32(uint32_t val)
{
_internal_foug_int_real_32_convert_t conv;
conv.as_integer = val;
return conv.as_float;
}
uint32_t foug_convert_uint32(foug_real32_t val)
{
_internal_foug_int_real_32_convert_t conv;
conv.as_float = val;
return conv.as_integer;
}

9
src/convert.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef FOUG_CONVERT_H
#define FOUG_CONVERT_H
#include "global.h"
FOUG_LIB_EXPORT foug_real32_t foug_convert_real32(uint32_t val);
FOUG_LIB_EXPORT uint32_t foug_convert_uint32(foug_real32_t val);
#endif /* FOUG_CONVERT_H */

View File

@ -1,5 +1,7 @@
#include "endian.h"
#include "convert.h"
#include <string.h>
typedef union
@ -8,26 +10,6 @@ typedef union
uint8_t bytes[4];
} _internal_foug_int_bytes_32_convert_t;
typedef union
{
uint32_t as_integer;
foug_real32_t as_float;
} _internal_foug_int_real_32_convert_t;
static foug_real32_t convert_real32(uint32_t val)
{
_internal_foug_int_real_32_convert_t conv;
conv.as_integer = val;
return conv.as_float;
}
static uint32_t convert_uint32(foug_real32_t val)
{
_internal_foug_int_real_32_convert_t conv;
conv.as_float = val;
return conv.as_integer;
}
/*! Endianness (byte order) of the host's CPU architecture */
foug_endianness_t foug_host_endianness()
{
@ -133,23 +115,23 @@ void foug_encode_uint32_be(uint32_t val, uint8_t* bytes)
/*! Read a 32bit real from memory-location \p bytes (little-endian byte order) */
foug_real32_t foug_decode_real32_le(const uint8_t* bytes)
{
return convert_real32(foug_decode_uint32_le(bytes));
return foug_convert_real32(foug_decode_uint32_le(bytes));
}
/*! Read a 32bit real from memory-location \p bytes (mixed-endian byte order) */
foug_real32_t foug_decode_real32_me(const uint8_t* bytes)
{
return convert_real32(foug_decode_uint32_me(bytes));
return foug_convert_real32(foug_decode_uint32_me(bytes));
}
/*! Read a 32bit real from memory-location \p bytes (big-endian byte order) */
foug_real32_t foug_decode_real32_be(const uint8_t* bytes)
{
return convert_real32(foug_decode_uint32_be(bytes));
return foug_convert_real32(foug_decode_uint32_be(bytes));
}
/*! Write 32bit real \p val to the memory location at \p bytes in little-endian byte order */
void foug_encode_real32_le(foug_real32_t val, uint8_t* bytes)
{
foug_encode_uint32_le(convert_uint32(val), bytes);
foug_encode_uint32_le(foug_convert_uint32(val), bytes);
}