ldmc/morphisms/posint.morphism-base

118 lines
2.9 KiB
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

```
#include <array/length-prefix.h>
```
morph_nat_as_u64_to_pos ()
~ native.UInt64
-->
~ <PosInt 0 LittleEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit 0>~native.UInt64>
```
dst->len = 1;
dst->items[0] = *src;
return 0;
```
morph_nat_as_pos_to_u64 (Endianness:Type)
~ <PosInt 0 Endianness>
~ <Seq~<LengthPrefix native.UInt64> <Digit 0>~native.UInt64>
-->
~ native.UInt64
```
*dst = src->items[0];
return 0;
```
morph_posint_radix_le (SrcRadix:, DstRadix:)
~ <PosInt SrcRadix LittleEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit SrcRadix>~native.UInt64>
-->
~ <PosInt DstRadix LittleEndian>
~ <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 );
#if DstRadix==0
length_prefix_uint64_t_array_uint64_t_push( dst, value );
#else
if( value == 0 ) {
length_prefix_uint64_t_array_uint64_t_push( dst, 0 );
} else while( value > 0 ) {
length_prefix_uint64_t_array_uint64_t_push( dst, value % DstRadix );
value /= DstRadix;
}
#endif
return 0;
```
morph_posint_radix_be (SrcRadix:, DstRadix:)
~ <PosInt SrcRadix BigEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit SrcRadix>~native.UInt64>
-->
~ <PosInt DstRadix BigEndian>
~ <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];
}
#if DstRadix==0
dst->len = 1;
dst->items[0] = value;
#else
uint64_t v = value;
dst->len = 0;
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;
}
#endif
return 0;
```
morph_posint_endianness (Radix:)
~ <PosInt Radix LittleEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~ native.UInt64>
-->
~ <PosInt Radix BigEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~ native.UInt64>
```
return length_prefix_uint64_t_array_uint64_t_reverse( src, dst );
```
morph_posint_endianness (Radix:)
~ <PosInt Radix BigEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~ native.UInt64>
-->
~ <PosInt Radix LittleEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~ native.UInt64>
```
return length_prefix_uint64_t_array_uint64_t_reverse( src, dst );
```