From 6d5efce31a9e1e3cf806660c9bfe67fd4d64a4c4 Mon Sep 17 00:00:00 2001 From: Antoine Soulier Date: Mon, 25 Mar 2024 10:01:31 -0700 Subject: [PATCH 1/2] wasm: Remove warnings, and separate builtin backend --- Makefile | 11 ++++++++--- src/common.h | 10 +--------- src/fastmath.h | 19 ------------------- wasm/math.h | 37 +++++++++++++++++++++++++++++++++++++ wasm/string.h | 28 ++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 wasm/math.h create mode 100644 wasm/string.h diff --git a/Makefile b/Makefile index 1bd805a..50100bc 100644 --- a/Makefile +++ b/Makefile @@ -35,11 +35,13 @@ CFLAGS += -std=c11 -Wall -Wextra -Wdouble-promotion -Wvla -pedantic TARGET = $(lastword $(shell $(CC) -v 2>&1 | grep "Target: ")) +LIB_SHARED := true LIB_SUFFIX := so ifeq ($(TARGET),wasm32) + LIB_SHARED := false LIB_SUFFIX := wasm - CFLAGS += -mbulk-memory + CFLAGS += -Iwasm -mbulk-memory LDFLAGS += -nostdlib -Wl,--no-entry -Wl,--export-dynamic endif @@ -148,8 +150,11 @@ $(BUILD_DIR)/%.o: %.cc $(MAKEFILE_DEPS) $(addprefix -I,$(INCLUDE)) \ $(addprefix -D,$(DEFINE)) -MMD -MF $(@:.o=.d) -o $@ -$(LIB): CFLAGS += -fvisibility=hidden -flto -fPIC -$(LIB): LDFLAGS += -flto -shared +ifeq ($(LIB_SHARED),true) + $(LIB): CFLAGS += -fvisibility=hidden -flto -fPIC + $(LIB): LDFLAGS += -flto -shared +endif + $(LIB): $(MAKEFILE_DEPS) @echo " LD $(notdir $@)" $(V)mkdir -p $(dir $@) diff --git a/src/common.h b/src/common.h index 3d907b9..2b23a8d 100644 --- a/src/common.h +++ b/src/common.h @@ -22,17 +22,9 @@ #include #include "fastmath.h" -#include #include - -#ifdef __wasm32__ -#define memmove __builtin_memmove -#define memset __builtin_memset -#define memcpy __builtin_memcpy -#define NULL ((void*)0) -#else +#include #include -#endif #ifdef __ARM_ARCH #include diff --git a/src/fastmath.h b/src/fastmath.h index ac45e61..221d69f 100644 --- a/src/fastmath.h +++ b/src/fastmath.h @@ -20,26 +20,7 @@ #define __LC3_FASTMATH_H #include - -#ifdef __wasm32__ -#define log10f __builtin_log10f -#define sqrtf __builtin_sqrtf -#define fabsf __builtin_fabsf -#define floorf __builtin_floorf -#define fminf __builtin_fminf -#define fmaxf __builtin_fmaxf -#define truncf __builtin_truncf -// This is not exactly roundf, as this return the even value for two equally near -// values. e.g -// - roundf(0.5) = 1, nearbyint(0.5) = 0, -// - roundf(1.5) = 2, nearbyint(1.5) = 2, -// - roundf(2.5) = 3, nearbyint(2.5) = 2 -// but this builtin maps to https://webassembly.github.io/spec/core/exec/numerics.html#op-fnearest -#define roundf __builtin_nearbyint -#define INFINITY __builtin_inff() -#else #include -#endif /** diff --git a/wasm/math.h b/wasm/math.h new file mode 100644 index 0000000..f004338 --- /dev/null +++ b/wasm/math.h @@ -0,0 +1,37 @@ +/****************************************************************************** + * + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_MATH_H +#define __LC3_MATH_H + +#define INFINITY __builtin_inff() + +#define floorf __builtin_floorf +#define truncf __builtin_truncf + +static inline float roundf(float x) +{ return x >= 0 ? truncf(x + 0.5f) : truncf(x - 0.5f); } + +#define fabsf __builtin_fabsf +#define fmaxf __builtin_fmaxf +#define fminf __builtin_fminf + +#define log10f __builtin_log10f +#define sqrtf __builtin_sqrtf + +#endif /* __LC3_MATH_H */ diff --git a/wasm/string.h b/wasm/string.h new file mode 100644 index 0000000..bab773c --- /dev/null +++ b/wasm/string.h @@ -0,0 +1,28 @@ +/****************************************************************************** + * + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +#ifndef __LC3_STRING_H +#define __LC3_STRING_H + +#define NULL ( (void *)0 ) + +#define memcpy __builtin_memcpy +#define memmove __builtin_memmove +#define memset __builtin_memset + +#endif /* __LC3_STRING_H */ From 0d45f4ecf6d5d0e9c0dd392d84f371c4e01aed60 Mon Sep 17 00:00:00 2001 From: Antoine Soulier Date: Wed, 27 Mar 2024 10:57:48 -0700 Subject: [PATCH 2/2] README: Add wasm compilation --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index f99f4c0..6cb2a7e 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,20 @@ $ make -j CC=path_to_android_ndk_prebuilt/toolchain-prefix-clang LIBC=bionic Compiled library will be found in `bin` directory. +#### Web Assembly (WASM) + +Web assembly compilation is supported using LLVM WebAssembly backend. +Installation of LLVM compiler and linker is needed: + +```sh +# apt install clang lld +``` + +The webasm object is compiled using: +```sh +$ make CC="clang --target=wasm32" +``` + ## Tools Tools can be all compiled, while invoking `make` as follows :