Skip to content

Commit 7f80afc

Browse files
committed
[GR-54521] Reimplement asinh, acosh, atanh using MathUtils.
PullRequest: graalpython/3359
2 parents f5272b6 + a53abc4 commit 7f80afc

File tree

2 files changed

+9
-53
lines changed

2 files changed

+9
-53
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/CmathModuleBuiltins.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2020, 2023, Oracle and/or its affiliates.
1+
/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -47,6 +47,7 @@
4747
import com.oracle.truffle.api.dsl.TypeSystemReference;
4848
import com.oracle.truffle.api.frame.VirtualFrame;
4949
import com.oracle.truffle.api.nodes.Node;
50+
import com.oracle.truffle.api.utilities.MathUtils;
5051

5152
@CoreFunctions(defineModule = "cmath")
5253
public final class CmathModuleBuiltins extends PythonBuiltins {
@@ -634,7 +635,7 @@ static ComplexValue compute(Node inliningTarget, double real, double imag, PRais
634635
ComplexValue s1 = SqrtNode.compute(inliningTarget, 1.0 - real, -imag, raiseNode);
635636
ComplexValue s2 = SqrtNode.compute(inliningTarget, 1.0 + real, imag, raiseNode);
636637
rreal = 2.0 * Math.atan2(s1.real, s2.real);
637-
rimag = MathModuleBuiltins.AsinhNode.compute(s2.real * s1.imag - s2.imag * s1.real);
638+
rimag = MathUtils.asinh(s2.real * s1.imag - s2.imag * s1.real);
638639
}
639640
return new ComplexValue(rreal, rimag);
640641
}
@@ -677,7 +678,7 @@ static ComplexValue compute(Node inliningTarget, double real, double imag, PRais
677678
} else {
678679
ComplexValue s1 = SqrtNode.compute(inliningTarget, real - 1.0, imag, raiseNode);
679680
ComplexValue s2 = SqrtNode.compute(inliningTarget, real + 1.0, imag, raiseNode);
680-
rreal = MathModuleBuiltins.AsinhNode.compute(s1.real * s2.real + s1.imag * s2.imag);
681+
rreal = MathUtils.asinh(s1.real * s2.real + s1.imag * s2.imag);
681682
rimag = 2.0 * Math.atan2(s1.imag, s2.real);
682683
}
683684
return new ComplexValue(rreal, rimag);
@@ -743,7 +744,7 @@ static ComplexValue compute(Node inliningTarget, double real, double imag, PRais
743744
} else {
744745
ComplexValue s1 = SqrtNode.compute(inliningTarget, 1.0 + imag, -real, raiseNode);
745746
ComplexValue s2 = SqrtNode.compute(inliningTarget, 1.0 - imag, real, raiseNode);
746-
rreal = MathModuleBuiltins.AsinhNode.compute(s1.real * s2.imag - s2.real * s1.imag);
747+
rreal = MathUtils.asinh(s1.real * s2.imag - s2.real * s1.imag);
747748
rimag = Math.atan2(imag, s1.real * s2.real - s1.imag * s2.imag);
748749
}
749750
return new ComplexValue(rreal, rimag);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
106106
import com.oracle.truffle.api.profiles.InlinedLoopConditionProfile;
107107
import com.oracle.truffle.api.profiles.LoopConditionProfile;
108+
import com.oracle.truffle.api.utilities.MathUtils;
108109

