aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine Soulier <asoulier@google.com>2024-03-25 10:01:31 -0700
committerAntoine SOULIER <103120622+asoulier@users.noreply.github.com>2024-03-27 11:01:57 -0700
commit16e082c9293c36a9ac207e4b685b576fb5c2d83a (patch)
tree567aed13f0b532f821a5bd5a1294daf8ad7a01d2
parentbfea2c04c16040038c9abde331d58ea85af50f95 (diff)
downloadliblc3-16e082c9293c36a9ac207e4b685b576fb5c2d83a.tar.gz
wasm: Remove warnings, and separate builtin backend
-rw-r--r--Makefile11
-rw-r--r--src/common.h10
-rw-r--r--src/fastmath.h19
-rw-r--r--wasm/math.h37
-rw-r--r--wasm/string.h28
5 files changed, 74 insertions, 31 deletions
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 <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>
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 <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
/**
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 */