Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RealNeGate committed Jan 3, 2023
2 parents 40d3f85 + d53791d commit fec3b98
Show file tree
Hide file tree
Showing 31 changed files with 1,772 additions and 1,363 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: CI
on: [pull_request, workflow_dispatch, push]

jobs:
build_windows:
runs-on: windows-2019
steps:
- uses: actions/checkout@v1
- uses: seanmiddleditch/gha-setup-ninja@master

- name: Download Submodules
run: git submodule update --init --recursive

- name: Build TB
shell: cmd
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat
py build.py x64 aarch64 wasm
timeout-minutes: 10
build_linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: seanmiddleditch/gha-setup-ninja@master
- name: Download LLVM
run: sudo apt-get install llvm-11 clang-11

- name: Download Submodules
run: git submodule update --init --recursive

- name: Build TB
run: python3 build.py x64 aarch64 wasm
timeout-minutes: 10
57 changes: 57 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Deploy
on: [workflow_dispatch]

jobs:
build_windows:
runs-on: windows-2019
steps:
- uses: actions/checkout@v1
- uses: seanmiddleditch/gha-setup-ninja@master

- name: Download Submodules
run: git submodule update --init --recursive

- name: Build TB
shell: cmd
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat
py build.py x64 aarch64 wasm
timeout-minutes: 10

- name: Copy artifacts
run: |
mkdir dist
cp include dist
cp -r tildebackend.lib dist
- name: Upload artifact
uses: actions/upload-artifact@v1
with:
name: windows_artifacts
path: dist
build_linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: seanmiddleditch/gha-setup-ninja@master
- name: Download LLVM
run: sudo apt-get install llvm-11 clang-11

- name: Download Submodules
run: git submodule update --init --recursive

- name: Build TB
run: python3 build.py x64 aarch64 wasm
timeout-minutes: 10

- name: Copy artifacts
run: |
mkdir dist
cp include dist
cp -r tildebackend.a dist
- name: Upload artifact
uses: actions/upload-artifact@v1
with:
name: ubuntu_artifacts
path: dist
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CC = clang
CFLAGS = -Wall -Werror -Wno-unused-function -I include -I deps/luajit/src
LDLIBS =

SOURCES := src/tb/tb.c
OBJECTS := $(addprefix bin/, $(notdir $(SOURCES:%.c=%.o)))
INCLUDES := include

# Convert into library
all: tildebackend.lib

tildebackend.lib: $(OBJECTS)
lib /nologo /out:$out $(inputs)

# C source code
%.o: %.c
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $(@)
35 changes: 0 additions & 35 deletions Tupfile

This file was deleted.

2 changes: 1 addition & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@
ninja.close()

