[stdlib] Implement kotlin.math for wasm

This commit is contained in:
Ilya Matveev
2018-02-28 15:01:45 +03:00
committed by ilmat192
parent ffae98f7e0
commit 164d35c73f
8 changed files with 2304 additions and 98 deletions
@@ -1,3 +1,19 @@
/*
* Copyright 2010-2018 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.native.interop.gen.wasm.idl
// There are no WebIDL descriptions of Math,
@@ -32,14 +48,11 @@ val idlMath = listOf(
Operation("expm1", idlDouble, true, Arg("x", idlDouble)),
Operation("floor", idlDouble, true, Arg("x", idlDouble)),
Operation("fround", idlDouble, true, Arg("x", idlDouble)),
//Operation("hypot([x[, y[, …]]]),
//Operation("imul(x, y),
Operation("log", idlDouble, true, Arg("x", idlDouble)),
Operation("log1p", idlDouble, true, Arg("x", idlDouble)),
Operation("log10", idlDouble, true, Arg("x", idlDouble)),
Operation("log2", idlDouble, true, Arg("x", idlDouble)),
//Operation("max([x[, y[, …]]]), // TODO: Support varargs.
//Operation("min([x[, y[, …]]]),
Operation("pow", idlDouble, true, Arg("x", idlDouble), Arg("y", idlDouble)),
Operation("random", idlDouble, true),
Operation("round", idlDouble, true, Arg("x", idlDouble)),
@@ -49,7 +62,12 @@ val idlMath = listOf(
Operation("sqrt", idlDouble, true, Arg("x", idlDouble)),
Operation("tan", idlDouble, true, Arg("x", idlDouble)),
Operation("tanh", idlDouble, true, Arg("x", idlDouble)),
//Operation("toSource(),
Operation("trunc", idlDouble, true, Arg("x", idlDouble))
Operation("trunc", idlDouble, true, Arg("x", idlDouble)),
// Actually these functions have vararg parameter.
// But their kotlin analogs have only 2 parameters so we don't need to support varargs here.
Operation("hypot", idlDouble, true, Arg("x", idlDouble), Arg("y", idlDouble)),
Operation("max", idlDouble, true, Arg("x", idlDouble), Arg("y", idlDouble)),
Operation("min", idlDouble, true, Arg("x", idlDouble), Arg("y", idlDouble))
)
)
+17
View File
@@ -1650,6 +1650,23 @@ task extend_exception(type: RunKonanTest) {
source = "runtime/exceptions/extend0.kt"
}
// A special test variant for wasm without exception testing.
// TODO: Remove when exceptions become available for wasm.
task runtime_math_wasm(type: RunKonanTest) {
disabled = (project.testTarget != 'wasm32')
source = "runtime/math/wasm.kt"
}
task runtime_math(type: RunKonanTest) {
disabled = (project.testTarget == 'wasm32')
source = "stdlib_external/numbers/MathTest.kt"
}
task runtime_math_harmony(type: RunKonanTest) {
disabled = (project.testTarget == 'wasm32')
source = "stdlib_external/numbers/HarmonyMathTests.kt"
}
task catch3(type: RunKonanTest) {
goldValue = "Before\nCaught Throwable\nDone\n"
source = "codegen/try/catch3.kt"
File diff suppressed because it is too large Load Diff
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
* Copyright 2010-2018 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -324,7 +324,6 @@ class DoubleMathTest {
}
}
@Test fun nextAndPrev() {
for (value in listOf(0.0, -0.0, Double.MIN_VALUE, -1.0, 2.0.pow(10))) {
val next = value.nextUp()
@@ -368,6 +367,63 @@ class DoubleMathTest {
assertEquals(maxUlp, (-Double.MAX_VALUE).ulp)
}
}
@Test fun IEEEremainder() {
val data = arrayOf( // a a IEEErem 2.5
doubleArrayOf(-2.0, 0.5),
doubleArrayOf(-1.25, -1.25),
doubleArrayOf( 0.0, 0.0),
doubleArrayOf( 1.0, 1.0),
doubleArrayOf( 1.25, 1.25),
doubleArrayOf( 1.5, -1.0),
doubleArrayOf( 2.0, -0.5),
doubleArrayOf( 2.5, 0.0),
doubleArrayOf( 3.5, 1.0),
doubleArrayOf( 3.75, -1.25),
doubleArrayOf( 4.0, -1.0)
)
for ((a, r) in data) {
assertEquals(r, a.IEEErem(2.5), "($a).IEEErem(2.5)")
}
assertTrue(Double.NaN.IEEErem(2.5).isNaN())
assertTrue(2.0.IEEErem(Double.NaN).isNaN())
assertTrue(Double.POSITIVE_INFINITY.IEEErem(2.0).isNaN())
assertTrue(2.0.IEEErem(0.0).isNaN())
assertEquals(PI, PI.IEEErem(Double.NEGATIVE_INFINITY))
}
/*
* Special cases:
* - `atan2(0.0, 0.0)` is `0.0`
* - `atan2(0.0, x)` is `0.0` for `x > 0` and `PI` for `x < 0`
* - `atan2(-0.0, x)` is `-0.0` for 'x > 0` and `-PI` for `x < 0`
* - `atan2(y, +Inf)` is `0.0` for `0 < y < +Inf` and `-0.0` for '-Inf < y < 0`
* - `atan2(y, -Inf)` is `PI` for `0 < y < +Inf` and `-PI` for `-Inf < y < 0`
* - `atan2(y, 0.0)` is `PI/2` for `y > 0` and `-PI/2` for `y < 0`
* - `atan2(+Inf, x)` is `PI/2` for finite `x`y
* - `atan2(-Inf, x)` is `-PI/2` for finite `x`
* - `atan2(NaN, x)` and `atan2(y, NaN)` is `NaN`
*/
@Test fun atan2SpecialCases() {
assertEquals(atan2(0.0, 0.0), 0.0)
assertEquals(atan2(0.0, 1.0), 0.0)
assertEquals(atan2(0.0, -1.0), PI)
assertEquals(atan2(-0.0, 1.0), -0.0)
assertEquals(atan2(-0.0, -1.0), -PI)
assertEquals(atan2(1.0, Double.POSITIVE_INFINITY), 0.0)
assertEquals(atan2(-1.0, Double.POSITIVE_INFINITY), -0.0)
assertEquals(atan2(1.0, Double.NEGATIVE_INFINITY), PI)
assertEquals(atan2(-1.0, Double.NEGATIVE_INFINITY), -PI)
assertEquals(atan2(1.0, 0.0), PI/2)
assertEquals(atan2(-1.0, 0.0), -PI/2)
assertEquals(atan2(Double.POSITIVE_INFINITY, 1.0), PI/2)
assertEquals(atan2(Double.NEGATIVE_INFINITY, 1.0), -PI/2)
assertTrue(atan2(Double.NaN, 1.0).isNaN())
assertTrue(atan2(1.0, Double.NaN).isNaN())
}
}
class FloatMathTest {
@@ -704,63 +760,7 @@ class FloatMathTest {
}
}
}
class IntegerMathTest {
@Test fun intSigns() {
val negatives = listOf(Int.MIN_VALUE, -65536, -1)
val positives = listOf(1, 100, 256, Int.MAX_VALUE)
negatives.forEach { assertEquals(-1, it.sign) }
positives.forEach { assertEquals(1, it.sign) }
assertEquals(0, 0.sign)
(negatives - Int.MIN_VALUE).forEach { assertEquals(-it, it.absoluteValue) }
assertEquals(Int.MIN_VALUE, Int.MIN_VALUE.absoluteValue)
positives.forEach { assertEquals(it, it.absoluteValue) }
}
@Test fun longSigns() {
val negatives = listOf(Long.MIN_VALUE, -65536L, -1L)
val positives = listOf(1L, 100L, 256L, Long.MAX_VALUE)
negatives.forEach { assertEquals(-1, it.sign) }
positives.forEach { assertEquals(1, it.sign) }
assertEquals(0, 0L.sign)
(negatives - Long.MIN_VALUE).forEach { assertEquals(-it, it.absoluteValue) }
assertEquals(Long.MIN_VALUE, Long.MIN_VALUE.absoluteValue)
positives.forEach { assertEquals(it, it.absoluteValue) }
}
@Test fun IEEEremainder() {
val data = arrayOf( // a a IEEErem 2.5
doubleArrayOf(-2.0, 0.5),
doubleArrayOf(-1.25, -1.25),
doubleArrayOf( 0.0, 0.0),
doubleArrayOf( 1.0, 1.0),
doubleArrayOf( 1.25, 1.25),
doubleArrayOf( 1.5, -1.0),
doubleArrayOf( 2.0, -0.5),
doubleArrayOf( 2.5, 0.0),
doubleArrayOf( 3.5, 1.0),
doubleArrayOf( 3.75, -1.25),
doubleArrayOf( 4.0, -1.0)
)
for ((a, r) in data) {
assertEquals(r, a.IEEErem(2.5), "($a).IEEErem(2.5)")
}
assertTrue(Double.NaN.IEEErem(2.5).isNaN())
assertTrue(2.0.IEEErem(Double.NaN).isNaN())
assertTrue(Double.POSITIVE_INFINITY.IEEErem(2.0).isNaN())
assertTrue(2.0.IEEErem(0.0).isNaN())
assertEquals(PI, PI.IEEErem(Double.NEGATIVE_INFINITY))
}
@Test fun IEEEremainderFloat() {
val data = arrayOf( // a a IEEErem 2.5
floatArrayOf(-2.0f, 0.5f),
floatArrayOf(-1.25f, -1.25f),
@@ -785,36 +785,36 @@ class IntegerMathTest {
assertEquals(PI.toFloat(), PI.toFloat().IEEErem(Float.NEGATIVE_INFINITY))
}
/*
* Special cases:
* - `atan2(0.0, 0.0)` is `0.0`
* - `atan2(0.0, x)` is `0.0` for `x > 0` and `PI` for `x < 0`
* - `atan2(-0.0, x)` is `-0.0` for 'x > 0` and `-PI` for `x < 0`
* - `atan2(y, +Inf)` is `0.0` for `0 < y < +Inf` and `-0.0` for '-Inf < y < 0`
* - `atan2(y, -Inf)` is `PI` for `0 < y < +Inf` and `-PI` for `-Inf < y < 0`
* - `atan2(y, 0.0)` is `PI/2` for `y > 0` and `-PI/2` for `y < 0`
* - `atan2(+Inf, x)` is `PI/2` for finite `x`y
* - `atan2(-Inf, x)` is `-PI/2` for finite `x`
* - `atan2(NaN, x)` and `atan2(y, NaN)` is `NaN`
*/
@Test fun atan2SpecialCases() {
}
assertEquals(atan2(0.0, 0.0), 0.0)
assertEquals(atan2(0.0, 1.0), 0.0)
assertEquals(atan2(0.0, -1.0), PI)
assertEquals(atan2(-0.0, 1.0), -0.0)
assertEquals(atan2(-0.0, -1.0), -PI)
assertEquals(atan2(1.0, Double.POSITIVE_INFINITY), 0.0)
assertEquals(atan2(-1.0, Double.POSITIVE_INFINITY), -0.0)
assertEquals(atan2(1.0, Double.NEGATIVE_INFINITY), PI)
assertEquals(atan2(-1.0, Double.NEGATIVE_INFINITY), -PI)
assertEquals(atan2(1.0, 0.0), PI/2)
assertEquals(atan2(-1.0, 0.0), -PI/2)
assertEquals(atan2(Double.POSITIVE_INFINITY, 1.0), PI/2)
assertEquals(atan2(Double.NEGATIVE_INFINITY, 1.0), -PI/2)
class IntegerMathTest {
assertTrue(atan2(Double.NaN, 1.0).isNaN())
assertTrue(atan2(1.0, Double.NaN).isNaN())
@Test
fun intSigns() {
val negatives = listOf(Int.MIN_VALUE, -65536, -1)
val positives = listOf(1, 100, 256, Int.MAX_VALUE)
negatives.forEach { assertEquals(-1, it.sign) }
positives.forEach { assertEquals(1, it.sign) }
assertEquals(0, 0.sign)
(negatives - Int.MIN_VALUE).forEach { assertEquals(-it, it.absoluteValue) }
assertEquals(Int.MIN_VALUE, Int.MIN_VALUE.absoluteValue)
positives.forEach { assertEquals(it, it.absoluteValue) }
}
}
@Test
fun longSigns() {
val negatives = listOf(Long.MIN_VALUE, -65536L, -1L)
val positives = listOf(1L, 100L, 256L, Long.MAX_VALUE)
negatives.forEach { assertEquals(-1, it.sign) }
positives.forEach { assertEquals(1, it.sign) }
assertEquals(0, 0L.sign)
(negatives - Long.MIN_VALUE).forEach { assertEquals(-it, it.absoluteValue) }
assertEquals(Long.MIN_VALUE, Long.MIN_VALUE.absoluteValue)
positives.forEach { assertEquals(it, it.absoluteValue) }
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
* Copyright 2010-2018 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,10 @@
#include <math.h>
#include <stdlib.h>
#include "DoubleConversions.h"
#include "Exceptions.h"
#include "KotlinMath.h"
#include "ReturnSlot.h"
#include "Types.h"
#if (__MINGW32__ || __MINGW64__)
@@ -27,7 +30,8 @@
#define KONAN_NEED_ASINH_ACOSH 0
#endif
#ifdef KONAN_NEED_ASINH_ACOSH
#if KONAN_NEED_ASINH_ACOSH
namespace {
// MinGW's implmenetation if asinh/acosh function returns NaN for large arguments so we use another implementation.
@@ -92,7 +96,9 @@ namespace {
extern "C" {
#ifndef KONAN_NO_MATH // There is a platform math library.
#ifndef KONAN_NO_MATH // We have a platform math library. Call its math functions.
#ifndef KONAN_WASM // Use libm.
// region Double math.
@@ -237,9 +243,215 @@ KLong Kotlin_math_absl(KLong x) { return llabs(x); }
// endregion
#else // if there is no platform math library.
#else // KONAN_WASM defined. Use JS math implementation.
#define RETURN_RESULT_OF_JS_CALL(call, doubleArg) { \
call(doubleUpper(doubleArg), doubleLower(doubleArg), Konan_js_getCurrentArena()); \
return ReturnSlot_getDouble(); \
}
#define RETURN_RESULT_OF_JS_CALL2(call, doubleArg1, doubleArg2) { \
call(doubleUpper(doubleArg1), \
doubleLower(doubleArg1), \
doubleUpper(doubleArg2), \
doubleLower(doubleArg2), \
Konan_js_getCurrentArena()); \
return ReturnSlot_getDouble(); \
}
KDouble Kotlin_math_sin(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_sin, x); }
KDouble Kotlin_math_cos(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_cos, x); }
KDouble Kotlin_math_tan(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_tan, x); }
KDouble Kotlin_math_asin(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_asin, x); }
KDouble Kotlin_math_acos(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_acos, x); }
KDouble Kotlin_math_atan(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_atan, x); }
KDouble Kotlin_math_sinh(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_sinh, x); }
KDouble Kotlin_math_cosh(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_cosh, x); }
KDouble Kotlin_math_tanh(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_tanh, x); }
KDouble Kotlin_math_asinh(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_asinh, x); }
KDouble Kotlin_math_acosh(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_acosh, x); }
KDouble Kotlin_math_atanh(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_atanh, x); }
KDouble Kotlin_math_sqrt(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_sqrt, x); }
KDouble Kotlin_math_exp(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_exp, x); }
KDouble Kotlin_math_expm1(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_expm1, x); }
KDouble Kotlin_math_ln(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_log, x); }
KDouble Kotlin_math_log10(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_log10, x); }
KDouble Kotlin_math_log2(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_log2, x); }
KDouble Kotlin_math_ln1p(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_log1p, x); }
KDouble Kotlin_math_ceil(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_ceil, x); }
KDouble Kotlin_math_floor(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_floor, x); }
KDouble Kotlin_math_round(KDouble x) {
if (fmod(x, 0.5) != 0.0) {
RETURN_RESULT_OF_JS_CALL(knjs__Math_round, x);
}
KDouble f = floor(x);
return (fmod(f, 2) == 0.0) ? f : ceil(x);
}
KDouble Kotlin_math_abs(KDouble x) { RETURN_RESULT_OF_JS_CALL(knjs__Math_abs, x); }
KDouble Kotlin_math_atan2(KDouble y, KDouble x) { RETURN_RESULT_OF_JS_CALL2(knjs__Math_atan2, y, x); }
KDouble Kotlin_math_hypot(KDouble x, KDouble y) { RETURN_RESULT_OF_JS_CALL2(knjs__Math_hypot, x, y); }
// extensions
KDouble Kotlin_math_Double_pow(KDouble thiz, KDouble x) { RETURN_RESULT_OF_JS_CALL2(knjs__Math_pow, thiz, x); }
KDouble Kotlin_math_Double_IEEErem(KDouble thiz, KDouble divisor) {
if (isnan(thiz) || isnan(divisor) || isinf(thiz) || divisor == 0.0) {
return NAN;
}
if (!isinf(thiz) && isinf(divisor)) {
return thiz;
}
KDouble rounded = Kotlin_math_round(thiz / divisor);
return thiz - rounded * divisor;
}
KDouble Kotlin_math_Double_nextUp(KDouble thiz) {
if (isnan(thiz) || thiz == HUGE_VAL) {
return thiz;
}
if (thiz == 0.0) {
return DBL_TRUE_MIN;
}
return bitsToDouble(doubleToBits(thiz) + (thiz > 0 ? 1 : -1));
}
KDouble Kotlin_math_Double_nextDown(KDouble thiz) {
if (isnan(thiz) || thiz == -HUGE_VAL) {
return thiz;
}
if (thiz == 0.0) {
return -DBL_TRUE_MIN;
}
return bitsToDouble(doubleToBits(thiz) - (thiz > 0 ? 1 : -1));
}
KDouble Kotlin_math_Double_nextTowards(KDouble thiz, KDouble to) {
if (isnan(thiz) || isnan(to)) {
return NAN;
}
if (to > thiz) {
return Kotlin_math_Double_nextUp(thiz);
}
if (to < thiz) {
return Kotlin_math_Double_nextDown(thiz);
}
// thiz == to
return to;
}
KBoolean Kotlin_math_Double_signBit(KDouble thiz) {
return (doubleToBits(thiz) & (KLong) 1 << 63) != 0;
}
KDouble Kotlin_math_Double_withSign(KDouble thiz, KDouble sign) {
bool oldSign = Kotlin_math_Double_signBit(thiz);
bool newSign = Kotlin_math_Double_signBit(sign);
return (oldSign == newSign) ? thiz : -thiz;
}
// endregion
// region Float math.
KFloat Kotlin_math_sinf(KFloat x) { return (KFloat)Kotlin_math_sin (x); }
KFloat Kotlin_math_cosf(KFloat x) { return (KFloat)Kotlin_math_cos (x); }
KFloat Kotlin_math_tanf(KFloat x) { return (KFloat)Kotlin_math_tan (x); }
KFloat Kotlin_math_asinf(KFloat x) { return (KFloat)Kotlin_math_asin (x); }
KFloat Kotlin_math_acosf(KFloat x) { return (KFloat)Kotlin_math_acos (x); }
KFloat Kotlin_math_atanf(KFloat x) { return (KFloat)Kotlin_math_atan (x); }
KFloat Kotlin_math_sinhf(KFloat x) { return (KFloat)Kotlin_math_sinh (x); }
KFloat Kotlin_math_coshf(KFloat x) { return (KFloat)Kotlin_math_cosh (x); }
KFloat Kotlin_math_tanhf(KFloat x) { return (KFloat)Kotlin_math_tanh (x); }
KFloat Kotlin_math_asinhf(KFloat x) { return (KFloat)Kotlin_math_asinh (x); }
KFloat Kotlin_math_acoshf(KFloat x) { return (KFloat)Kotlin_math_acosh (x); }
KFloat Kotlin_math_atanhf(KFloat x) { return (KFloat)Kotlin_math_atanh (x); }
KFloat Kotlin_math_sqrtf(KFloat x) { return (KFloat)Kotlin_math_sqrt (x); }
KFloat Kotlin_math_expf(KFloat x) { return (KFloat)Kotlin_math_exp (x); }
KFloat Kotlin_math_expm1f(KFloat x) { return (KFloat)Kotlin_math_expm1 (x); }
KFloat Kotlin_math_lnf(KFloat x) { return (KFloat)Kotlin_math_ln (x); }
KFloat Kotlin_math_log10f(KFloat x) { return (KFloat)Kotlin_math_log10 (x); }
KFloat Kotlin_math_log2f(KFloat x) { return (KFloat)Kotlin_math_log2 (x); }
KFloat Kotlin_math_ln1pf(KFloat x) { return (KFloat)Kotlin_math_ln1p (x); }
KFloat Kotlin_math_ceilf(KFloat x) { return (KFloat)Kotlin_math_ceil (x); }
KFloat Kotlin_math_floorf(KFloat x) { return (KFloat)Kotlin_math_floor (x); }
KFloat Kotlin_math_roundf(KFloat x) { return (KFloat)Kotlin_math_round (x); }
KFloat Kotlin_math_absf(KFloat x) { return (KFloat)Kotlin_math_abs (x); }
KFloat Kotlin_math_atan2f(KFloat y, KFloat x) { return (KFloat)Kotlin_math_atan2(y, x); }
KFloat Kotlin_math_hypotf(KFloat x, KFloat y) { return (KFloat)Kotlin_math_hypot(x, y); }
// extensions
KFloat Kotlin_math_Float_pow(KFloat thiz, KFloat x) { return (KFloat)Kotlin_math_Double_pow(thiz, x); }
KFloat Kotlin_math_Float_IEEErem(KFloat thiz, KFloat divisor) {
return (KFloat)Kotlin_math_Double_IEEErem(thiz, divisor);
}
KFloat Kotlin_math_Float_withSign(KFloat thiz, KFloat sign) {
return (KFloat)Kotlin_math_Double_withSign(thiz, sign);
}
KFloat Kotlin_math_Float_nextUp(KFloat thiz) {
if (isnan(thiz) || thiz == HUGE_VALF) {
return thiz;
}
if (thiz == 0.0) {
return FLT_TRUE_MIN;
}
return bitsToFloat(floatToBits(thiz) + (thiz > 0 ? 1 : -1));
}
KFloat Kotlin_math_Float_nextDown(KFloat thiz) {
if (isnan(thiz) || thiz == -HUGE_VALF) {
return thiz;
}
if (thiz == 0.0) {
return -FLT_TRUE_MIN;
}
return bitsToFloat(floatToBits(thiz) - (thiz > 0 ? 1 : -1));
}
KFloat Kotlin_math_Float_nextTowards(KFloat thiz, KFloat to) {
if (isnan(thiz) || isnan(to)) {
return NAN;
}
if (to > thiz) {
return Kotlin_math_Float_nextUp(thiz);
}
if (to < thiz) {
return Kotlin_math_Float_nextDown(thiz);
}
// thiz == to
return to;
}
KBoolean Kotlin_math_Float_signBit(KFloat thiz) { return Kotlin_math_Double_signBit(thiz); }
// endregion
// region Integer math
KInt Kotlin_math_absi(KInt x) { return (x >= 0) ? x : -x; }
KLong Kotlin_math_absl(KLong x) { return (x >= 0) ? x : -x; }
#endif // #ifndef KONAN_WASM
#else // KONAN_NO_MATH defined - we have no patform math library. Throw NotImplementedError from math functions.
// kotlin.math isn't supported for WASM.
namespace {
RUNTIME_NORETURN void NotImplemented() {
+69
View File
@@ -0,0 +1,69 @@
/*
* Copyright 2010-2018 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RUNTIME_KOTLINMATH_H
#define RUNTIME_KOTLINMATH_H
#include "Types.h"
#ifdef KONAN_WASM
extern "C" {
JS::Arena Konan_js_getCurrentArena();
// Bridges for JS math.
void knjs__Math_abs(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_acos(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_acosh(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_asin(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_asinh(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_atan(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_atan2(KInt yUpper, KInt yLower, KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_atanh(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_cbrt(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_ceil(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_clz32(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_cos(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_cosh(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_exp(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_expm1(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_floor(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_fround(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_log(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_log1p(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_log10(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_log2(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_round(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_sign(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_sin(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_sinh(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_sqrt(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_tan(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_tanh(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_trunc(KInt xUpper, KInt xLower, JS::Arena resultArena);
void knjs__Math_hypot(KInt xUpper, KInt xLower, KInt yUpper, KInt yLower, JS::Arena resultArena);
void knjs__Math_max(KInt xUpper, KInt xLower, KInt yUpper, KInt yLower, JS::Arena resultArena);
void knjs__Math_min(KInt xUpper, KInt xLower, KInt yUpper, KInt yLower, JS::Arena resultArena);
void knjs__Math_pow(KInt xUpper, KInt xLower, KInt yUpper, KInt yLower, JS::Arena resultArena);
}
#endif // KONAN_WASM
#endif // RUNTIME_KOTLINMATH_H
+267
View File
@@ -0,0 +1,267 @@
/*
* Copyright 2010-2018 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
konan.libraries.push ({
knjs_get__Math_E: function() {
var result = Math.E;
return doubleToReturnSlot(result);
},
knjs_get__Math_LN2: function() {
var result = Math.LN2;
return doubleToReturnSlot(result);
},
knjs_get__Math_LN10: function() {
var result = Math.LN10;
return doubleToReturnSlot(result);
},
knjs_get__Math_LOG2E: function() {
var result = Math.LOG2E;
return doubleToReturnSlot(result);
},
knjs_get__Math_LOG10E: function() {
var result = Math.LOG10E;
return doubleToReturnSlot(result);
},
knjs_get__Math_PI: function() {
var result = Math.PI;
return doubleToReturnSlot(result);
},
knjs_get__Math_SQRT1_2: function() {
var result = Math.SQRT1_2;
return doubleToReturnSlot(result);
},
knjs_get__Math_SQRT2: function() {
var result = Math.SQRT2;
return doubleToReturnSlot(result);
},
knjs__Math_abs: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.abs(x);
return doubleToReturnSlot(result);
},
knjs__Math_acos: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.acos(x);
return doubleToReturnSlot(result);
},
knjs__Math_acosh: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.acosh(x);
return doubleToReturnSlot(result);
},
knjs__Math_asin: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.asin(x);
return doubleToReturnSlot(result);
},
knjs__Math_asinh: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.asinh(x);
return doubleToReturnSlot(result);
},
knjs__Math_atan: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.atan(x);
return doubleToReturnSlot(result);
},
knjs__Math_atanh: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.atanh(x);
return doubleToReturnSlot(result);
},
knjs__Math_atan2: function(yUpper, yLower, xUpper, xLower) {
var y = twoIntsToDouble(yUpper, yLower);
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.atan2(y, x);
return doubleToReturnSlot(result);
},
knjs__Math_cbrt: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.cbrt(x);
return doubleToReturnSlot(result);
},
knjs__Math_ceil: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.ceil(x);
return doubleToReturnSlot(result);
},
knjs__Math_clz32: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.clz32(x);
return doubleToReturnSlot(result);
},
knjs__Math_cos: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.cos(x);
return doubleToReturnSlot(result);
},
knjs__Math_cosh: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.cosh(x);
return doubleToReturnSlot(result);
},
knjs__Math_exp: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.exp(x);
return doubleToReturnSlot(result);
},
knjs__Math_expm1: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.expm1(x);
return doubleToReturnSlot(result);
},
knjs__Math_floor: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.floor(x);
return doubleToReturnSlot(result);
},
knjs__Math_fround: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.fround(x);
return doubleToReturnSlot(result);
},
knjs__Math_log: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.log(x);
return doubleToReturnSlot(result);
},
knjs__Math_log1p: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.log1p(x);
return doubleToReturnSlot(result);
},
knjs__Math_log10: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.log10(x);
return doubleToReturnSlot(result);
},
knjs__Math_log2: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.log2(x);
return doubleToReturnSlot(result);
},
knjs__Math_pow: function(xUpper, xLower, yUpper, yLower) {
var x = twoIntsToDouble(xUpper, xLower);
var y = twoIntsToDouble(yUpper, yLower);
var result = Math.pow(x, y);
return doubleToReturnSlot(result);
},
knjs__Math_random: function() {
var result = Math.random();
return doubleToReturnSlot(result);
},
knjs__Math_round: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.round(x);
return doubleToReturnSlot(result);
},
knjs__Math_sign: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.sign(x);
return doubleToReturnSlot(result);
},
knjs__Math_sin: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.sin(x);
return doubleToReturnSlot(result);
},
knjs__Math_sinh: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.sinh(x);
return doubleToReturnSlot(result);
},
knjs__Math_sqrt: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.sqrt(x);
return doubleToReturnSlot(result);
},
knjs__Math_tan: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.tan(x);
return doubleToReturnSlot(result);
},
knjs__Math_tanh: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.tanh(x);
return doubleToReturnSlot(result);
},
knjs__Math_trunc: function(xUpper, xLower) {
var x = twoIntsToDouble(xUpper, xLower);
var result = Math.trunc(x);
return doubleToReturnSlot(result);
},
knjs__Math_hypot: function(xUpper, xLower, yUpper, yLower) {
var x = twoIntsToDouble(xUpper, xLower);
var y = twoIntsToDouble(yUpper, yLower);
var result = Math.hypot(x, y);
return doubleToReturnSlot(result);
},
knjs__Math_max: function(xUpper, xLower, yUpper, yLower) {
var x = twoIntsToDouble(xUpper, xLower);
var y = twoIntsToDouble(yUpper, yLower);
var result = Math.max(x, y);
return doubleToReturnSlot(result);
},
knjs__Math_min: function(xUpper, xLower, yUpper, yLower) {
var x = twoIntsToDouble(xUpper, xLower);
var y = twoIntsToDouble(yUpper, yLower);
var result = Math.min(x, y);
return doubleToReturnSlot(result);
}
})
@@ -138,7 +138,7 @@ class ClangArgs(private val configurables: Configurables) : Configurables by con
KonanTarget.WASM32 ->
listOf("-DKONAN_WASM=1", "-DKONAN_NO_FFI=1", "-DKONAN_NO_THREADS=1", "-DKONAN_NO_EXCEPTIONS=1",
"-DKONAN_NO_MATH=1", "-DKONAN_INTERNAL_DLMALLOC=1", "-DKONAN_INTERNAL_SNPRINTF=1",
"-DKONAN_INTERNAL_DLMALLOC=1", "-DKONAN_INTERNAL_SNPRINTF=1",
"-DKONAN_INTERNAL_NOW=1", "-DKONAN_NO_MEMMEM", "-DKONAN_NO_CTORS_SECTION")
is KonanTarget.ZEPHYR ->