Skip to content

Commit 72f33a3

Browse files
minadararslan
authored andcommitted
wasm32 support (#192)
1 parent f24b1bf commit 72f33a3

13 files changed

Lines changed: 126 additions & 8 deletions

File tree

Make.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ endif
3131

3232
AR = $(TOOLPREFIX)ar
3333

34+
ifeq ($(ARCH),wasm32)
35+
CC = clang-8
36+
USEGCC = 0
37+
CFLAGS_add += -fno-builtin -fno-strict-aliasing
38+
endif
39+
3440
ifeq ($(USECLANG),1)
3541
USEGCC = 0
3642
CC = clang
@@ -118,6 +124,10 @@ SFLAGS_arch += -m64
118124
LDFLAGS_arch += -m64
119125
endif
120126

127+
ifeq ($(ARCH),wasm32)
128+
CFLAGS_arch += -ffreestanding -nostdlib -nostdinc --target=wasm32-unknown-unknown
129+
endif
130+
121131
# Add our "arch"-related FLAGS in. We separate arch-related flags out so that
122132
# we can conveniently get at them for targets that don't want the rest of
123133
# *FLAGS_add, such as the testing Makefile targets

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ endif
4949
.PHONY: all check test clean distclean \
5050
install install-static install-shared install-pkgconfig install-headers
5151

52-
all: libopenlibm.a libopenlibm.$(OLM_MAJOR_MINOR_SHLIB_EXT)
52+
53+
OLM_LIBS := libopenlibm.a
54+
ifneq ($(ARCH), wasm32)
55+
OLM_LIBS += libopenlibm.$(OLM_MAJOR_MINOR_SHLIB_EXT)
56+
endif
57+
58+
all : $(OLM_LIBS)
5359

5460
check test: test/test-double test/test-float
5561
test/test-double

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ also supports arm, aarch64, ppc64le, and mips.
2727
Linux and Windows.
2828
3. Use `make USECLANG=1` to build with clang. This is the default on OS X, FreeBSD,
2929
and OpenBSD.
30-
4. Architectures are auto-detected. Use `make ARCH=i386` to force a
30+
4. Use `make ARCH=wasm32` to build the wasm32 library with clang. Requires clang-8.
31+
5. Architectures are auto-detected. Use `make ARCH=i386` to force a
3132
build for i386. Other supported architectures are i486, i586, and
3233
i686. GCC 4.8 is the minimum requirement for correct codegen on
3334
older 32-bit architectures.

include/openlibm_math.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define __WIN32__
2626
#endif
2727

28-
#ifndef __arm__
28+
#if !defined(__arm__) && !defined(__wasm__)
2929
#define OLM_LONG_DOUBLE
3030
#endif
3131

src/Make.files

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ $(CUR_SRCS) = common.c \
1515
s_copysign.c s_copysignf.c s_cos.c s_cosf.c \
1616
s_csqrt.c s_csqrtf.c s_erf.c s_erff.c \
1717
s_exp2.c s_exp2f.c s_expm1.c s_expm1f.c s_fabs.c s_fabsf.c s_fdim.c \
18-
s_floor.c s_floorf.c s_fma.c s_fmaf.c \
18+
s_floor.c s_floorf.c \
1919
s_fmax.c s_fmaxf.c s_fmin.c \
2020
s_fminf.c s_fpclassify.c \
2121
s_frexp.c s_frexpf.c s_ilogb.c s_ilogbf.c \
2222
s_isinf.c s_isfinite.c s_isnormal.c s_isnan.c \
23-
s_llrint.c s_llrintf.c s_llround.c s_llroundf.c \
24-
s_log1p.c s_log1pf.c s_logb.c s_logbf.c s_lrint.c s_lrintf.c \
25-
s_lround.c s_lroundf.c s_modf.c s_modff.c \
26-
s_nearbyint.c s_nextafter.c s_nextafterf.c \
23+
s_log1p.c s_log1pf.c s_logb.c s_logbf.c \
24+
s_modf.c s_modff.c \
25+
s_nextafter.c s_nextafterf.c \
2726
s_nexttowardf.c s_remquo.c s_remquof.c \
2827
s_rint.c s_rintf.c s_round.c s_roundf.c \
2928
s_scalbln.c s_scalbn.c s_scalbnf.c s_signbit.c \
@@ -32,10 +31,18 @@ $(CUR_SRCS) = common.c \
3231
s_trunc.c s_truncf.c s_cpow.c s_cpowf.c \
3332
w_cabs.c w_cabsf.c
3433

34+
ifneq ($(ARCH), wasm32)
35+
36+
$(CUR_SRCS) += \
37+
s_fma.c s_fmaf.c s_lrint.c s_lrintf.c s_lround.c s_lroundf.c \
38+
s_llrint.c s_llrintf.c s_llround.c s_llroundf.c s_nearbyint.c
39+
3540
ifneq ($(OS), WINNT)
3641
$(CUR_SRCS) += s_nan.c
3742
endif
3843

44+
endif
45+
3946
# Add in long double functions for x86, x64 and aarch64
4047
ifneq ($(filter $(ARCH),i387 amd64 aarch64),)
4148
# C99 long double functions

src/cdefs-compat.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#endif /* __APPLE__ */
2222
#endif /* __strong_reference */
2323

24+
#ifdef __wasm__
25+
# define __weak_reference(sym,alias) __strong_reference(sym,alias)
26+
#endif
27+
2428
#ifndef __weak_reference
2529
#ifdef __ELF__
2630
#ifdef __STDC__

src/s_fdim.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ fn(type x, type y) \
4343

4444
DECL(double, fdim)
4545
DECL(float, fdimf)
46+
#ifdef OLM_LONG_DOUBLE
4647
DECL(long double, fdiml)
48+
#endif

src/s_scalbln.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ scalblnf (float x, long n)
6262
return (scalbnf(x, in));
6363
}
6464

65+
#ifdef OLM_LONG_DOUBLE
6566
OLM_DLLEXPORT long double
6667
scalblnl (long double x, long n)
6768
{
@@ -76,3 +77,4 @@ scalblnl (long double x, long n)
7677
}
7778
return (scalbnl(x, (int)n));
7879
}
80+
#endif

wasm32/Make.files

Whitespace-only changes.

wasm32/assert.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define assert(x) ((void)0)

0 commit comments

Comments
 (0)