jdk

Форк
0
/
TestFloatClassCheck.java 
72 строки · 2.7 Кб
1
/*
2
 * Copyright (c) 2022, 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.
8
 *
9
 * This code is distributed in the hope that it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 * version 2 for more details (a copy is included in the LICENSE file that
13
 * accompanied this code).
14
 *
15
 * You should have received a copy of the GNU General Public License version
16
 * 2 along with this work; if not, write to the Free Software Foundation,
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
 * or visit www.oracle.com if you need additional information or have any
21
 * questions.
22
 */
23

24
package compiler.intrinsics;
25
import java.util.random.RandomGenerator;
26
import java.util.random.RandomGeneratorFactory;
27

28
public class TestFloatClassCheck {
29
    RandomGenerator rng;
30
    int BUFFER_SIZE = 1024;
31
    float[] inputs;
32
    boolean[] outputs;
33

34
    public TestFloatClassCheck() {
35
        outputs = new boolean[BUFFER_SIZE];
36
        inputs = new float[BUFFER_SIZE];
37
        RandomGenerator rng = RandomGeneratorFactory.getDefault().create(0);
38
        float input;
39
        for (int i = 0; i < BUFFER_SIZE; i++) {
40
            if (i % 5 == 0) {
41
                input = (i%2 == 0) ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
42
            }
43
            else if (i % 3 == 0) input = Float.NaN;
44
            else input = rng.nextFloat();
45
            inputs[i] = input;
46
        }
47
    }
48

49
    public void checkResult(String method) {
50
        for (int i=0; i < BUFFER_SIZE; i++) {
51
            boolean expected = floatClassCheck(inputs[i], method);
52
            if (expected != outputs[i]) {
53
                String errorMsg = "Correctness check failed for Float." + method +
54
                "() for input = " + inputs[i];
55
                throw new RuntimeException(errorMsg);
56
            }
57
        }
58
    }
59

60
    public boolean floatClassCheck(float f, String method) {
61
        int infBits = Float.floatToRawIntBits(Float.POSITIVE_INFINITY);
62
        int bits =  Float.floatToRawIntBits(f);
63
        bits = bits & Integer.MAX_VALUE;
64
        switch (method) {
65
            case "isFinite": return (bits < infBits);
66
            case "isInfinite": return (bits == infBits);
67
            case "isNaN": return (bits > infBits);
68
            default: throw new IllegalArgumentException("incorrect method for Float");
69
        }
70
    }
71

72
}
73

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.