Compare commits

...

3 commits

Author SHA1 Message Date
c341e973b1 Add VLQ 2025-12-05 20:52:39 +03:00
2364bc6809 Add VLQ 2025-12-05 20:52:24 +03:00
7f0064db02 Add MIN/MAX constants 2025-12-05 18:07:44 +03:00
6 changed files with 135 additions and 0 deletions

1
axl.h
View file

@ -5,5 +5,6 @@
#include "axl_memory.h"
#include "axl_string.h"
#include "axl_io.h"
#include "axl_vlq.h"
#endif

View file

@ -15,4 +15,21 @@ typedef unsigned long long u64;
#define false 0
#define NULL ((void*)0)
// Minimum and maximum values for integer types
#define I8_MIN (-128)
#define I8_MAX 127
#define U8_MAX 255
#define I16_MIN (-32768)
#define I16_MAX 32767
#define U16_MAX 65535
#define I32_MIN (-2147483647 - 1)
#define I32_MAX 2147483647
#define U32_MAX 4294967295U
#define I64_MIN (-9223372036854775807LL - 1LL)
#define I64_MAX 9223372036854775807LL
#define U64_MAX 18446744073709551615ULL
#endif // !AXL_TYPES_H

57
axl_vlq.c Normal file
View file

@ -0,0 +1,57 @@
#include "axl_vlq.h"
u32 axl_vlq_encode(u32 num, u8* out)
{
u8 buf[5];
int pos = 0;
if(num == 0)
{
out[0] = 0;
return 1;
}
while(num > 0)
{
buf[pos] = num & 0x7F;
num >>= 7;
pos++;
}
for(int j = pos - 1; j > 0; j--)
{
buf[j] |= 0x80;
}
for(int j = pos - 1; j >= 0; j--)
{
*out++ = buf[j];
}
return pos;
}
u32 axl_vlq_decode(const u8* in, u32* num)
{
*num = 0;
u32 pos = 0;
while(pos < AXL_VLQ_MAX_LEN)
{
u8 b = in[pos++];
*num = (*num << 7) | (b & 0x7F);
if(!(b & 0x80))
{
return pos;
}
if(*num > (U32_MAX >> 7))
{
return 0;
}
}
return 0;
}

11
axl_vlq.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef AXL_VLQ_H
#define AXL_VLQ_H
#include "axl_types.h"
#define AXL_VLQ_MAX_LEN 5
u32 axl_vlq_encode(u32 num, u8* out);
u32 axl_vlq_decode(const u8* in, u32* num);
#endif // !AXL_VLQ_H

30
axl_vlq_test.c Normal file
View file

@ -0,0 +1,30 @@
#include "axl_koan.h"
#include "axl_memory.h"
#include "axl_types.h"
#include "axl_vlq.h"
KOAN(vlq_basic_encode_decode)
{
u32 expected[] = {0, 126, 129, 256, 65535, 70000, 9999999, U32_MAX};
u32 expected_len[] = {1, 1, 2, 2, 3, 3, 4, 5 };
u8 encoded[AXL_VLQ_MAX_LEN];
for(u32 i = 0; i < sizeof(expected) / sizeof(expected[0]); i++)
{
u32 encoded_len = axl_vlq_encode(expected[i], encoded);
u32 decoded = 0;
u32 decoded_len = axl_vlq_decode(encoded, &decoded);
ASSERT_UINT_EQ(expected_len[i], encoded_len);
ASSERT_UINT_EQ(expected_len[i], decoded_len);
ASSERT_UINT_EQ(expected[i], decoded);
}
}
int main(void)
{
axl_init();
return koan_run_all();
}

19
main.c
View file

@ -1,4 +1,5 @@
#include "axl.h"
#include "axl_vlq.h"
#include <windows.h>
int _start(void)
@ -29,5 +30,23 @@ int _start(void)
(void)fds;
(void)f;
u32 new_len = 0;
u8 src[] = "Bonjour le monde!";
u32 src_len = axl_strlen((i8*)src);
for(u32 i = 0; i < src_len; i += 4)
{
u8 buff[AXL_VLQ_MAX_LEN + 1];
u32 encoded_l = axl_vlq_encode(*((u32*)(src + i)), buff);
new_len += encoded_l;
buff[encoded_l] = '\0';
axl_puts((i8*)buff);
}
(void)new_len;
return 0;
}