ldmc/morphisms/posint.morphism-base

119 lines
2.9 KiB
Text
Raw Normal View History

2025-03-15 18:28:57 +01:00
```
#include <array/length-prefix.h>
```
morph_nat_as_u64_to_pos ()
2025-03-20 16:28:16 +01:00
~ native.UInt64
-->
~ <PosInt 0 LittleEndian>
2025-03-20 16:28:16 +01:00
~ <Seq~<LengthPrefix native.UInt64> <Digit 0>~native.UInt64>
```
dst->len = 1;
dst->items[0] = *src;
return 0;
```
2025-03-18 14:05:42 +01:00
morph_nat_as_pos_to_u64 (Endianness:Type)
2025-03-18 14:05:42 +01:00
~ <PosInt 0 Endianness>
2025-03-20 16:28:16 +01:00
~ <Seq~<LengthPrefix native.UInt64> <Digit 0>~native.UInt64>
-->
2025-03-20 16:28:16 +01:00
~ native.UInt64
```
*dst = src->items[0];
return 0;
```
morph_posint_radix_le (SrcRadix:, DstRadix:)
2025-01-28 22:33:08 +01:00
~ <PosInt SrcRadix LittleEndian>
2025-03-20 16:28:16 +01:00
~ <Seq~<LengthPrefix native.UInt64> <Digit SrcRadix>~native.UInt64>
2025-01-28 22:33:08 +01:00
-->
~ <PosInt DstRadix LittleEndian>
2025-03-20 16:28:16 +01:00
~ <Seq~<LenghtPrefix native.UInt64> <Digit DstRadix>~native.UInt64>
```
uint64_t value = 0;
for( uint64_t i = 0; i < src->len; ++i ) {
value *= SrcRadix;
value += src->items[src->len - i - 1];
}
length_prefix_uint64_t_array_uint64_t_clear( dst );
2025-03-18 14:05:42 +01:00
#if DstRadix==0
length_prefix_uint64_t_array_uint64_t_push( dst, value );
2025-03-18 14:05:42 +01:00
#else
if( value == 0 ) {
length_prefix_uint64_t_array_uint64_t_push( dst, 0 );
2025-03-18 14:05:42 +01:00
} else while( value > 0 ) {
length_prefix_uint64_t_array_uint64_t_push( dst, value % DstRadix );
value /= DstRadix;
}
2025-03-18 14:05:42 +01:00
#endif
return 0;
```
2025-01-28 22:33:08 +01:00
morph_posint_radix_be (SrcRadix:, DstRadix:)
~ <PosInt SrcRadix BigEndian>
2025-03-20 16:28:16 +01:00
~ <Seq~<LengthPrefix native.UInt64> <Digit SrcRadix>~native.UInt64>
-->
~ <PosInt DstRadix BigEndian>
2025-03-20 16:28:16 +01:00
~ <Seq~<LengthPrefix native.UInt64> <Digit DstRadix>~native.UInt64>
```
uint64_t value = 0;
for( uint64_t i = 0; i < src->len; ++i ) {
value *= SrcRadix;
value += src->items[i];
}
2025-03-18 14:05:42 +01:00
#if DstRadix==0
dst->len = 1;
dst->items[0] = value;
2025-03-18 14:05:42 +01:00
#else
uint64_t v = value;
dst->len = 0;
2025-03-18 14:05:42 +01:00
if( v == 0 ) {
dst->len = 1;
} else while( v ) {
dst->len++;
v /= DstRadix;
}
uint64_t i = dst->len;
while( value > 0 ) {
dst->items[--i] = value % DstRadix;
value /= DstRadix;
}
2025-03-18 14:05:42 +01:00
#endif
return 0;
```
2025-01-28 22:33:08 +01:00
morph_posint_endianness (Radix:)
~ <PosInt Radix LittleEndian>
2025-03-20 16:28:16 +01:00
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~ native.UInt64>
2025-01-28 22:33:08 +01:00
-->
~ <PosInt Radix BigEndian>
2025-03-20 16:28:16 +01:00
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~ native.UInt64>
```
return length_prefix_uint64_t_array_uint64_t_reverse( src, dst );
```
2025-01-28 22:33:08 +01:00
morph_posint_endianness (Radix:)
~ <PosInt Radix BigEndian>
2025-03-20 16:28:16 +01:00
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~ native.UInt64>
2025-01-28 22:33:08 +01:00
-->
~ <PosInt Radix LittleEndian>
2025-03-20 16:28:16 +01:00
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~ native.UInt64>
```
return length_prefix_uint64_t_array_uint64_t_reverse( src, dst );
```