A portable compilation target for the web
Allows running languages other than JavaScript within browser environments
C/C++ → IR → wasm → x86/ARM
(module
(type $0 (func (param i32 i32) (result i32)))
(export "add" $add)
(func $add (type $0) (param $var$0 i32) (param $var$1 i32) (result i32)
(i32.add
(get_local $var$0)
(get_local $var$1)
)
)
)
00 61 73 6d 01 00 00 00 04 74 79 70 65 87 80 80 |.asm.....type...|
80 00 01 40 02 01 01 01 01 08 66 75 6e 63 74 69 |...@......functi|
6f 6e 82 80 80 80 00 01 00 06 6d 65 6d 6f 72 79 |on........memory|
85 80 80 80 00 80 02 80 02 01 06 65 78 70 6f 72 |...........expor|
74 86 80 80 80 00 01 00 03 61 64 64 04 63 6f 64 |t........add.cod|
65 8c 80 80 80 00 01 86 80 80 80 00 00 14 00 14 |e...............|
01 40 04 6e 61 6d 65 86 80 80 80 00 01 03 61 64 |.@.name.......ad|
64 00 |d.|
// File: dasum.c
#include <math.h>
double c_dasum( const int N, const double *X, const int stride ) {
double sum;
int m;
int n;
int i;
sum = 0.0;
if ( N <= 0 || stride <= 0 ) {
return sum;
}
// If the stride is equal to `1`, use unrolled loops...
if ( stride == 1 ) {
m = N % 6;
// If we have a remainder, run a clean-up loop...
if ( m > 0 ) {
for ( i = 0; i < m; i++ ) {
sum += fabs( X[i] );
}
}
if ( N < 6 ) {
return sum;
}
for ( i = m; i < N; i += 6 ) {
sum += fabs( X[i] ) + fabs( X[i+1] ) + fabs( X[i+2] ) + fabs( X[i+3] ) + fabs( X[i+4] ) + fabs( X[i+5] );
}
return sum;
}
n = N * stride;
for ( i = 0; i < n; i += stride ) {
sum += fabs( X[i] );
}
return sum;
}
Length | JavaScript | wasm | Native | Perf |
---|---|---|---|---|
10 | 22,438,020 | 18,226,375 | 7,084,870 | 2.57x |
100 | 4,350,384 | 6,428,586 | 6,428,626 | 1.0x |
1,000 | 481,417 | 997,234 | 3,289,090 | 0.30x |
10,000 | 28,186 | 110,540 | 355,172 | 0.31x |
100,000 | 1,617 | 11,157 | 30,058 | 0.37x |
1,000,000 | 153 | 979 | 1,850 | 0.53x |
https://github.com/stdlib-js/stdlib
https://www.patreon.com/athan
$$y = a_nx_n + a_{n-1}x_{n-1} + \cdots + a_0x_0 + b$$
$$t_{n+1} = t_{n}$$
$$t_{n+1} = \frac{1}{7} \sum_{i=n-6}^{n} t_i$$
$$t_{n+1} = \frac{1}{W} \sum_{i=n-W+1}^{n} t_i$$