Skip to content

Commit 24d7943

Browse files
authored
Merge pull request #4544 from kripken/rust-updates
Add some llvm funcs for rust #4543
2 parents 0cdefa6 + 63d0e19 commit 24d7943

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/library.js

+31
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,22 @@ LibraryManager.library = {
946946
{{{ makeStructuralReturn(['retl', 'reth']) }}};
947947
},
948948

949+
llvm_ctlz_i8__asm: true,
950+
llvm_ctlz_i8__sig: 'ii',
951+
llvm_ctlz_i8: function(x, isZeroUndef) {
952+
x = x | 0;
953+
isZeroUndef = isZeroUndef | 0;
954+
return (Math_clz32(x) | 0) - 24 | 0;
955+
},
956+
957+
llvm_ctlz_i16__asm: true,
958+
llvm_ctlz_i16__sig: 'ii',
959+
llvm_ctlz_i16: function(x, isZeroUndef) {
960+
x = x | 0;
961+
isZeroUndef = isZeroUndef | 0;
962+
return (Math_clz32(x) | 0) - 16 | 0
963+
},
964+
949965
llvm_ctlz_i64__asm: true,
950966
llvm_ctlz_i64__sig: 'iii',
951967
llvm_ctlz_i64: function(l, h, isZeroUndef) {
@@ -1462,6 +1478,21 @@ LibraryManager.library = {
14621478
llvm_floor_f32: 'Math_floor',
14631479
llvm_floor_f64: 'Math_floor',
14641480

1481+
llvm_exp2_f32: function(x) {
1482+
return Math.pow(2, x);
1483+
},
1484+
llvm_exp2_f64: 'llvm_exp2_f32',
1485+
1486+
llvm_log2_f32: function(x) {
1487+
return Math.log(x) / Math.LN2; // TODO: Math.log2, when browser support is there
1488+
},
1489+
llvm_log2_f64: 'llvm_log2_f32',
1490+
1491+
llvm_log10_f32: function(x) {
1492+
return Math.log(x) / Math.LN10; // TODO: Math.log10, when browser support is there
1493+
},
1494+
llvm_log10_f64: 'llvm_log10_f32',
1495+
14651496
llvm_copysign_f32: function(x, y) {
14661497
return y < 0 || (y === 0 && 1/y < 0) ? -Math_abs(x) : Math_abs(x);
14671498
},

tests/core/test_llvm_intrinsics.c

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
extern "C" {
55
extern unsigned short llvm_bswap_i16(unsigned short x);
66
extern unsigned int llvm_bswap_i32(unsigned int x);
7+
extern int32_t llvm_ctlz_i8(int8_t x, int izZeroUndef);
8+
extern int32_t llvm_ctlz_i16(int16_t x, int izZeroUndef);
79
extern int32_t llvm_ctlz_i32(int32_t x, int izZeroUndef);
810
extern int64_t llvm_ctlz_i64(int64_t x, int izZeroUndef);
911
extern int32_t llvm_cttz_i32(int32_t x, int izZeroUndef);
@@ -19,6 +21,12 @@ extern float llvm_floor_f32(float x);
1921
extern double llvm_floor_f64(double x);
2022
extern float llvm_sin_f32(float x);
2123
extern double llvm_sin_f64(double x);
24+
extern float llvm_exp2_f32(float x);
25+
extern double llvm_exp2_f64(double x);
26+
extern float llvm_log2_f32(float x);
27+
extern double llvm_log2_f64(double x);
28+
extern float llvm_log10_f32(float x);
29+
extern double llvm_log10_f64(double x);
2230
}
2331

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

52+
printf("small ctlz: %d,%d\n", (int)llvm_ctlz_i8(2, 0), llvm_ctlz_i16(2, 0));
53+
4454
printf("llvm_ctpop_i32:\n");
4555
printf("%d\n", (int)llvm_ctpop_i32(-594093059)); // 22
4656
printf("%d\n", (int)llvm_ctpop_i32(0xdeadbeef)); // 24
@@ -67,5 +77,14 @@ int main(void) {
6777
printf("%.1f\n", llvm_sin_f32(90.0f * 3.14/180));
6878
printf("%.1f\n", llvm_sin_f64(270.0 * 3.14/180));
6979

80+
printf("exp2_f32 %.1f\n", llvm_exp2_f32(3));
81+
printf("exp2_f64 %.1f\n", llvm_exp2_f64(4.5));
82+
printf("log2_f32 %.1f\n", llvm_log2_f32(16));
83+
printf("log2_f64 %.1f\n", llvm_log2_f64(20));
84+
printf("log10_f32 %.1f\n", llvm_log10_f32(1000));
85+
printf("log10_f64 %.1f\n", llvm_log10_f64(2000));
86+
87+
printf("ok.\n");
88+
7089
return 0;
7190
}

tests/core/test_llvm_intrinsics.out

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ c5,de,15,8a
55
23,21
66
40,10
77
5,4
8+
small ctlz: 6,14
89
llvm_ctpop_i32:
910
22
1011
24
@@ -26,3 +27,10 @@ llvm_expect_i32:
2627
-9
2728
1.0
2829
-1.0
30+
exp2_f32 8.0
31+
exp2_f64 22.6
32+
log2_f32 4.0
33+
log2_f64 4.3
34+
log10_f32 3.0
35+
log10_f64 3.3
36+
ok.

0 commit comments

Comments
 (0)