diff options
| author | kx <kx@radix-linux.su> | 2026-10-09 18:00:15 +0300 |
|---|---|---|
| committer | kx <kx@radix-linux.su> | 2026-10-09 18:00:15 +0300 |
| commit | 065da3509019b15dd0e678072e6be629e5330bef (patch) | |
| tree | 69c934c9e786ac048b4fbe8f4c4c5571661521f4 /cpu/cpu-byteorder.c | |
| parent | d130fe27fa753be02ed76fd9b277aa3c3d928eb9 (diff) | |
| download | libmcpu-0.4.0.tar.xz | |
Version 0.4.00.4.0
Diffstat (limited to 'cpu/cpu-byteorder.c')
| -rw-r--r-- | cpu/cpu-byteorder.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/cpu/cpu-byteorder.c b/cpu/cpu-byteorder.c new file mode 100644 index 0000000..ebc612a --- /dev/null +++ b/cpu/cpu-byteorder.c @@ -0,0 +1,116 @@ +/*************************************************************** + __CPU_BYTEORDER.C + + Byte-order accessors for canonical PROGRAM fields and + architectural data-object representation. + + PART OF : CPU - library . + + Copyright (C) 2026 by Andrew V.Kosteltsev. + All Rights Reserved. + ***************************************************************/ + +#include <string.h> +#include <libmcpu.h> + +__mpu_uint16_t +__cpu_data_get_u16( const __mpu_UNIT_t *p ) +{ + __mpu_uint16_t v = 0; + if( p ) memcpy( &v, p, sizeof(v) ); + return( v ); +} + +__mpu_uint32_t +__cpu_data_get_u32( const __mpu_UNIT_t *p ) +{ + __mpu_uint32_t v = 0; + if( p ) memcpy( &v, p, sizeof(v) ); + return( v ); +} + +__mpu_uint64_t +__cpu_data_get_u64( const __mpu_UNIT_t *p ) +{ + __mpu_uint64_t v = 0; + if( p ) memcpy( &v, p, sizeof(v) ); + return( v ); +} + +void +__cpu_data_put_u16( __mpu_UNIT_t *p, __mpu_uint16_t v ) +{ + if( p ) memcpy( p, &v, sizeof(v) ); +} + +void +__cpu_data_put_u32( __mpu_UNIT_t *p, __mpu_uint32_t v ) +{ + if( p ) memcpy( p, &v, sizeof(v) ); +} + +void +__cpu_data_put_u64( __mpu_UNIT_t *p, __mpu_uint64_t v ) +{ + if( p ) memcpy( p, &v, sizeof(v) ); +} + +__mpu_uint16_t +__cpu_code_get_u16( const __mpu_UNIT_t *p ) +{ + if( !p ) return( 0 ); + return (__mpu_uint16_t)(((__mpu_uint16_t)p[0] << 8) | + (__mpu_uint16_t)p[1]); +} + +__mpu_uint32_t +__cpu_code_get_u32( const __mpu_UNIT_t *p ) +{ + __mpu_uint32_t v; + if( !p ) return( 0 ); + v = ((__mpu_uint32_t)p[0] << 24); + v |= ((__mpu_uint32_t)p[1] << 16); + v |= ((__mpu_uint32_t)p[2] << 8); + v |= (__mpu_uint32_t)p[3]; + return( v ); +} + +__mpu_uint64_t +__cpu_code_get_u64( const __mpu_UNIT_t *p ) +{ + __mpu_uint64_t v = 0; + int k; + if( !p ) return( 0 ); + for( k = 0; k < 8; ++k ) v = ( v << 8 ) | p[k]; + return( v ); +} + +void +__cpu_code_put_u16( __mpu_UNIT_t *p, __mpu_uint16_t v ) +{ + if( !p ) return; + p[0] = (__mpu_UNIT_t)(v >> 8); + p[1] = (__mpu_UNIT_t)v; +} + +void +__cpu_code_put_u32( __mpu_UNIT_t *p, __mpu_uint32_t v ) +{ + if( !p ) return; + p[0] = (__mpu_UNIT_t)(v >> 24); + p[1] = (__mpu_UNIT_t)(v >> 16); + p[2] = (__mpu_UNIT_t)(v >> 8); + p[3] = (__mpu_UNIT_t)v; +} + +void +__cpu_code_put_u64( __mpu_UNIT_t *p, __mpu_uint64_t v ) +{ + int k; + if( !p ) return; + for( k = 7; k >= 0; --k ) + { + p[k] = (__mpu_UNIT_t)( v & __mpu_UINT64_C( 0xff ) ); + v >>= 8; + } +} |
