summaryrefslogtreecommitdiff
path: root/cpu/cpu-byteorder.c
blob: ebc612a3718d0c4e86de903ed0218e4a4c3c0349 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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;
  }
}