109110
@CoreFunctions(defineModule = "math")
110111
public final class MathModuleBuiltins extends PythonBuiltins {
@@ -1258,7 +1259,6 @@ private static double compute(Node inliningTarget, double value, PRaiseNode.Lazy
12581259
@GenerateNodeFactory
12591260
public abstract static class AcoshNode extends PythonUnaryBuiltinNode {
12601261

1261-
private static final double TWO_POW_P28 = 0x1.0p28;
12621262
private static final double LN_2 = 6.93147180559945286227e-01;
12631263

12641264
@Specialization
@@ -1285,14 +1285,7 @@ static double doGeneric(VirtualFrame frame, Object value,
12851285

12861286
private static double compute(Node inliningTarget, double value, PRaiseNode.Lazy raiseNode) {
12871287
checkMathDomainError(value < 1, inliningTarget, raiseNode);
1288-
if (value >= TWO_POW_P28) {
1289-
return Math.log(value) + LN_2;
1290-
}
1291-
if (value <= 2.0) {
1292-
double t = value - 1.0;
1293-
return Math.log1p(t + Math.sqrt(2.0 * t + t * t));
1294-
}
1295-
return Math.log(value + Math.sqrt(value * value - 1.0));
1288+
return MathUtils.acosh(value);
12961289
}
12971290
}
12981291

@@ -1433,8 +1426,6 @@ private static double compute(Node inliningTarget, double value, PRaiseNode.Lazy
14331426
@GenerateNodeFactory
14341427
public abstract static class AtanhNode extends PythonUnaryBuiltinNode {
14351428

1436-
private static final double TWO_POW_M28 = 0x1.0p-28;
1437-
14381429
@Specialization
14391430
static double doGeneric(VirtualFrame frame, Object value,
14401431
@Bind("this") Node inliningTarget,
@@ -1445,17 +1436,7 @@ static double doGeneric(VirtualFrame frame, Object value,
14451436
private static double compute(Node inliningTarget, double value, PRaiseNode.Lazy raiseNode) {
14461437
double abs = Math.abs(value);
14471438
checkMathDomainError(abs >= 1.0, inliningTarget, raiseNode);
1448-
if (abs < TWO_POW_M28) {
1449-
return value;
1450-
}
1451-
double t;
1452-
if (abs < 0.5) {
1453-
t = abs + abs;
1454-
t = 0.5 * Math.log1p(t + t * abs / (1.0 - abs));
1455-
} else {
1456-
t = 0.5 * Math.log1p((abs + abs) / (1.0 - abs));
1457-
}
1458-
return Math.copySign(t, value);
1439+
return MathUtils.atanh(value);
14591440
}
14601441
}
14611442

@@ -1465,41 +1446,15 @@ private static double compute(Node inliningTarget, double value, PRaiseNode.Lazy
14651446
@GenerateNodeFactory
14661447
public abstract static class AsinhNode extends PythonUnaryBuiltinNode {
14671448

1468-
private static final double LN_2 = 6.93147180559945286227e-01;
1469-
private static final double TWO_POW_P28 = 0x1.0p28;
1470-
private static final double TWO_POW_M28 = 0x1.0p-28;
1471-
14721449
@Specialization
14731450
static double doGeneric(VirtualFrame frame, Object value,
14741451
@Bind("this") Node inliningTarget,
14751452
@Cached MathUnaryHelperNode helperNode) {
14761453
return helperNode.execute(frame, inliningTarget, value, AsinhNode::compute);
14771454
}
14781455

1479-
@TruffleBoundary
14801456
private static double compute(Node inliningTarget, double value, PRaiseNode.Lazy raiseNode) {
1481-
return compute(value);
1482-
}
1483-
1484-
static double compute(double value) {
1485-
double absx = Math.abs(value);
1486-
1487-
if (Double.isNaN(value) || Double.isInfinite(value)) {
1488-
return value + value;
1489-
}
1490-
if (absx < TWO_POW_M28) {
1491-
return value;
1492-
}
1493-
double w;
1494-
if (absx > TWO_POW_P28) {
1495-
w = Math.log(absx) + LN_2;
1496-
} else if (absx > 2.0) {
1497-
w = Math.log(2.0 * absx + 1.0 / (Math.sqrt(value * value + 1.0) + absx));
1498-
} else {
1499-
double t = value * value;
1500-
w = Math.log1p(absx + t / (1.0 + Math.sqrt(1.0 + t)));
1501-
}
1502-
return Math.copySign(w, value);
1457+
return MathUtils.asinh(value);
15031458
}
15041459

15051460
public static AsinhNode create() {

0 commit comments

Comments
 (0)