Skip to content

Commit

Permalink
Merge pull request #4544 from kripken/rust-updates
Browse files Browse the repository at this point in the history
Add some llvm funcs for rust #4543
  • Loading branch information
kripken authored Sep 7, 2016
2 parents 0cdefa6 + 63d0e19 commit 24d7943
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,22 @@ LibraryManager.library = {
{{{ makeStructuralReturn(['retl', 'reth']) }}};
},

llvm_ctlz_i8__asm: true,
llvm_ctlz_i8__sig: 'ii',
llvm_ctlz_i8: function(x, isZeroUndef) {
x = x | 0;
isZeroUndef = isZeroUndef | 0;
return (Math_clz32(x) | 0) - 24 | 0;
},

llvm_ctlz_i16__asm: true,
llvm_ctlz_i16__sig: 'ii',
llvm_ctlz_i16: function(x, isZeroUndef) {
x = x | 0;
isZeroUndef = isZeroUndef | 0;
return (Math_clz32(x) | 0) - 16 | 0
},

llvm_ctlz_i64__asm: true,
llvm_ctlz_i64__sig: 'iii',
llvm_ctlz_i64: function(l, h, isZeroUndef) {
Expand Down Expand Up @@ -1462,6 +1478,21 @@ LibraryManager.library = {
llvm_floor_f32: 'Math_floor',
llvm_floor_f64: 'Math_floor',

llvm_exp2_f32: function(x) {
return Math.pow(2, x);
},
llvm_exp2_f64: 'llvm_exp2_f32',

llvm_log2_f32: function(x) {
return Math.log(x) / Math.LN2; // TODO: Math.log2, when browser support is there
},
llvm_log2_f64: 'llvm_log2_f32',

llvm_log10_f32: function(x) {
return Math.log(x) / Math.LN10; // TODO: Math.log10, when browser support is there
},
llvm_log10_f64: 'llvm_log10_f32',

llvm_copysign_f32: function(x, y) {
return y < 0 || (y === 0 && 1/y < 0) ? -Math_abs(x) : Math_abs(x);
},
Expand Down
19 changes: 19 additions & 0 deletions tests/core/test_llvm_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
extern "C" {
extern unsigned short llvm_bswap_i16(unsigned short x);
extern unsigned int llvm_bswap_i32(unsigned int x);
extern int32_t llvm_ctlz_i8(int8_t x, int izZeroUndef);
extern int32_t llvm_ctlz_i16(int16_t x, int izZeroUndef);
extern int32_t llvm_ctlz_i32(int32_t x, int izZeroUndef);
extern int64_t llvm_ctlz_i64(int64_t x, int izZeroUndef);
extern int32_t llvm_cttz_i32(int32_t x, int izZeroUndef);
Expand All @@ -19,6 +21,12 @@ extern float llvm_floor_f32(float x);
extern double llvm_floor_f64(double x);
extern float llvm_sin_f32(float x);
extern double llvm_sin_f64(double x);
extern float llvm_exp2_f32(float x);
extern double llvm_exp2_f64(double x);
extern float llvm_log2_f32(float x);
extern double llvm_log2_f64(double x);
extern float llvm_log10_f32(float x);
extern double llvm_log10_f64(double x);
}

int main(void) {
Expand All @@ -41,6 +49,8 @@ int main(void) {
printf("%d,%d\n", (int)llvm_ctpop_i64((0x3101ULL << 32) | 1),
llvm_ctpop_i32(0x3101));

printf("small ctlz: %d,%d\n", (int)llvm_ctlz_i8(2, 0), llvm_ctlz_i16(2, 0));

printf("llvm_ctpop_i32:\n");
printf("%d\n", (int)llvm_ctpop_i32(-594093059)); // 22
printf("%d\n", (int)llvm_ctpop_i32(0xdeadbeef)); // 24
Expand All @@ -67,5 +77,14 @@ int main(void) {
printf("%.1f\n", llvm_sin_f32(90.0f * 3.14/180));
printf("%.1f\n", llvm_sin_f64(270.0 * 3.14/180));

printf("exp2_f32 %.1f\n", llvm_exp2_f32(3));
printf("exp2_f64 %.1f\n", llvm_exp2_f64(4.5));
printf("log2_f32 %.1f\n", llvm_log2_f32(16));
printf("log2_f64 %.1f\n", llvm_log2_f64(20));
printf("log10_f32 %.1f\n", llvm_log10_f32(1000));
printf("log10_f64 %.1f\n", llvm_log10_f64(2000));

printf("ok.\n");

return 0;
}
8 changes: 8 additions & 0 deletions tests/core/test_llvm_intrinsics.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ c5,de,15,8a
23,21
40,10
5,4
small ctlz: 6,14
llvm_ctpop_i32:
22
24
Expand All @@ -26,3 +27,10 @@ llvm_expect_i32:
-9
1.0
-1.0
exp2_f32 8.0
exp2_f64 22.6
log2_f32 4.0
log2_f64 4.3
log10_f32 3.0
log10_f64 3.3
ok.

0 comments on commit 24d7943

Please sign in to comment.