improve posint morphisms

This commit is contained in:
Michael Sippel 2025-03-18 14:05:42 +01:00
parent 6cfb25a1a7
commit 6f85e004b9
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -14,9 +14,9 @@ morph_nat_as_u64_to_pos ()
return 0; return 0;
``` ```
morph_nat_as_u64_to_pos () morph_nat_as_pos_to_u64 (Endianness:Type)
~ <PosInt 0 LittleEndian> ~ <PosInt 0 Endianness>
~ <Seq~<LengthPrefix x86.UInt64> <Digit 0>~x86.UInt64> ~ <Seq~<LengthPrefix x86.UInt64> <Digit 0>~x86.UInt64>
--> -->
~ x86.UInt64 ~ x86.UInt64
@ -42,14 +42,16 @@ morph_posint_radix_le (SrcRadix:, DstRadix:)
length_prefix_uint64_array_clear( dst ); length_prefix_uint64_array_clear( dst );
if( DstRadix == 0 ) { #if DstRadix==0
length_prefix_uint64_array_push( dst, value ); length_prefix_uint64_array_push( dst, value );
} else if( DstRadix > 0 ) { #else
while( value > 0 ) { if( value == 0 ) {
length_prefix_uint64_array_push( dst, 0 );
} else while( value > 0 ) {
length_prefix_uint64_array_push( dst, value % DstRadix ); length_prefix_uint64_array_push( dst, value % DstRadix );
value /= DstRadix; value /= DstRadix;
} }
} #endif
return 0; return 0;
``` ```
@ -69,13 +71,16 @@ morph_posint_radix_be (SrcRadix:, DstRadix:)
value += src->items[i]; value += src->items[i];
} }
if( DstRadix == 0 ) { #if DstRadix==0
dst->len = 1; dst->len = 1;
dst->items[0] = value; dst->items[0] = value;
} else { #else
uint64_t v = value; uint64_t v = value;
dst->len = 0; dst->len = 0;
while( v ) {
if( v == 0 ) {
dst->len = 1;
} else while( v ) {
dst->len++; dst->len++;
v /= DstRadix; v /= DstRadix;
} }
@ -85,7 +90,7 @@ morph_posint_radix_be (SrcRadix:, DstRadix:)
dst->items[--i] = value % DstRadix; dst->items[--i] = value % DstRadix;
value /= DstRadix; value /= DstRadix;
} }
} #endif
return 0; return 0;
``` ```