Skip to content

Commit b3a1847

Browse files
committed
Math emulation improvements
1 parent 552c72e commit b3a1847

File tree

12 files changed

+306
-67
lines changed

12 files changed

+306
-67
lines changed

dev/core/src/com/google/gwt/dev/cfg/ModuleDefSchema.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@
4343

4444
import javax.lang.model.SourceVersion;
4545

46-
// CHECKSTYLE_NAMING_OFF
47-
4846
/**
4947
* Configures a module definition object using XML.
5048
*/
49+
@SuppressWarnings({"checkstyle.MemberName", "checkstyle.MethodName"})
5150
public class ModuleDefSchema extends Schema {
5251

5352
private final class BodySchema extends Schema {
@@ -301,8 +300,8 @@ protected Schema __collapse_property_begin(PropertyName name,
301300

302301
@SuppressWarnings("unused") // called reflectively
303302
protected Schema __define_configuration_property_begin(PropertyName name,
304-
String is_multi_valued) throws UnableToCompleteException {
305-
boolean isMultiValued = toPrimitiveBoolean(is_multi_valued);
303+
String isMultiValuedStr) throws UnableToCompleteException {
304+
boolean isMultiValued = toPrimitiveBoolean(isMultiValuedStr);
306305

307306
// Don't allow configuration properties with the same name as a
308307
// deferred-binding property.
@@ -1513,4 +1512,3 @@ private JsFunction parseJsBlock(int startLineNumber, String script)
15131512
return fn;
15141513
}
15151514
}
1516-
// CHECKSTYLE_NAMING_ON

dev/core/src/com/google/gwt/dev/js/JsToStringGenerationVisitor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
/**
8585
* Produces text output from a JavaScript AST.
8686
*/
87+
@SuppressWarnings("checkstyle.MethodName")
8788
public class JsToStringGenerationVisitor extends JsVisitor {
8889

8990
private static final char[] CHARS_BREAK = "break".toCharArray();
@@ -888,8 +889,6 @@ public boolean visit(JsWhile x, JsContext ctx) {
888889
return false;
889890
}
890891

891-
//CHECKSTYLE_NAMING_OFF
892-
893892
protected void _newline() {
894893
p.newline();
895894
}
@@ -1308,8 +1307,6 @@ private void _while() {
13081307
p.print(CHARS_WHILE);
13091308
}
13101309

1311-
// CHECKSTYLE_NAMING_ON
1312-
13131310
private void indent() {
13141311
p.indentIn();
13151312
}

dev/core/super/com/google/gwt/dev/jjs/intrinsic/com/google/gwt/lang/Cast.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121

2222
import javaemul.internal.annotations.HasNoSideEffects;
2323

24-
// CHECKSTYLE_NAMING_OFF: Uses legacy conventions of underscore prefixes.
25-
2624
/**
2725
* This is a magic class the compiler uses to perform any cast operations that require code.<br />
2826
*
2927
* The cast operations are only as accurate as the contents of the castableTypeMaps and should not
3028
* be used directly by user code. The compiler takes care to record most cast operations in user
3129
* code so that it can build limited but accurate castableTypeMaps.
3230
*/
31+
@SuppressWarnings("checkstyle.MethodName")
3332
final class Cast {
3433

3534
/**
@@ -393,5 +392,3 @@ static native Class<?> getClass(Object array) /*-{
393392
|| @JavaScriptObject::class;
394393
}-*/;
395394
}
396-
397-
// CHECKSTYLE_NAMING_ON

eclipse/settings/code-style/gwt-checkstyle-tests.xml

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ A more lenient set of style checks for test cases, needed because test often nee
1111
<module name="Checker">
1212
<property name="severity" value="warning"/>
1313
<module name="RegexpHeader">
14-
<property name="fileExtensions" value="java"/>
14+
<property name="fileExtensions" value="java"/>
1515
<property name="severity" value="error"/>
1616
<property name="header" value="^/\*[ ]*$\n^ \* Copyright 20(0[6789]|[12][0-9]) (Google Inc\.|GWT Project Authors)$\n^ \*[ ]*$\n^ \* Licensed under the Apache License, Version 2\.0 \(the &quot;License&quot;\); you may not( use this file except)?$\n^ \* (use this file except )?in compliance with the License\. You may obtain a copy of( the License at)?$\n^ \* the License at$\n^ \*[ ]*$\n^ \* http://www\.apache\.org/licenses/LICENSE-2\.0$\n^ \*[ ]*\n \* Unless required by applicable law or agreed to in writing, software( distributed under the License)?$\n^ \* (distributed under the License )?is distributed on an &quot;AS IS&quot; BASIS, WITHOUT( WARRANTIES OR CONDITIONS OF ANY KIND, either express)?$\n^ \* (WARRANTIES OR CONDITIONS OF ANY KIND, either express )?or implied\. See the( License for the specific language governing permissions and limitations under)?$\n^ \* License for the specific language governing permissions and limitations under$\n^ \* the License\.$\n^ \*/$"/>
1717
<property name="multiLines" value="6,13"/>
@@ -22,7 +22,8 @@ A more lenient set of style checks for test cases, needed because test often nee
2222
<property name="severity" value="error"/>
2323
</module>
2424
<module name="TreeWalker">
25-
<property name="fileExtensions" value="java"/>
25+
<property name="fileExtensions" value="java"/>
26+
<module name="SuppressWarningsHolder" />
2627
<module name="InterfaceIsType">
2728
<metadata name="com.atlassw.tools.eclipse.checkstyle.lastEnabledSeverity" value="error"/>
2829
<property name="severity" value="ignore"/>
@@ -211,24 +212,6 @@ A more lenient set of style checks for test cases, needed because test often nee
211212
</module>
212213
<module name="RedundantModifier"/>
213214
<module name="EqualsHashCode"/>
214-
<module name="SuppressionCommentFilter">
215-
<metadata name="com.atlassw.tools.eclipse.checkstyle.comment" value="Avoid name checking"/>
216-
<property name="offCommentFormat" value="CHECKSTYLE_NAMING_OFF"/>
217-
<property name="onCommentFormat" value="CHECKSTYLE_NAMING_ON"/>
218-
<property name="checkFormat" value="MethodName"/>
219-
</module>
220-
<module name="SuppressionCommentFilter">
221-
<metadata name="com.atlassw.tools.eclipse.checkstyle.comment" value="Avoid name checking"/>
222-
<property name="offCommentFormat" value="CHECKSTYLE_NAMING_OFF"/>
223-
<property name="onCommentFormat" value="CHECKSTYLE_NAMING_ON"/>
224-
<property name="checkFormat" value="MemberName"/>
225-
</module>
226-
<module name="SuppressionCommentFilter">
227-
<metadata name="com.atlassw.tools.eclipse.checkstyle.comment" value="Avoid name checking"/>
228-
<property name="offCommentFormat" value="CHECKSTYLE_NAMING_OFF"/>
229-
<property name="onCommentFormat" value="CHECKSTYLE_NAMING_ON"/>
230-
<property name="checkFormat" value="ParameterName"/>
231-
</module>
232215
</module>
233216
<module name="SuppressWithPlainTextCommentFilter">
234217
<property name="offCommentFormat" value="CHECKSTYLE_OFF"/>
@@ -237,4 +220,5 @@ A more lenient set of style checks for test cases, needed because test often nee
237220
<module name="JavadocPackage">
238221
<property name="severity" value="ignore"/>
239222
</module>
223+
<module name="SuppressWarningsFilter" />
240224
</module>

eclipse/settings/code-style/gwt-checkstyle.xml

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Description:
2727
</module>
2828
<module name="TreeWalker">
2929
<property name="fileExtensions" value="java"/>
30-
<module name="InterfaceIsType">
30+
<module name="SuppressWarningsHolder" />
31+
<module name="InterfaceIsType">
3132
<property name="severity" value="ignore"/>
3233
</module>
3334
<module name="RedundantImport">
@@ -195,25 +196,6 @@ Description:
195196
</module>
196197
<module name="RedundantModifier"/>
197198
<module name="EqualsHashCode"/>
198-
199-
<module name="SuppressionCommentFilter">
200-
<metadata name="com.atlassw.tools.eclipse.checkstyle.comment" value="Avoid name checking"/>
201-
<property name="offCommentFormat" value="CHECKSTYLE_NAMING_OFF"/>
202-
<property name="onCommentFormat" value="CHECKSTYLE_NAMING_ON"/>
203-
<property name="checkFormat" value="MethodName"/>
204-
</module>
205-
<module name="SuppressionCommentFilter">
206-
<metadata name="com.atlassw.tools.eclipse.checkstyle.comment" value="Avoid name checking"/>
207-
<property name="offCommentFormat" value="CHECKSTYLE_NAMING_OFF"/>
208-
<property name="onCommentFormat" value="CHECKSTYLE_NAMING_ON"/>
209-
<property name="checkFormat" value="MemberName"/>
210-
</module>
211-
<module name="SuppressionCommentFilter">
212-
<metadata name="com.atlassw.tools.eclipse.checkstyle.comment" value="Avoid name checking"/>
213-
<property name="offCommentFormat" value="CHECKSTYLE_NAMING_OFF"/>
214-
<property name="onCommentFormat" value="CHECKSTYLE_NAMING_ON"/>
215-
<property name="checkFormat" value="ParameterName"/>
216-
</module>
217199
</module>
218200
<module name="SuppressWithPlainTextCommentFilter">
219201
<property name="offCommentFormat" value="CHECKSTYLE_OFF"/>
@@ -222,4 +204,5 @@ Description:
222204
<module name="JavadocPackage">
223205
<property name="severity" value="ignore"/>
224206
</module>
207+
<module name="SuppressWarningsFilter" />
225208
</module>

samples/validation/src/main/java/com/google/gwt/sample/validation/client/ValidationMessages.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public interface ValidationMessages extends
3333
@DefaultStringValue("Name must be at least {min} characters long.")
3434
@Key("custom.name.size.message")
3535

36-
// CHECKSTYLE_NAMING_OFF
36+
@SuppressWarnings("checkstyle.MethodName")
3737
String custom_name_size_message();
38-
// CHECKSTYLE_NAMING_ON
3938
}

user/super/com/google/gwt/emul/java/lang/Math.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static javaemul.internal.InternalPreconditions.checkCriticalArithmetic;
1919

20+
import javaemul.internal.JsUtils;
2021
import jsinterop.annotations.JsMethod;
2122
import jsinterop.annotations.JsPackage;
2223
import jsinterop.annotations.JsType;
@@ -31,7 +32,6 @@ public final class Math {
3132
// public static float ulp (float x)
3233
// public static int getExponent (double d)
3334
// public static int getExponent (float f)
34-
// public static double IEEEremainder(double f1, double f2)
3535

3636
public static final double E = 2.7182818284590452354;
3737
public static final double PI = 3.14159265358979323846;
@@ -52,6 +52,16 @@ public static long abs(long x) {
5252
return x < 0 ? -x : x;
5353
}
5454

55+
public static int absExact(int v) {
56+
checkCriticalArithmetic(v != Integer.MIN_VALUE);
57+
return abs(v);
58+
}
59+
60+
public static long absExact(long v) {
61+
checkCriticalArithmetic(v != Long.MIN_VALUE);
62+
return abs(v);
63+
}
64+
5565
@JsMethod(namespace = JsPackage.GLOBAL, name = "Math.acos")
5666
public static native double acos(double x);
5767

@@ -116,9 +126,8 @@ public static long decrementExact(long x) {
116126
@JsMethod(namespace = JsPackage.GLOBAL, name = "Math.exp")
117127
public static native double exp(double x);
118128

119-
public static double expm1(double d) {
120-
return d == 0 ? d : exp(d) - 1;
121-
}
129+
@JsMethod(namespace = JsPackage.GLOBAL, name = "Math.expm1")
130+
public static native double expm1(double d);
122131

123132
@JsMethod(namespace = JsPackage.GLOBAL, name = "Math.floor")
124133
public static native double floor(double x);
@@ -145,11 +154,30 @@ public static long floorMod(long dividend, long divisor) {
145154
return ((dividend % divisor) + divisor) % divisor;
146155
}
147156

148-
public static double hypot(double x, double y) {
149-
return Double.isInfinite(x) || Double.isInfinite(y) ?
150-
Double.POSITIVE_INFINITY : sqrt(x * x + y * y);
157+
@SuppressWarnings("CheckStyle.MethodName")
158+
public static double IEEEremainder(double v, double m) {
159+
double ratio = v / m;
160+
double closest = Math.ceil(ratio);
161+
double frac = Math.abs(closest - ratio);
162+
if (frac > 0.5 || frac == 0.5 && (closest % 2 != 0)) {
163+
closest = Math.floor(ratio);
164+
}
165+
// if closest == 0 and m == inf, avoid multiplication
166+
return closest == 0 ? v : v - m * closest;
167+
}
168+
169+
public static int getExponent(double v) {
170+
int[] intBits = JsUtils.doubleToRawIntBits(v);
171+
return ((intBits[1] >> 20) & 2047) - Double.MAX_EXPONENT;
172+
}
173+
174+
public static int getExponent(float v) {
175+
return ((JsUtils.floatToRawIntBits(v) >> 23) & 255) - Float.MAX_EXPONENT;
151176
}
152177

178+
@JsMethod(namespace = JsPackage.GLOBAL, name = "Math.hypot")
179+
public static native double hypot(double x, double y);
180+
153181
public static int incrementExact(int x) {
154182
checkCriticalArithmetic(x != Integer.MAX_VALUE);
155183
return x + 1;
@@ -167,9 +195,8 @@ public static double log10(double x) {
167195
return log(x) * NativeMath.LOG10E;
168196
}
169197

170-
public static double log1p(double x) {
171-
return x == 0 ? x : log(x + 1);
172-
}
198+
@JsMethod(namespace = JsPackage.GLOBAL, name = "Math.log1p")
199+
public static native double log1p(double x);
173200

174201
@JsMethod(namespace = JsPackage.GLOBAL, name = "Math.max")
175202
public static native double max(double x, double y);

user/super/com/google/gwt/emul/javaemul/internal/JsUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,14 @@ public static float intBitsToFloat(int value) {
123123
return JsUtils.<float[]>uncheckedCast(new Float32Array(buf))[0];
124124
}
125125

126-
public static long doubleToRawLongBits(double value) {
126+
public static int[] doubleToRawIntBits(double value) {
127127
ArrayBuffer buf = new ArrayBuffer(8);
128128
JsUtils.<double[]>uncheckedCast(new Float64Array(buf))[0] = value;
129-
int[] intBits = JsUtils.<int[]>uncheckedCast(new Uint32Array(buf));
129+
return JsUtils.<int[]>uncheckedCast(new Uint32Array(buf));
130+
}
131+
132+
public static long doubleToRawLongBits(double value) {
133+
int[] intBits = doubleToRawIntBits(value);
130134
return LongUtils.fromBits(intBits[0] | 0, intBits[1] | 0);
131135
}
132136

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2025 GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.google.gwt.emultest.java17.lang;
17+
18+
import com.google.gwt.emultest.java.util.EmulTestBase;
19+
20+
/**
21+
* Tests for java.lang.String Java 12 API emulation.
22+
*/
23+
public class StringTest extends EmulTestBase {
24+
25+
public void testAbsExact() {
26+
assertEquals(Integer.MAX_VALUE, Math.absExact(hideFromCompiler(Integer.MAX_VALUE)));
27+
assertEquals(0, Math.absExact(hideFromCompiler(0)));
28+
assertEquals(1, Math.absExact(hideFromCompiler(-1)));
29+
assertThrowsArithmetic(() -> Math.absExact(hideFromCompiler(Integer.MIN_VALUE)));
30+
assertEquals(Long.MAX_VALUE, Math.absExact(hideFromCompiler(Long.MAX_VALUE)));
31+
assertEquals(0, Math.absExact(hideFromCompiler(0l)));
32+
assertEquals(1, Math.absExact(hideFromCompiler(-1l)));
33+
assertThrowsArithmetic(() -> Math.absExact(hideFromCompiler(Long.MIN_VALUE)));
34+
}
35+
36+
public void testAbsExactWithFolding() {
37+
assertEquals(Integer.MAX_VALUE, Math.absExact(Integer.MAX_VALUE));
38+
assertEquals(0, Math.absExact(0));
39+
assertEquals(1, Math.absExact(-1));
40+
assertThrowsArithmetic(() -> Math.absExact(Integer.MIN_VALUE));
41+
assertEquals(Long.MAX_VALUE, Math.absExact(Long.MAX_VALUE));
42+
assertEquals(0, Math.absExact(0L));
43+
assertEquals(1, Math.absExact(-1L));
44+
assertThrowsArithmetic(() -> Math.absExact(Long.MIN_VALUE));
45+
}
46+
}

user/test/com/google/gwt/emultest/EmulJava17Suite.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.gwt.emultest;
1717

1818
import com.google.gwt.emultest.java17.lang.CharSequenceTest;
19+
import com.google.gwt.emultest.java17.lang.MathTest;
1920
import com.google.gwt.emultest.java17.lang.StringTest;
2021
import com.google.gwt.emultest.java17.util.MapEntryTest;
2122
import com.google.gwt.emultest.java17.util.stream.CollectorsTest;
@@ -32,6 +33,7 @@
3233
@Suite.SuiteClasses({
3334
CharSequenceTest.class,
3435
StringTest.class,
36+
MathTest.class,
3537
CollectorsTest.class,
3638
StreamTest.class,
3739
DoubleStreamTest.class,

0 commit comments

Comments
 (0)