Skip to content

Commit fecf842

Browse files
committed
implement round/roundf ourselves, which is more optimized than musl and also lets it work without PRECISE_F32 #3876
1 parent 73d9f57 commit fecf842

6 files changed

+33
-6
lines changed

emscripten-version.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
"1.35.7"
1+
"1.35.8"
22

src/library.js

+14
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,20 @@ LibraryManager.library = {
14801480
llvm_exp_f32: 'Math_exp',
14811481
llvm_exp_f64: 'Math_exp',
14821482

1483+
round__asm: true,
1484+
round__sig: 'dd',
1485+
round: function(d) {
1486+
d = +d;
1487+
return d >= +0 ? +Math_floor(d + +0.5) : +Math_ceil(d - +0.5);
1488+
},
1489+
1490+
roundf__asm: true,
1491+
roundf__sig: 'dd',
1492+
roundf: function(f) {
1493+
f = +f;
1494+
return f >= +0 ? +Math_floor(f + +0.5) : +Math_ceil(f - +0.5); // TODO: use fround?
1495+
},
1496+
14831497
_reallyNegative: function(x) {
14841498
return x < 0 || (x === 0 && (1/x) === -Infinity);
14851499
},

tests/core/test_rounding.in

+10
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,15 @@ int main() {
3434
fractpart = modf (param , &intpart);
3535
printf ("%f = %f + %f \n", param, intpart, fractpart);
3636

37+
printf("%.1f ", roundf(1.4));
38+
printf("%.1f ", roundf(1.6));
39+
printf("%.1f ", roundf(-1.4));
40+
printf("%.1f ", roundf(-1.6));
41+
42+
printf("%.1f ", roundf(1.5));
43+
printf("%.1f ", roundf(2.5));
44+
printf("%.1f ", roundf(-1.5));
45+
printf("%.1f ", roundf(-2.5));
46+
3747
return 0;
3848
}

tests/core/test_rounding.out

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
1.0 2.0 -1.0 -2.0 2.0 3.0 -2.0 -3.0 1 2 -1 -2 2 2 -2 -2
22
3.141593 = 3.000000 + 0.141593
33
-3.141593 = -3.000000 + -0.141593
4+
1.0 2.0 -1.0 -2.0 2.0 3.0 -2.0 -3.0

tests/test_core.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -837,12 +837,14 @@ def test_frexp(self):
837837
self.do_run_from_file(src, output)
838838

839839
def test_rounding(self):
840-
Settings.PRECISE_F32 = 1 # in the move to llvm 3.7, froundf in musl became more sensitive to float/double differences
840+
for precise_f32 in [0, 1]:
841+
print precise_f32
842+
Settings.PRECISE_F32 = precise_f32
841843

842-
test_path = path_from_root('tests', 'core', 'test_rounding')
843-
src, output = (test_path + s for s in ('.in', '.out'))
844+
test_path = path_from_root('tests', 'core', 'test_rounding')
845+
src, output = (test_path + s for s in ('.in', '.out'))
844846

845-
self.do_run_from_file(src, output)
847+
self.do_run_from_file(src, output)
846848

847849
def test_fcvt(self):
848850
test_path = path_from_root('tests', 'core', 'test_fcvt')

tools/system_libs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def create_libc(libname):
9595
blacklist = set(
9696
['ipc', 'passwd', 'thread', 'signal', 'sched', 'ipc', 'time', 'linux', 'aio', 'exit', 'legacy', 'mq', 'process', 'search', 'setjmp', 'env', 'ldso', 'conf'] + # musl modules
9797
['memcpy.c', 'memset.c', 'memmove.c', 'getaddrinfo.c', 'getnameinfo.c', 'inet_addr.c', 'res_query.c', 'gai_strerror.c', 'proto.c', 'gethostbyaddr.c', 'gethostbyaddr_r.c', 'gethostbyname.c', 'gethostbyname2_r.c', 'gethostbyname_r.c', 'gethostbyname2.c', 'usleep.c', 'alarm.c', 'syscall.c'] + # individual files
98-
['abs.c', 'cos.c', 'cosf.c', 'cosl.c', 'sin.c', 'sinf.c', 'sinl.c', 'tan.c', 'tanf.c', 'tanl.c', 'acos.c', 'acosf.c', 'acosl.c', 'asin.c', 'asinf.c', 'asinl.c', 'atan.c', 'atanf.c', 'atanl.c', 'atan2.c', 'atan2f.c', 'atan2l.c', 'exp.c', 'expf.c', 'expl.c', 'log.c', 'logf.c', 'logl.c', 'sqrt.c', 'sqrtf.c', 'sqrtl.c', 'fabs.c', 'fabsf.c', 'fabsl.c', 'ceil.c', 'ceilf.c', 'ceill.c', 'floor.c', 'floorf.c', 'floorl.c', 'pow.c', 'powf.c', 'powl.c'] # individual math files
98+
['abs.c', 'cos.c', 'cosf.c', 'cosl.c', 'sin.c', 'sinf.c', 'sinl.c', 'tan.c', 'tanf.c', 'tanl.c', 'acos.c', 'acosf.c', 'acosl.c', 'asin.c', 'asinf.c', 'asinl.c', 'atan.c', 'atanf.c', 'atanl.c', 'atan2.c', 'atan2f.c', 'atan2l.c', 'exp.c', 'expf.c', 'expl.c', 'log.c', 'logf.c', 'logl.c', 'sqrt.c', 'sqrtf.c', 'sqrtl.c', 'fabs.c', 'fabsf.c', 'fabsl.c', 'ceil.c', 'ceilf.c', 'ceill.c', 'floor.c', 'floorf.c', 'floorl.c', 'pow.c', 'powf.c', 'powl.c', 'round.c', 'roundf.c'] # individual math files
9999
)
100100
# TODO: consider using more math code from musl, doing so makes box2d faster
101101
for dirpath, dirnames, filenames in os.walk(musl_srcdir):

0 commit comments

Comments
 (0)