Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm Compilation cleanup #51

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 $@)
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
10 changes: 1 addition & 9 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,9 @@
#include <lc3.h>
#include "fastmath.h"

#include <stdalign.h>
#include <limits.h>

#ifdef __wasm32__
#define memmove __builtin_memmove
#define memset __builtin_memset
#define memcpy __builtin_memcpy
#define NULL ((void*)0)
#else
#include <stdalign.h>
#include <string.h>
#endif

#ifdef __ARM_ARCH
#include <arm_acle.h>
Expand Down
19 changes: 0 additions & 19 deletions src/fastmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,7 @@
#define __LC3_FASTMATH_H

#include <stdint.h>

#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 <math.h>
#endif


/**
Expand Down
37 changes: 37 additions & 0 deletions wasm/math.h
Original file line number Diff line number Diff line change
@@ -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 */
28 changes: 28 additions & 0 deletions wasm/string.h
Original file line number Diff line number Diff line change
@@ -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 */