Skip to content

Commit d721c1b

Browse files
Use log function from libm.
1 parent a7f26c7 commit d721c1b

File tree

9 files changed

+222
-7
lines changed

9 files changed

+222
-7
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.posix;
26+
27+
import com.oracle.svm.core.Uninterruptible;
28+
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
29+
import com.oracle.svm.core.headers.LibMSupport;
30+
import com.oracle.svm.core.posix.headers.PosixLibM;
31+
32+
@AutomaticallyRegisteredImageSingleton(LibMSupport.class)
33+
public class PosixLibMSupport implements LibMSupport {
34+
@Override
35+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
36+
public double log(double value) {
37+
return PosixLibM.log(value);
38+
}
39+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.posix.headers;
26+
27+
import java.util.Collections;
28+
import java.util.List;
29+
30+
import org.graalvm.nativeimage.c.CContext;
31+
import org.graalvm.nativeimage.c.function.CFunction;
32+
33+
import com.oracle.svm.core.jfr.HasJfrSupport;
34+
35+
@CContext(value = LibMDependencies.class)
36+
public class PosixLibM {
37+
@CFunction(transition = CFunction.Transition.NO_TRANSITION)
38+
public static native double log(double value);
39+
}
40+
41+
class LibMDependencies implements CContext.Directives {
42+
@Override
43+
public boolean isInConfiguration() {
44+
return HasJfrSupport.get();
45+
}
46+
47+
@Override
48+
public List<String> getHeaderFiles() {
49+
return Collections.singletonList("<math.h>");
50+
}
51+
52+
@Override
53+
public List<String> getLibraries() {
54+
return Collections.singletonList("m");
55+
}
56+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.windows;
26+
27+
import com.oracle.svm.core.Uninterruptible;
28+
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
29+
import com.oracle.svm.core.headers.LibMSupport;
30+
import com.oracle.svm.core.windows.headers.WindowsLibC;
31+
32+
@AutomaticallyRegisteredImageSingleton(LibMSupport.class)
33+
public class WindowsLibMSupport implements LibMSupport {
34+
@Override
35+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
36+
public double log(double value) {
37+
return WindowsLibC.log(value);
38+
}
39+
}

substratevm/src/com.oracle.svm.core.windows/src/com/oracle/svm/core/windows/headers/WindowsDirectives.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public class WindowsDirectives implements CContext.Directives {
4444
"<stdio.h>",
4545
"<stdlib.h>",
4646
"<string.h>",
47-
"<io.h>"
47+
"<io.h>",
48+
"<math.h>"
4849
};
4950

5051
@Override
@@ -55,8 +56,7 @@ public boolean isInConfiguration() {
5556
@Override
5657
public List<String> getHeaderFiles() {
5758
if (Platform.includedIn(Platform.WINDOWS.class)) {
58-
List<String> result = new ArrayList<>(Arrays.asList(windowsLibs));
59-
return result;
59+
return new ArrayList<>(Arrays.asList(windowsLibs));
6060
} else {
6161
throw VMError.shouldNotReachHere("Unsupported OS");
6262
}

substratevm/src/com.oracle.svm.core.windows/src/com/oracle/svm/core/windows/headers/WindowsLibC.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,7 @@ public interface WCharPointer extends PointerBase {
104104

105105
@CFunction(transition = CFunction.Transition.NO_TRANSITION)
106106
public static native UnsignedWord strtoull(CCharPointer string, CCharPointerPointer endPtr, int base);
107+
108+
@CFunction(transition = CFunction.Transition.NO_TRANSITION)
109+
public static native double log(double value);
107110
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/headers/LibC.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package com.oracle.svm.core.headers;
2626

27-
import jdk.graal.compiler.api.replacements.Fold;
2827
import org.graalvm.nativeimage.ImageSingletons;
2928
import org.graalvm.nativeimage.c.type.CCharPointer;
3029
import org.graalvm.nativeimage.c.type.CCharPointerPointer;
@@ -34,6 +33,8 @@
3433

3534
import com.oracle.svm.core.Uninterruptible;
3635

36+
import jdk.graal.compiler.api.replacements.Fold;
37+
3738
public class LibC {
3839
public static final int EXIT_CODE_ABORT = 99;
3940

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.headers;
26+
27+
import static com.oracle.svm.core.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;
28+
29+
import org.graalvm.nativeimage.ImageSingletons;
30+
31+
import com.oracle.svm.core.Uninterruptible;
32+
33+
import jdk.graal.compiler.api.replacements.Fold;
34+
35+
public class LibM {
36+
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
37+
public static double log(double value) {
38+
return libm().log(value);
39+
}
40+
41+
@Fold
42+
static LibMSupport libm() {
43+
return ImageSingletons.lookup(LibMSupport.class);
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.headers;
26+
27+
import com.oracle.svm.core.Uninterruptible;
28+
29+
public interface LibMSupport {
30+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
31+
double log(double value);
32+
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/throttling/JfrAdaptiveSampler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
package com.oracle.svm.core.jfr.throttling;
2828

2929
import static com.oracle.svm.core.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;
30-
import static java.lang.Math.log;
3130

3231
import com.oracle.svm.core.Uninterruptible;
32+
import com.oracle.svm.core.headers.LibM;
3333
import com.oracle.svm.core.jdk.UninterruptibleUtils;
3434
import com.oracle.svm.core.jfr.utils.JfrRandom;
3535
import com.oracle.svm.core.thread.JavaSpinLockUtils;
@@ -172,7 +172,7 @@ private long deriveSamplingInterval(double sampleSize, JfrSamplerWindow expired)
172172

173173
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
174174
private double projectPopulationSize(JfrSamplerWindow expired) {
175-
avgPopulationSize = exponentiallyWeightedMovingAverage((double) expired.getPopulationSize(), ewmaPopulationSizeAlpha, avgPopulationSize);
175+
avgPopulationSize = exponentiallyWeightedMovingAverage(expired.getPopulationSize(), ewmaPopulationSizeAlpha, avgPopulationSize);
176176
return avgPopulationSize;
177177
}
178178

@@ -191,7 +191,7 @@ private long nextGeometric(double p) {
191191
} else if (u == 1.0) {
192192
u = 0.99;
193193
}
194-
return UninterruptibleUtils.Math.ceilToLong(log(1.0 - u) / log(1.0 - p));
194+
return UninterruptibleUtils.Math.ceilToLong(LibM.log(1.0 - u) / LibM.log(1.0 - p));
195195
}
196196

197197
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)

0 commit comments

Comments
 (0)