001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1997-1999 Raja Vallee-Rai
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: /*
021: * Modified by the Sable Research Group and others 1997-1999.
022: * See the 'credits' file distributed with Soot for the complete list of
023: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
024: */
025:
026: package soot.jimple;
027:
028: import soot.*;
029: import soot.util.*;
030:
031: public class FloatConstant extends RealConstant {
032: public final float value;
033:
034: private FloatConstant(float value) {
035: this .value = value;
036: }
037:
038: public static FloatConstant v(float value) {
039: return new FloatConstant(value);
040: }
041:
042: public boolean equals(Object c) {
043: return c instanceof FloatConstant
044: && ((FloatConstant) c).value == value;
045: }
046:
047: /** Returns a hash code for this FloatConstant object. */
048: public int hashCode() {
049: return Float.floatToIntBits(value);
050: }
051:
052: // PTC 1999/06/28
053: public NumericConstant add(NumericConstant c) {
054: if (!(c instanceof FloatConstant))
055: throw new IllegalArgumentException("FloatConstant expected");
056: return FloatConstant.v(this .value + ((FloatConstant) c).value);
057: }
058:
059: public NumericConstant subtract(NumericConstant c) {
060: if (!(c instanceof FloatConstant))
061: throw new IllegalArgumentException("FloatConstant expected");
062: return FloatConstant.v(this .value - ((FloatConstant) c).value);
063: }
064:
065: public NumericConstant multiply(NumericConstant c) {
066: if (!(c instanceof FloatConstant))
067: throw new IllegalArgumentException("FloatConstant expected");
068: return FloatConstant.v(this .value * ((FloatConstant) c).value);
069: }
070:
071: public NumericConstant divide(NumericConstant c) {
072: if (!(c instanceof FloatConstant))
073: throw new IllegalArgumentException("FloatConstant expected");
074: return FloatConstant.v(this .value / ((FloatConstant) c).value);
075: }
076:
077: public NumericConstant remainder(NumericConstant c) {
078: if (!(c instanceof FloatConstant))
079: throw new IllegalArgumentException("FloatConstant expected");
080: return FloatConstant.v(this .value % ((FloatConstant) c).value);
081: }
082:
083: public NumericConstant equalEqual(NumericConstant c) {
084: if (!(c instanceof FloatConstant))
085: throw new IllegalArgumentException("FloatConstant expected");
086: return IntConstant
087: .v((this .value == ((FloatConstant) c).value) ? 1 : 0);
088: }
089:
090: public NumericConstant notEqual(NumericConstant c) {
091: if (!(c instanceof FloatConstant))
092: throw new IllegalArgumentException("FloatConstant expected");
093: return IntConstant
094: .v((this .value != ((FloatConstant) c).value) ? 1 : 0);
095: }
096:
097: public NumericConstant lessThan(NumericConstant c) {
098: if (!(c instanceof FloatConstant))
099: throw new IllegalArgumentException("FloatConstant expected");
100: return IntConstant
101: .v((this .value < ((FloatConstant) c).value) ? 1 : 0);
102: }
103:
104: public NumericConstant lessThanOrEqual(NumericConstant c) {
105: if (!(c instanceof FloatConstant))
106: throw new IllegalArgumentException("FloatConstant expected");
107: return IntConstant
108: .v((this .value <= ((FloatConstant) c).value) ? 1 : 0);
109: }
110:
111: public NumericConstant greaterThan(NumericConstant c) {
112: if (!(c instanceof FloatConstant))
113: throw new IllegalArgumentException("FloatConstant expected");
114: return IntConstant
115: .v((this .value > ((FloatConstant) c).value) ? 1 : 0);
116: }
117:
118: public NumericConstant greaterThanOrEqual(NumericConstant c) {
119: if (!(c instanceof FloatConstant))
120: throw new IllegalArgumentException("FloatConstant expected");
121: return IntConstant
122: .v((this .value >= ((FloatConstant) c).value) ? 1 : 0);
123: }
124:
125: public IntConstant cmpg(RealConstant c) {
126: if (!(c instanceof FloatConstant))
127: throw new IllegalArgumentException("FloatConstant expected");
128: float cValue = ((FloatConstant) c).value;
129: if (this .value < cValue)
130: return IntConstant.v(-1);
131: else if (this .value == cValue)
132: return IntConstant.v(0);
133: else
134: /* this or c could be NaN */
135: return IntConstant.v(1);
136: }
137:
138: public IntConstant cmpl(RealConstant c) {
139: if (!(c instanceof FloatConstant))
140: throw new IllegalArgumentException("FloatConstant expected");
141: float cValue = ((FloatConstant) c).value;
142: if (this .value > cValue)
143: return IntConstant.v(1);
144: else if (this .value == cValue)
145: return IntConstant.v(0);
146: else
147: /* this or c could be NaN */
148: return IntConstant.v(-1);
149: }
150:
151: public NumericConstant negate() {
152: return FloatConstant.v(-(this .value));
153: }
154:
155: public String toString() {
156: String floatString = new Float(value).toString();
157:
158: if (floatString.equals("NaN") || floatString.equals("Infinity")
159: || floatString.equals("-Infinity"))
160: return "#" + floatString + "F";
161: else
162: return floatString + "F";
163: }
164:
165: public Type getType() {
166: return FloatType.v();
167: }
168:
169: public void apply(Switch sw) {
170: ((ConstantSwitch) sw).caseFloatConstant(this);
171: }
172: }
|