Skip to content

Commit

Permalink
Merge pull request #485 from ut-issl/feature/simple-libc
Browse files Browse the repository at this point in the history
Pre Release (v3.8.0-beta.3): Add simple bsearch, memchr implementation
  • Loading branch information
sksat authored Jan 25, 2023
2 parents dcb0515 + 7e30d75 commit d78c1ea
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@ cmake_minimum_required(VERSION 3.13)

project(C2A_CORE_LIB)

option(C2A_USE_SIMPLE_LIBC "use C2A-core hosted simple libc implementation" OFF)

if(C2A_USE_SIMPLE_LIBC)
message("use simple libc!!!")
set(C2A_LIBC_SRC
libc/memchr.c
libc/bsearch.c
)
else()
set(C2A_LIBC_SRC "")
endif()

set(C2A_SRCS
ascii_conv.c
c2a_round.c
crc.c
endian.c
majority_vote_for3.c
${C2A_LIBC_SRC}
)

if(BUILD_C2A_AS_CXX)
Expand Down
51 changes: 51 additions & 0 deletions Library/libc/bsearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @file
* @brief C2A が依存する libc 関数 bsearch を自前実装し,c2a-core から提供することで,C2A の移植性を高める.
* これにより,ベアメタル環境でも C2A を libc 無しに(newlib などを持ち出してくることなく)ビルド・動作させることができる.
* @note https://github.com/ut-issl/c2a-core/pull/485
* @note https://linuxjm.osdn.jp/html/LDP_man-pages/man3/bsearch.3.html
*/
#include <stdlib.h>

// compare func(key, base[i])
// key < b: compr_func(key, b) < 0
// key = b: compr_func(key, b) = 0
// key > b: compr_func(key, b) > 0
typedef int (*compr_func)(const void*, const void*);

void *bsearch(const void* key, const void* base, size_t nmemb, size_t size, compr_func compr)
{
size_t min = 0;
size_t max = nmemb;

if (nmemb == 0 || size == 0)
{
return NULL;
}

while (min < max)
{
size_t index = (min + max) / 2;
void* current = (void*) ((char*)base + (size * index));

int result = compr(key, current);
if (result == 0)
{
// found
return current;
}
else if (result < 0)
{
// key < current
max = index;
}
else // result > 0
{
// current < key
min = index + 1;
}
}

// not found
return NULL;
}
24 changes: 24 additions & 0 deletions Library/libc/memchr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @file
* @brief C2A が依存する libc 関数 memchr を自前実装し,c2a-core から提供することで,C2A の移植性を高める.
* これにより,ベアメタル環境でも C2A を libc 無しに(newlib などを持ち出してくることなく)ビルド・動作させることができる.
* @note https://github.com/ut-issl/c2a-core/pull/485
* @note https://linuxjm.osdn.jp/html/LDP_man-pages/man3/memchr.3.html
*/
#include <string.h>

void* memchr(const void* buf, int c, size_t n)
{
const unsigned char* s = (const unsigned char*) buf;

while (n--)
{
if (*s == (unsigned char)c)
{
return (void*)s;
}
s++;
}

return NULL;
}
2 changes: 1 addition & 1 deletion c2a_core_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ void C2A_core_main(void);
#define C2A_CORE_VER_MAJOR (3)
#define C2A_CORE_VER_MINOR (8)
#define C2A_CORE_VER_PATCH (0)
#define C2A_CORE_VER_PRE ("beta.2")
#define C2A_CORE_VER_PRE ("beta.3")

#endif

0 comments on commit d78c1ea

Please sign in to comment.