Skip to content

Commit

Permalink
implement round/roundf ourselves, which is more optimized than musl a…
Browse files Browse the repository at this point in the history
…nd also lets it work without PRECISE_F32 #3876
  • Loading branch information
kripken committed Nov 10, 2015
1 parent 73d9f57 commit fecf842
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion emscripten-version.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"1.35.7"
"1.35.8"

14 changes: 14 additions & 0 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,20 @@ LibraryManager.library = {
llvm_exp_f32: 'Math_exp',
llvm_exp_f64: 'Math_exp',

round__asm: true,
round__sig: 'dd',
round: function(d) {
d = +d;
return d >= +0 ? +Math_floor(d + +0.5) : +Math_ceil(d - +0.5);
},

roundf__asm: true,
roundf__sig: 'dd',
roundf: function(f) {
f = +f;
return f >= +0 ? +Math_floor(f + +0.5) : +Math_ceil(f - +0.5); // TODO: use fround?
},

_reallyNegative: function(x) {
return x < 0 || (x === 0 && (1/x) === -Infinity);
},
Expand Down
10 changes: 10 additions & 0 deletions tests/core/test_rounding.in
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,15 @@ int main() {
fractpart = modf (param , &intpart);
printf ("%f = %f + %f \n", param, intpart, fractpart);

printf("%.1f ", roundf(1.4));
printf("%.1f ", roundf(1.6));
printf("%.1f ", roundf(-1.4));
printf("%.1f ", roundf(-1.6));

printf("%.1f ", roundf(1.5));
printf("%.1f ", roundf(2.5));
printf("%.1f ", roundf(-1.5));
printf("%.1f ", roundf(-2.5));

return 0;
}
1 change: 1 addition & 0 deletions tests/core/test_rounding.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
1.0 2.0 -1.0 -2.0 2.0 3.0 -2.0 -3.0 1 2 -1 -2 2 2 -2 -2
3.141593 = 3.000000 + 0.141593
-3.141593 = -3.000000 + -0.141593
1.0 2.0 -1.0 -2.0 2.0 3.0 -2.0 -3.0
10 changes: 6 additions & 4 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,12 +837,14 @@ def test_frexp(self):
self.do_run_from_file(src, output)

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

test_path = path_from_root('tests', 'core', 'test_rounding')
src, output = (test_path + s for s in ('.in', '.out'))
test_path = path_from_root('tests', 'core', 'test_rounding')
src, output = (test_path + s for s in ('.in', '.out'))

self.do_run_from_file(src, output)
self.do_run_from_file(src, output)

def test_fcvt(self):
test_path = path_from_root('tests', 'core', 'test_fcvt')
Expand Down
2 changes: 1 addition & 1 deletion tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def create_libc(libname):
blacklist = set(
['ipc', 'passwd', 'thread', 'signal', 'sched', 'ipc', 'time', 'linux', 'aio', 'exit', 'legacy', 'mq', 'process', 'search', 'setjmp', 'env', 'ldso', 'conf'] + # musl modules
['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
['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
['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
)
# TODO: consider using more math code from musl, doing so makes box2d faster
for dirpath, dirnames, filenames in os.walk(musl_srcdir):
Expand Down

0 comments on commit fecf842

Please sign in to comment.