Skip to content

Commit 504b623

Browse files
committed
add emscripten_align typedefs for #2378
1 parent 8abdafa commit 504b623

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

system/include/emscripten/emscripten.h

+26
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ extern "C" {
1818
#include <SDL/SDL.h> /* for SDL_Delay in async_call */
1919
#endif
2020

21+
22+
/* Typedefs */
23+
24+
/*
25+
* Unaligned types, helpful to force LLVM to emit unaligned
26+
* loads/stores in places in your code where SAFE_HEAP found
27+
* an unaligned operation. (It's better to avoid unaligned
28+
* operations, but if you are reading from a packed stream of
29+
* bytes or such, these types may be useful.)
30+
*/
31+
32+
typedef short __attribute__((aligned(1))) emscripten_align1_short;
33+
34+
typedef int __attribute__((aligned(2))) emscripten_align2_int;
35+
typedef int __attribute__((aligned(1))) emscripten_align1_int;
36+
37+
typedef float __attribute__((aligned(2))) emscripten_align2_float;
38+
typedef float __attribute__((aligned(1))) emscripten_align1_float;
39+
40+
typedef double __attribute__((aligned(4))) emscripten_align4_double;
41+
typedef double __attribute__((aligned(2))) emscripten_align2_double;
42+
typedef double __attribute__((aligned(1))) emscripten_align1_double;
43+
44+
45+
/* Functions */
46+
2147
/*
2248
* Convenient syntax for inline assembly/js. Allows stuff like
2349
*

tests/core/test_set_align.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
#include <stdio.h>
3+
#include <emscripten.h>
4+
5+
volatile char data[16];
6+
7+
__attribute__((noinline)) void *get_aligned(int align)
8+
{
9+
char *ptr = (char*)(((int)(data + 7)) & ~7); // Make 8-byte aligned
10+
ptr += align; // Now 'align' aligned
11+
return (void*)ptr;
12+
}
13+
14+
int main()
15+
{
16+
emscripten_align4_double *d4 = (emscripten_align4_double*)get_aligned(4);
17+
*d4 = 17.0;
18+
printf("addr: %d, value: %f\n", ((int)d4) % 8, *d4);
19+
20+
emscripten_align2_double *d2 = (emscripten_align2_double*)get_aligned(2);
21+
*d2 = 18.0;
22+
printf("addr: %d, value: %f\n", ((int)d2) % 8, *d2);
23+
24+
emscripten_align1_double *d1 = (emscripten_align1_double*)get_aligned(1);
25+
*d1 = 19.0;
26+
printf("addr: %d, value: %f\n", ((int)d1) % 8, *d1);
27+
28+
emscripten_align2_float *f2 = (emscripten_align2_float*)get_aligned(2);
29+
*f2 = 20.0;
30+
printf("addr: %d, value: %f\n", ((int)f2) % 4, *f2);
31+
32+
emscripten_align1_float *f1 = (emscripten_align1_float*)get_aligned(1);
33+
*f1 = 21.0;
34+
printf("addr: %d, value: %f\n", ((int)f1) % 4, *f1);
35+
36+
emscripten_align2_int *i2 = (emscripten_align2_int*)get_aligned(2);
37+
*i2 = 22;
38+
printf("addr: %d, value: %d\n", ((int)i2) % 4, *i2);
39+
40+
emscripten_align1_int *i1 = (emscripten_align1_int*)get_aligned(1);
41+
*i1 = 23;
42+
printf("addr: %d, value: %d\n", ((int)i1) % 4, *i1);
43+
44+
emscripten_align1_short *s1 = (emscripten_align1_short*)get_aligned(1);
45+
*s1 = 24;
46+
printf("addr: %d, value: %d\n", ((int)s1) % 4, (int)*s1);
47+
48+
return 0;
49+
}
50+

tests/core/test_set_align.out

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
addr: 4, value: 17.000000
2+
addr: 2, value: 18.000000
3+
addr: 1, value: 19.000000
4+
addr: 2, value: 20.000000
5+
addr: 1, value: 21.000000
6+
addr: 2, value: 22
7+
addr: 1, value: 23
8+
addr: 1, value: 24

tests/test_core.py

+9
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,15 @@ def test_llvm_used(self):
19801980

19811981
self.do_run_from_file(src, output)
19821982

1983+
def test_set_align(self):
1984+
if self.run_name == 'slow2asm': return self.skip('FIXME in slow2asm')
1985+
1986+
Settings.SAFE_HEAP = 1
1987+
1988+
test_path = path_from_root('tests', 'core', 'test_set_align')
1989+
src, output = (test_path + s for s in ('.c', '.out'))
1990+
self.do_run_from_file(src, output)
1991+
19831992
def test_emscripten_api(self):
19841993
#if Settings.MICRO_OPTS or Settings.RELOOP or Building.LLVM_OPTS: return self.skip('FIXME')
19851994

0 commit comments

Comments
 (0)