meta: move endian functions from object to core

This commit is contained in:
2024-12-10 22:26:52 +00:00
parent 96308b2965
commit 7452491427
5 changed files with 273 additions and 42 deletions

View File

@@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.25)
project(bluelib C)
include (TestBigEndian)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(b_modules core object term cmd)

View File

@@ -36,6 +36,15 @@ function(add_bluelib_module)
set_target_properties(blue-${module_name} PROPERTIES FOLDER "Shared/${module_name}")
set_target_properties(blue-${module_name}-s PROPERTIES FOLDER "Static/${module_name}")
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
if(IS_BIG_ENDIAN)
target_compile_definitions(blue-${module_name} PRIVATE BIG_ENDIAN)
target_compile_definitions(blue-${module_name}-s PRIVATE BIG_ENDIAN)
else()
target_compile_definitions(blue-${module_name} PRIVATE LITTLE_ENDIAN)
target_compile_definitions(blue-${module_name}-s PRIVATE LITTLE_ENDIAN)
endif()
install(TARGETS blue-${module_name} blue-${module_name}-s)
install(FILES ${root_header} DESTINATION include/blue)

213
core/endian.c Normal file
View File

@@ -0,0 +1,213 @@
#include <blue/core/endian.h>
b_i16 b_i16_htob(uint16_t v)
{
b_i16 x;
#ifdef BIG_ENDIAN
x.i_uval = v;
#else
uint8_t *b = (uint8_t *)&v;
x.i_bytes[0] = b[1];
x.i_bytes[1] = b[0];
#endif
return x;
}
b_i16 b_i16_htos(uint16_t v)
{
b_i16 x;
#ifdef LITTLE_ENDIAN
x.i_uval = v;
#else
uint8_t *b = (uint8_t *)&v;
x.i_bytes[0] = b[1];
x.i_bytes[1] = b[0];
#endif
return x;
}
uint16_t b_i16_btoh(b_i16 v)
{
uint16_t x;
#ifdef BIG_ENDIAN
x = v.i_uval;
#else
uint8_t *b = (uint8_t *)&x;
b[0] = v.i_bytes[1];
b[1] = v.i_bytes[0];
#endif
return x;
}
uint16_t b_i16_stoh(b_i16 v)
{
uint16_t x;
#ifdef LITTLE_ENDIAN
x = v.i_uval;
#else
uint8_t *b = (uint8_t *)&x;
b[0] = v.i_bytes[1];
b[1] = v.i_bytes[0];
#endif
return x;
}
b_i32 b_i32_htob(uint32_t v)
{
b_i32 x;
#ifdef BIG_ENDIAN
x.i_uval = v;
#else
uint8_t *b = (uint8_t *)&v;
x.i_bytes[0] = b[3];
x.i_bytes[1] = b[2];
x.i_bytes[2] = b[1];
x.i_bytes[3] = b[0];
#endif
return x;
}
b_i32 b_i32_htos(uint32_t v)
{
b_i32 x;
#ifdef LITTLE_ENDIAN
x.i_uval = v;
#else
uint8_t *b = (uint8_t *)&v;
x.i_bytes[0] = b[3];
x.i_bytes[1] = b[2];
x.i_bytes[2] = b[1];
x.i_bytes[3] = b[0];
#endif
return x;
}
uint32_t b_i32_btoh(b_i32 v)
{
uint32_t x;
#ifdef BIG_ENDIAN
x = v.i_uval;
#else
uint8_t *b = (uint8_t *)&x;
b[0] = v.i_bytes[3];
b[1] = v.i_bytes[2];
b[2] = v.i_bytes[1];
b[3] = v.i_bytes[0];
#endif
return x;
}
uint32_t b_i32_stoh(b_i32 v)
{
uint32_t x;
#ifdef LITTLE_ENDIAN
x = v.i_uval;
#else
uint8_t *b = (uint8_t *)&x;
b[0] = v.i_bytes[3];
b[1] = v.i_bytes[2];
b[2] = v.i_bytes[1];
b[3] = v.i_bytes[0];
#endif
return x;
}
b_i64 b_i64_htob(uint64_t v)
{
b_i64 x;
#ifdef BIG_ENDIAN
x.i_uval = v;
#else
uint8_t *b = (uint8_t *)&v;
x.i_bytes[0] = b[7];
x.i_bytes[1] = b[6];
x.i_bytes[2] = b[5];
x.i_bytes[3] = b[4];
x.i_bytes[4] = b[3];
x.i_bytes[5] = b[2];
x.i_bytes[6] = b[1];
x.i_bytes[7] = b[0];
#endif
return x;
}
b_i64 b_i64_htos(uint64_t v)
{
b_i64 x;
#ifdef LITTLE_ENDIAN
x.i_uval = v;
#else
uint8_t *b = (uint8_t *)&v;
x.i_bytes[0] = b[7];
x.i_bytes[1] = b[6];
x.i_bytes[2] = b[5];
x.i_bytes[3] = b[4];
x.i_bytes[4] = b[3];
x.i_bytes[5] = b[2];
x.i_bytes[6] = b[1];
x.i_bytes[7] = b[0];
#endif
return x;
}
uint64_t b_i64_btoh(b_i64 v)
{
uint64_t x;
#ifdef BIG_ENDIAN
x = v.i_uval;
#else
uint8_t *b = (uint8_t *)&x;
b[0] = v.i_bytes[7];
b[1] = v.i_bytes[6];
b[2] = v.i_bytes[5];
b[3] = v.i_bytes[4];
b[4] = v.i_bytes[3];
b[5] = v.i_bytes[2];
b[6] = v.i_bytes[1];
b[7] = v.i_bytes[0];
#endif
return x;
}
uint64_t b_i64_stoh(b_i64 v)
{
uint64_t x;
#ifdef LITTLE_ENDIAN
x = v.i_uval;
#else
uint8_t *b = (uint8_t *)&x;
b[0] = v.i_bytes[7];
b[1] = v.i_bytes[6];
b[2] = v.i_bytes[5];
b[3] = v.i_bytes[4];
b[4] = v.i_bytes[3];
b[5] = v.i_bytes[2];
b[6] = v.i_bytes[1];
b[7] = v.i_bytes[0];
#endif
return x;
}

