read morphism body definitions from morphism-base file and generate instantiations based on type-variable substitutions
This commit is contained in:
parent
3f4a99ad79
commit
cea1f36e63
8 changed files with 244 additions and 255 deletions
morphisms
include
morphism-base
src
|
@ -63,17 +63,3 @@ int length_prefix_array_map_64_to_8(
|
|||
struct LengthPrefixUInt64Array const * restrict src,
|
||||
struct LengthPrefixUInt8Array * restrict dst
|
||||
);
|
||||
|
||||
|
||||
/*
|
||||
* Morphisms
|
||||
*/
|
||||
|
||||
int morph_string_as_nullterm_to_length_prefix(
|
||||
char const * restrict src,
|
||||
struct LengthPrefixUInt8Array * restrict dst
|
||||
);
|
||||
int morph_string_as_length_prefix_to_nullterm(
|
||||
struct LengthPrefixUInt8Array const * restrict src,
|
||||
char * restrict dst
|
||||
);
|
|
@ -1,41 +0,0 @@
|
|||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <morphisms/length-prefix.h>
|
||||
|
||||
int morph_digit_as_char_to_uint8(
|
||||
//uint64_t const radix,
|
||||
uint8_t const * restrict src,
|
||||
uint8_t * restrict dst
|
||||
);
|
||||
int morph_digit_as_char_to_uint64(
|
||||
//uint64_t const radix,
|
||||
uint8_t const * restrict src,
|
||||
uint64_t * restrict dst
|
||||
);
|
||||
int morph_digit_as_uint8_to_char(
|
||||
//uint64_t const radix,
|
||||
uint8_t const * restrict src,
|
||||
uint8_t * restrict dst
|
||||
);
|
||||
int morph_digit_as_uint64_to_char(
|
||||
//uint64_t const radix,
|
||||
uint64_t const * restrict src,
|
||||
uint8_t * restrict dst
|
||||
);
|
||||
int morph_posint_endianness(
|
||||
uint64_t const radix,
|
||||
struct LengthPrefixUInt64Array const * restrict src,
|
||||
struct LengthPrefixUInt64Array * restrict dst
|
||||
);
|
||||
int morph_posint_radix_le(
|
||||
uint64_t const src_radix,
|
||||
uint64_t const dst_radix,
|
||||
struct LengthPrefixUInt64Array const * restrict src,
|
||||
struct LengthPrefixUInt64Array * restrict dst
|
||||
);
|
||||
int morph_posint_radix_be(
|
||||
uint64_t const src_radix,
|
||||
uint64_t const dst_radix,
|
||||
struct LengthPrefixUInt64Array const * restrict src,
|
||||
struct LengthPrefixUInt64Array * restrict dst
|
||||
);
|
67
morphisms/morphism-base/digit.morphism-base
Normal file
67
morphisms/morphism-base/digit.morphism-base
Normal file
|
@ -0,0 +1,67 @@
|
|||
morph_digit_as_char_to_uint8 (Radix:ℤ)
|
||||
<Digit Radix> ~ Char ~ Ascii ~ x86.UInt8
|
||||
--> <Digit Radix> ~ x86.UInt8
|
||||
```
|
||||
if( *src >= '0' && *src <= '9' )
|
||||
*dst = *src - '0';
|
||||
else if( *src >= 'a' && *src <= 'f')
|
||||
*dst = 0xa + *src - 'a';
|
||||
else if( *src >= 'A' && *src <= 'F')
|
||||
*dst = 0xa + *src - 'A';
|
||||
else
|
||||
return -1;
|
||||
|
||||
if( *dst < Radix ) {
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
morph_digit_as_char_to_uint64 (Radix:ℤ)
|
||||
<Digit Radix> ~ Char ~ Ascii ~ x86.UInt8
|
||||
--> <Digit Radix> ~ x86.UInt64
|
||||
```
|
||||
if( *src >= '0' && *src <= '9' )
|
||||
*dst = *src - '0';
|
||||
else if( *src >= 'a' && *src <= 'f')
|
||||
*dst = 0xa + *src - 'a';
|
||||
else if( *src >= 'A' && *src <= 'F')
|
||||
*dst = 0xa + *src - 'A';
|
||||
else
|
||||
return -1;
|
||||
|
||||
if( *dst < Radix ) {
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
morph_digit_as_uint8_to_char (Radix:ℤ_16)
|
||||
<Digit Radix> ~ x86.UInt8
|
||||
--> <Digit Radix> ~ Char ~ Ascii ~ x86.UInt8
|
||||
```
|
||||
if ( *src < 10 )
|
||||
*dst = *src + '0';
|
||||
else if( *dst < 16 )
|
||||
*dst = *src - 0xa + 'a';
|
||||
else
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
```
|
||||
|
||||
morph_digit_as_uint64_to_char (Radix:ℤ_16)
|
||||
<Digit Radix> ~ x86.UInt64
|
||||
--> <Digit Radix> ~ Char ~ Ascii ~ x86.UInt8
|
||||
```
|
||||
if ( *src < 10 )
|
||||
*dst = *src + '0';
|
||||
else if( *dst < 16 )
|
||||
*dst = *src - 0xa + 'a';
|
||||
else
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
```
|
|
@ -1,9 +1,24 @@
|
|||
morph_string_as_nullterm_to_length_prefix ()
|
||||
<Seq~<ValueDelim '\0'> x86.UInt8>
|
||||
morph_array_as_valterm_to_lenpfx (Terminator:x86.UInt8)
|
||||
<Seq~<ValueTerminated Terminator> x86.UInt8>
|
||||
--> <Seq~<LengthPrefix x86.UInt64> x86.UInt8>
|
||||
@lib/libmorph_length-prefix.so:src/length_prefix.c
|
||||
```
|
||||
length_prefix_uint8_array_clear(dst);
|
||||
while( *src != Terminator ) {
|
||||
length_prefix_uint8_array_push(dst, *src);
|
||||
src++;
|
||||
}
|
||||
|
||||
morph_string_as_length_prefix_to_nullterm ()
|
||||
return 0;
|
||||
```
|
||||
|
||||
morph_array_as_lenpfx_to_valterm (Terminator:x86.UInt8)
|
||||
<Seq~<LengthPrefix x86.UInt64> x86.UInt8>
|
||||
--> <Seq~<ValueDelim '\0'> x86.UInt8>
|
||||
@lib/libmorph_length-prefix.so:src/length_prefix.c
|
||||
--> <Seq~<ValueTerminated Terminator> x86.UInt8>
|
||||
```
|
||||
for( uint64_t i = 0; i < src->len; ++i ) {
|
||||
*dst ++ = src->items[i];
|
||||
}
|
||||
*dst = Terminator;
|
||||
|
||||
return 0;
|
||||
```
|
||||
|
|
|
@ -1,31 +1,27 @@
|
|||
morph_digit_as_char_to_uint8 (Radix:ℤ_16)
|
||||
<Digit Radix> ~ Char ~ Ascii ~ x86.UInt8
|
||||
--> <Digit Radix> ~ x86.UInt8
|
||||
@lib/libmorph-posint.so:src/posint.c
|
||||
|
||||
morph_digit_as_uint8_to_char (Radix:ℤ_16)
|
||||
<Digit Radix> ~ x86.UInt8
|
||||
--> <Digit Radix> ~ Char ~ Ascii ~ x86.UInt8
|
||||
@lib/libmorph-posint.so:src/posint.c
|
||||
|
||||
morph_digit_as_char_to_uint64 (Radix:ℤ_16)
|
||||
<Digit Radix> ~ Char ~ Ascii ~ x86.UInt8
|
||||
--> <Digit Radix> ~ x86.UInt64
|
||||
@lib/libmorph-posint.so:src/posint.c
|
||||
|
||||
morph_digit_as_uint64_to_char (Radix:ℤ_16)
|
||||
<Digit Radix> ~ x86.UInt64
|
||||
--> <Digit Radix> ~ Char ~ Ascii ~ x86.UInt8
|
||||
@lib/libmorph-posint.so:src/posint.c
|
||||
|
||||
morph_posint_radix_le (SrcRadix:ℤ, DstRadix:ℤ)
|
||||
ℕ
|
||||
~ <PosInt SrcRadix LittleEndian>
|
||||
~ <Seq~<LengthPrefix x86.UInt64> <Digit SrcRadix>~x86.UInt64>
|
||||
--> ℕ
|
||||
~ <PosInt DstRadix LittleEndian>
|
||||
~ <Seq~<LengthPrefix x86.UInt64> <Digit DstRadix>~x86.UInt64>
|
||||
@lib/libmorph-posint.so:src/posint.c
|
||||
~ <Seq~<LenghtPrefix x86.UInt64> <Digit DstRadix>~x86.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_array_clear( dst );
|
||||
|
||||
while( value > 0 ) {
|
||||
length_prefix_uint64_array_push( dst, value % DstRadix );
|
||||
value /= DstRadix;
|
||||
}
|
||||
|
||||
return 0;
|
||||
```
|
||||
|
||||
morph_posint_radix_be (SrcRadix:ℤ, DstRadix:ℤ)
|
||||
ℕ
|
||||
|
@ -34,7 +30,29 @@ morph_posint_radix_be (SrcRadix:ℤ, DstRadix:ℤ)
|
|||
--> ℕ
|
||||
~ <PosInt DstRadix BigEndian>
|
||||
~ <Seq~<LengthPrefix x86.UInt64> <Digit DstRadix>~x86.UInt64>
|
||||
@lib/libmorph-posint.so:src/posint.c
|
||||
```
|
||||
uint64_t value = 0;
|
||||
|
||||
for( uint64_t i = 0; i < src->len; ++i ) {
|
||||
value *= SrcRadix;
|
||||
value += src->items[i];
|
||||
}
|
||||
|
||||
uint64_t v = value;
|
||||
dst->len = 0;
|
||||
while( v ) {
|
||||
dst->len++;
|
||||
v /= DstRadix;
|
||||
}
|
||||
|
||||
uint64_t i = dst->len;
|
||||
while( value > 0 ) {
|
||||
dst->items[--i] = ( dst, value % DstRadix );
|
||||
value /= DstRadix;
|
||||
}
|
||||
|
||||
return 0;
|
||||
```
|
||||
|
||||
morph_posint_endianness (Radix:ℤ)
|
||||
ℕ
|
||||
|
@ -43,7 +61,9 @@ morph_posint_endianness (Radix:ℤ)
|
|||
--> ℕ
|
||||
~ <PosInt Radix BigEndian>
|
||||
~ <Seq~<LengthPrefix x86.UInt64> <Digit Radix> ~ x86.UInt64>
|
||||
@lib/libmorph-posint.so:src/posint.c
|
||||
```
|
||||
return length_prefix_uint64_array_reverse( src, dst );
|
||||
```
|
||||
|
||||
morph_posint_endianness (Radix:ℤ)
|
||||
ℕ
|
||||
|
@ -52,4 +72,6 @@ morph_posint_endianness (Radix:ℤ)
|
|||
--> ℕ
|
||||
~ <PosInt Radix LittleEndian>
|
||||
~ <Seq~<LengthPrefix x86.UInt64> <Digit Radix> ~ x86.UInt64>
|
||||
@lib/libmorph-posint.so:src/posint.c
|
||||
```
|
||||
return length_prefix_uint64_array_reverse( src, dst );
|
||||
```
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <morphisms/length-prefix.h>
|
||||
#include <array/length-prefix.h>
|
||||
|
||||
void length_prefix_uint64_array_clear(
|
||||
struct LengthPrefixUInt64Array * data
|
||||
|
@ -120,28 +120,3 @@ void length_prefix_uint8_array_dump(
|
|||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
int morph_string_as_nullterm_to_length_prefix(
|
||||
char const * restrict src,
|
||||
struct LengthPrefixUInt8Array * restrict dst
|
||||
) {
|
||||
length_prefix_uint8_array_clear(dst);
|
||||
while( *src ) {
|
||||
length_prefix_uint8_array_push(dst, *src);
|
||||
src++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int morph_string_as_length_prefix_to_nullterm(
|
||||
struct LengthPrefixUInt8Array const * restrict src,
|
||||
char * restrict dst
|
||||
) {
|
||||
for( uint64_t i = 0; i < src->len; ++i ) {
|
||||
*dst ++ = src->items[i];
|
||||
}
|
||||
*dst = '\0';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,119 +0,0 @@
|
|||
#include <stdint.h>
|
||||
#include <morphisms/length-prefix.h>
|
||||
|
||||
int morph_digit_as_char_to_uint8(
|
||||
//uint64_t const radix,
|
||||
uint8_t const * restrict src,
|
||||
uint8_t * restrict dst
|
||||
) {
|
||||
if( *src >= '0' && *src <= '9' )
|
||||
*dst = *src - '0';
|
||||
else if( *src >= 'a' && *src <= 'f')
|
||||
*dst = 0xa + *src - 'a';
|
||||
else if( *src >= 'A' && *src <= 'F')
|
||||
*dst = 0xa + *src - 'A';
|
||||
else
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int morph_digit_as_char_to_uint64(
|
||||
//uint64_t const radix,
|
||||
uint8_t const * restrict src,
|
||||
uint64_t * restrict dst
|
||||
) {
|
||||
return morph_digit_as_char_to_uint8(src, (void*)dst);
|
||||
}
|
||||
|
||||
int morph_digit_as_uint8_to_char(
|
||||
//uint64_t const radix,
|
||||
uint8_t const * restrict src,
|
||||
uint8_t * restrict dst
|
||||
) {
|
||||
if ( *src < 10 )
|
||||
*dst = *src + '0';
|
||||
else if( *dst < 16 )
|
||||
*dst = *src - 0xa + 'a';
|
||||
else
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int morph_digit_as_uint64_to_char(
|
||||
//uint64_t const radix,
|
||||
uint64_t const * restrict src,
|
||||
uint8_t * restrict dst
|
||||
) {
|
||||
return morph_digit_as_uint8_to_char((void*)src, dst);
|
||||
}
|
||||
|
||||
/* switches endianness by reversing the digit sequence
|
||||
*/
|
||||
int morph_posint_endianness(
|
||||
uint64_t const radix,
|
||||
|
||||
struct LengthPrefixUInt64Array const * restrict src,
|
||||
struct LengthPrefixUInt64Array * restrict dst
|
||||
) {
|
||||
return length_prefix_uint64_array_reverse( src, dst );
|
||||
}
|
||||
|
||||
/* morph radix in little endian
|
||||
*/
|
||||
int morph_posint_radix_le(
|
||||
uint64_t const src_radix,
|
||||
uint64_t const dst_radix,
|
||||
|
||||
struct LengthPrefixUInt64Array const * restrict src,
|
||||
struct LengthPrefixUInt64Array * restrict dst
|
||||
) {
|
||||
uint64_t value = 0;
|
||||
|
||||
for( uint64_t i = 0; i < src->len; ++i ) {
|
||||
value *= src_radix;
|
||||
value += src->items[src->len - i - 1];
|
||||
}
|
||||
|
||||
length_prefix_uint64_array_clear( dst );
|
||||
|
||||
while( value > 0 ) {
|
||||
length_prefix_uint64_array_push( dst, value % dst_radix );
|
||||
value /= dst_radix;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* morph radix in little endian
|
||||
*/
|
||||
int morph_posint_radix_be(
|
||||
uint64_t const src_radix,
|
||||
uint64_t const dst_radix,
|
||||
|
||||
struct LengthPrefixUInt64Array const * restrict src,
|
||||
struct LengthPrefixUInt64Array * restrict dst
|
||||
) {
|
||||
uint64_t value = 0;
|
||||
|
||||
for( uint64_t i = 0; i < src->len; ++i ) {
|
||||
value *= src_radix;
|
||||
value += src->items[i];
|
||||
}
|
||||
|
||||
uint64_t v = value;
|
||||
dst->len = 0;
|
||||
while( v ) {
|
||||
dst->len++;
|
||||
v /= dst_radix;
|
||||
}
|
||||
|
||||
uint64_t i = dst->len;
|
||||
while( value > 0 ) {
|
||||
dst->items[--i] = ( dst, value % dst_radix );
|
||||
value /= dst_radix;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue