1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dexgen.rop.cst;
18
19import com.android.dexgen.rop.type.Type;
20import com.android.dexgen.util.Hex;
21
22/**
23 * Constants of type {@code CONSTANT_Float_info}.
24 */
25public final class CstFloat
26        extends CstLiteral32 {
27    /** {@code non-null;} instance representing {@code 0} */
28    public static final CstFloat VALUE_0 = make(Float.floatToIntBits(0.0f));
29
30    /** {@code non-null;} instance representing {@code 1} */
31    public static final CstFloat VALUE_1 = make(Float.floatToIntBits(1.0f));
32
33    /** {@code non-null;} instance representing {@code 2} */
34    public static final CstFloat VALUE_2 = make(Float.floatToIntBits(2.0f));
35
36    /**
37     * Makes an instance for the given value. This may (but does not
38     * necessarily) return an already-allocated instance.
39     *
40     * @param bits the {@code float} value as {@code int} bits
41     */
42    public static CstFloat make(int bits) {
43        /*
44         * Note: Javadoc notwithstanding, this implementation always
45         * allocates.
46         */
47        return new CstFloat(bits);
48    }
49
50    /**
51     * Constructs an instance. This constructor is private; use {@link #make}.
52     *
53     * @param bits the {@code float} value as {@code int} bits
54     */
55    private CstFloat(int bits) {
56        super(bits);
57    }
58
59    /** {@inheritDoc} */
60    @Override
61    public String toString() {
62        int bits = getIntBits();
63        return "float{0x" + Hex.u4(bits) + " / " +
64            Float.intBitsToFloat(bits) + '}';
65    }
66
67    /** {@inheritDoc} */
68    public Type getType() {
69        return Type.FLOAT;
70    }
71
72    /** {@inheritDoc} */
73    @Override
74    public String typeName() {
75        return "float";
76    }
77
78    /** {@inheritDoc} */
79    public String toHuman() {
80        return Float.toString(Float.intBitsToFloat(getIntBits()));
81    }
82
83    /**
84     * Gets the {@code float} value.
85     *
86     * @return the value
87     */
88    public float getValue() {
89        return Float.intBitsToFloat(getIntBits());
90    }
91}
92