View File

@@ -0,0 +1,49 @@
#ifndef BLUELIB_CORE_ENDIAN_H_
#define BLUELIB_CORE_ENDIAN_H_
#include <blue/core/misc.h>
#include <stdint.h>
typedef struct {
union {
unsigned char i_bytes[sizeof(uint16_t)];
int16_t i_val;
uint16_t i_uval;
};
} b_i16;
typedef struct {
union {
unsigned char i_bytes[sizeof(uint32_t)];
int32_t i_val;
uint32_t i_uval;
};
} b_i32;
typedef struct {
union {
unsigned char i_bytes[sizeof(uint64_t)];
int64_t i_val;
uint64_t i_uval;
};
} b_i64;
BLUE_API b_i16 b_i16_htob(uint16_t v);
BLUE_API b_i16 b_i16_htos(uint16_t v);
BLUE_API uint16_t b_i16_btoh(b_i16 v);
BLUE_API uint16_t b_i16_stoh(b_i16 v);
BLUE_API b_i32 b_i32_htob(uint32_t v);
BLUE_API b_i32 b_i32_htos(uint32_t v);
BLUE_API uint32_t b_i32_btoh(b_i32 v);
BLUE_API uint32_t b_i32_stoh(b_i32 v);
BLUE_API b_i64 b_i64_htob(uint64_t v);
BLUE_API b_i64 b_i64_htos(uint64_t v);
BLUE_API uint64_t b_i64_btoh(b_i64 v);
BLUE_API uint64_t b_i64_stoh(b_i64 v);
#endif

View File

@@ -66,30 +66,6 @@ typedef enum b_number_type {
B_NUMBER_QWORD = B_NUMBER_INT64,
} b_number_type;
typedef struct {
union {
unsigned char i_bytes[sizeof(uint16_t)];
int16_t i_val;
uint16_t i_uval;
};
} b_i16;
typedef struct {
union {
unsigned char i_bytes[sizeof(uint32_t)];
int32_t i_val;
uint32_t i_uval;
};
} b_i32;
typedef struct {
union {
unsigned char i_bytes[sizeof(uint64_t)];
int64_t i_val;
uint64_t i_uval;
};
} b_i64;
BLUE_API b_number *b_number_create(b_number_type type, void *value_ptr);
static inline b_number *b_number_retain(b_number *number)
{
@@ -264,22 +240,4 @@ BLUE_API bool b_number_is_float(const b_number *number);
BLUE_API size_t b_number_data_size(const b_number *number);
BLUE_API b_i16 b_i16_htob(uint16_t v);
BLUE_API b_i16 b_i16_htos(uint16_t v);
BLUE_API uint16_t b_i16_btoh(b_i16 v);
BLUE_API uint16_t b_i16_stoh(b_i16 v);
BLUE_API b_i32 b_i32_htob(uint32_t v);
BLUE_API b_i32 b_i32_htos(uint32_t v);
BLUE_API uint32_t b_i32_btoh(b_i32 v);
BLUE_API uint32_t b_i32_stoh(b_i32 v);
BLUE_API b_i64 b_i64_htob(uint64_t v);
BLUE_API b_i64 b_i64_htos(uint64_t v);
BLUE_API uint64_t b_i64_btoh(b_i64 v);
BLUE_API uint64_t b_i64_stoh(b_i64 v);
#endif