# run ninja
subprocess.call(['ninja'])
exit(subprocess.call(['ninja']))
6 changes: 5 additions & 1 deletion include/tb.h
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,11 @@ extern "C" {
////////////////////////////////
// JIT compilation
////////////////////////////////
TB_API void tb_module_export_jit(TB_Module* m);
typedef struct TB_JITContext TB_JITContext;

// passing 0 to jit_heap_capacity will default to 4MiB
TB_API TB_JITContext* tb_module_begin_jit(TB_Module* m, size_t jit_heap_capacity);
TB_API void tb_module_end_jit(TB_JITContext* jit);

#define TB_FOR_FUNCTIONS(it, module) for (TB_Function* it = tb_first_function(module); it != NULL; it = tb_next_function(it))
TB_API TB_Function* tb_first_function(TB_Module* m);
Expand Down
63 changes: 42 additions & 21 deletions src/tb/bigint/BigInt.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string.h>
#include <assert.h>
#include <limits.h>

#include "BigInt.h"
#define MaxBigIntWords 2
Expand Down Expand Up @@ -73,7 +74,7 @@ int BigInt_to_int(size_t NumWords, BigInt_t * BigInt)
{
int ret = 0;

/* Endianness issue if machine is not little-endian? */
/* Endianness issue if machine is not little-endian? */
#if (BigIntWordSize == 1)
ret += BigInt[0];
ret += BigInt[1] << 8;
Expand All @@ -100,7 +101,7 @@ size_t BigInt_truncate(size_t NumWords, BigInt_t * BigInt)

void BigInt_from_string(size_t NumWords, BigInt_t * BigInt, char * str)
{
assert(NumWords <= MaxBigIntWords);
assert(NumWords <= MaxBigIntWords);
BigInt_zero(NumWords, BigInt);

BigInt_t temp[MaxBigIntWords];
Expand Down Expand Up @@ -211,12 +212,12 @@ void BigInt_inc(size_t NumWords, BigInt_t * BigInt)

int BigInt_bextr(size_t NumWords, BigInt_t * BigInt, int Bit)
{
size_t ElemIndex, ElemBit;
size_t ElemIndex, ElemBit;

ElemIndex = Bit / BigIntWordSize;
ElemBit = Bit % BigIntWordSize;
ElemIndex = Bit / (BigIntWordSize * CHAR_BIT);
ElemBit = Bit % (BigIntWordSize * CHAR_BIT);

return BigInt[ElemIndex] & (1u << ElemBit);
return BigInt[ElemIndex] & (1u << ElemBit);
}

void BigInt_add(size_t AWords, BigInt_t * A, size_t BWords, BigInt_t * B, size_t Out_NumWords, BigInt_t * Out)
Expand Down Expand Up @@ -357,7 +358,7 @@ void BigInt_sub(size_t AWords, BigInt_t * A, size_t BWords, BigInt_t * B, size_t

void BigInt_mul_basic(size_t NumWords, BigInt_t * A, BigInt_t * B, BigInt_t * Out)
{
assert(NumWords <= MaxBigIntWords);
assert(NumWords <= MaxBigIntWords);

BigInt_t row[MaxBigIntWords];
BigInt_t tmp[MaxBigIntWords];
Expand Down Expand Up @@ -423,19 +424,19 @@ static void BigInt_Karatsuba_internal(size_t num1_NumWords, BigInt_t * num1, siz
// z1 = karatsuba((low1 + high1), (low2 + high2))
// z2 = karatsuba(high1, high2)
size_t z0_NumWords = low1_NumWords + low2_NumWords;
assert(z0_NumWords <= MaxBigIntWords);
assert(z0_NumWords <= MaxBigIntWords);
BigInt_t z0[MaxBigIntWords];

size_t z1_NumWords = (MAX(low1_NumWords, high1_NumWords)+1) + (MAX(low2_NumWords, high2_NumWords)+1);
assert(z1_NumWords <= MaxBigIntWords);
assert(z1_NumWords <= MaxBigIntWords);
BigInt_t z1[MaxBigIntWords];

size_t z2_NumWords = high1_NumWords + high2_NumWords;
int use_out_as_z2 = (Out_NumWords >= z2_NumWords); /* Sometimes we can use Out to store z2, then we don't have to copy from z2 to out later (2X SPEEDUP!) */
if (use_out_as_z2) {BigInt_zero(Out_NumWords-(z2_NumWords),Out+z2_NumWords);}/* The remaining part of Out must be ZERO'D */

assert(z2_NumWords <= MaxBigIntWords);
BigInt_t tmp[MaxBigIntWords];
assert(z2_NumWords <= MaxBigIntWords);
BigInt_t tmp[MaxBigIntWords];
BigInt_t * z2 = (use_out_as_z2) ? Out : tmp;

/* Make z0 and z2 */
Expand All @@ -446,7 +447,7 @@ static void BigInt_Karatsuba_internal(size_t num1_NumWords, BigInt_t * num1, siz
{
size_t low1high1_NumWords = MAX(low1_NumWords, high1_NumWords)+1;
size_t low2high2_NumWords = MAX(low2_NumWords, high2_NumWords)+1;
assert(low1high1_NumWords <= MaxBigIntWords && low2high2_NumWords <= MaxBigIntWords);
assert(low1high1_NumWords <= MaxBigIntWords && low2high2_NumWords <= MaxBigIntWords);
BigInt_t low1high1[MaxBigIntWords];
BigInt_t low2high2[MaxBigIntWords];
BigInt_add(low1_NumWords, low1, high1_NumWords, high1, low1high1_NumWords, low1high1);
Expand All @@ -471,10 +472,10 @@ void BigInt_mul(size_t ANumWords, BigInt_t * A, size_t BNumWords, BigInt_t * B,

void BigInt_div(size_t NumWords, BigInt_t * A, BigInt_t * B, BigInt_t * Out)
{
assert(NumWords <= MaxBigIntWords);
BigInt_t current[NumWords];
BigInt_t denom[NumWords];
BigInt_t tmp[NumWords];
assert(NumWords <= MaxBigIntWords);
BigInt_t current[NumWords];
BigInt_t denom[NumWords];
BigInt_t tmp[NumWords];

BigInt_from_int(NumWords, current, 1); // int current = 1;
BigInt_copy(NumWords, denom, B); // denom = B
Expand Down Expand Up @@ -550,14 +551,14 @@ void BigInt_rshift(size_t NumWords, BigInt_t * B, int nbits)
void BigInt_mod(size_t NumWords, BigInt_t * A, BigInt_t * B, BigInt_t * Out)
{
/* Take divmod and throw away div part */
assert(NumWords <= MaxBigIntWords);
assert(NumWords <= MaxBigIntWords);
BigInt_t tmp[MaxBigIntWords];
BigInt_divmod(NumWords, A, B, tmp, Out);
}

void BigInt_divmod(size_t NumWords, BigInt_t * A, BigInt_t * B, BigInt_t * C, BigInt_t * D)
{
assert(NumWords <= MaxBigIntWords);
assert(NumWords <= MaxBigIntWords);
BigInt_t tmp[MaxBigIntWords];

/* Out = (A / B) */
Expand Down Expand Up @@ -591,6 +592,13 @@ void BigInt_xor(size_t NumWords, BigInt_t * A, BigInt_t * B, BigInt_t * Out)
}
}

void BigInt_not(size_t NumWords, BigInt_t * A)
{
for (size_t i = 0; i < NumWords; ++i) {
A[i] = ~A[i];
}
}

int BigInt_cmp(size_t NumWords, BigInt_t * A, BigInt_t * B)
{
size_t i = NumWords;
Expand All @@ -606,6 +614,19 @@ int BigInt_cmp(size_t NumWords, BigInt_t * A, BigInt_t * B)
return EQUAL;
}

int BigInt_is_small_num(size_t NumWords, BigInt_t * BigInt, BigInt_t Comparand)
{
if (NumWords == 0 || BigInt[0] != Comparand) return 0;

for (size_t i = 1; i < NumWords; ++i) {
if (BigInt[i]) {
return 0;
}
}

return 1;
}

int BigInt_is_zero(size_t NumWords, BigInt_t * BigInt)
{
for (size_t i = 0; i < NumWords; ++i) {
Expand All @@ -625,9 +646,9 @@ void BigInt_pow(size_t NumWords, BigInt_t * A, BigInt_t * B, BigInt_t * Out)
/* Return 1 when exponent is 0 -- BigInt^0 = 1 */
BigInt_inc(NumWords, Out);
} else {
assert(NumWords <= MaxBigIntWords);
assert(NumWords <= MaxBigIntWords);
BigInt_t bcopy[MaxBigIntWords];
BigInt_t tmp[MaxBigIntWords];
BigInt_t tmp[MaxBigIntWords];
BigInt_copy(NumWords, bcopy, B);

/* Copy A -> tmp */
Expand All @@ -652,7 +673,7 @@ void BigInt_pow(size_t NumWords, BigInt_t * A, BigInt_t * B, BigInt_t * Out)

void BigInt_isqrt(size_t NumWords, BigInt_t * A, BigInt_t * B)
{
assert(NumWords <= MaxBigIntWords);
assert(NumWords <= MaxBigIntWords);
BigInt_t low[MaxBigIntWords];
BigInt_t high[MaxBigIntWords];
BigInt_t mid[MaxBigIntWords];
Expand Down
2 changes: 2 additions & 0 deletions src/tb/bigint/BigInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ void BigInt_divmod(size_t NumWords, BigInt_t * A, BigInt_t * B, BigInt_t * C, Bi
void BigInt_and(size_t NumWords, BigInt_t * A, BigInt_t * B, BigInt_t * Out); /* Out = A & B */
void BigInt_or(size_t NumWords, BigInt_t * A, BigInt_t * B, BigInt_t * Out); /* Out = A | B */
void BigInt_xor(size_t NumWords, BigInt_t * A, BigInt_t * B, BigInt_t * Out); /* Out = A ^ B */
void BigInt_not(size_t NumWords, BigInt_t * A); /* Out = ~A */
void BigInt_lshift(size_t NumWords, BigInt_t * B, int nbits); /* B = A << nbits */
void BigInt_rshift(size_t NumWords, BigInt_t * B, int nbits); /* B = A >> nbits */

/* Special operators and comparison */
int BigInt_cmp(size_t NumWords, BigInt_t * A, BigInt_t * B); /* Compare: returns LARGER, EQUAL or SMALLER */
int BigInt_is_zero(size_t NumWords, BigInt_t * BigInt); /* For comparison with zero */
int BigInt_is_small_num(size_t NumWords, BigInt_t * BigInt, BigInt_t Comparand);
void BigInt_inc(size_t NumWords, BigInt_t * BigInt); /* Increment: add one to BigInt */
void BigInt_dec(size_t NumWords, BigInt_t * BigInt); /* Decrement: subtract one from BigInt */
int BigInt_bextr(size_t NumWords, BigInt_t * BigInt, int Bit); /* Read bit: grabs a bit from any point in the BigInt */
Expand Down
Loading

0 comments on commit fec3b98

Please sign in to comment.