KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > FloatConstant


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.jimple;
28
29 import soot.*;
30 import soot.util.*;
31 import java.util.*;
32
33 public class FloatConstant extends RealConstant
34 {
35     public final float value;
36
37     private FloatConstant(float value)
38     {
39         this.value = value;
40     }
41
42     public static FloatConstant v(float value)
43     {
44         return new FloatConstant(value);
45     }
46
47     public boolean equals(Object JavaDoc c)
48     {
49         return c instanceof FloatConstant && ((FloatConstant) c).value == value;
50     }
51
52     /** Returns a hash code for this FloatConstant object. */
53     public int hashCode()
54     {
55         return Float.floatToIntBits(value);
56     }
57
58     // PTC 1999/06/28
59
public NumericConstant add(NumericConstant c)
60     {
61         if (!(c instanceof FloatConstant))
62             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
63         return FloatConstant.v(this.value + ((FloatConstant)c).value);
64     }
65
66     public NumericConstant subtract(NumericConstant c)
67     {
68         if (!(c instanceof FloatConstant))
69             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
70         return FloatConstant.v(this.value - ((FloatConstant)c).value);
71     }
72
73     public NumericConstant multiply(NumericConstant c)
74     {
75         if (!(c instanceof FloatConstant))
76             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
77         return FloatConstant.v(this.value * ((FloatConstant)c).value);
78     }
79
80     public NumericConstant divide(NumericConstant c)
81     {
82         if (!(c instanceof FloatConstant))
83             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
84         return FloatConstant.v(this.value / ((FloatConstant)c).value);
85     }
86
87     public NumericConstant remainder(NumericConstant c)
88     {
89         if (!(c instanceof FloatConstant))
90             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
91         return FloatConstant.v(this.value % ((FloatConstant)c).value);
92     }
93
94     public NumericConstant equalEqual(NumericConstant c)
95     {
96         if (!(c instanceof FloatConstant))
97             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
98         return IntConstant.v((this.value == ((FloatConstant)c).value) ? 1 : 0);
99     }
100
101     public NumericConstant notEqual(NumericConstant c)
102     {
103         if (!(c instanceof FloatConstant))
104             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
105         return IntConstant.v((this.value != ((FloatConstant)c).value) ? 1 : 0);
106     }
107
108     public NumericConstant lessThan(NumericConstant c)
109     {
110         if (!(c instanceof FloatConstant))
111             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
112         return IntConstant.v((this.value < ((FloatConstant)c).value) ? 1 : 0);
113     }
114
115     public NumericConstant lessThanOrEqual(NumericConstant c)
116     {
117         if (!(c instanceof FloatConstant))
118             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
119         return IntConstant.v((this.value <= ((FloatConstant)c).value) ? 1 : 0);
120     }
121
122     public NumericConstant greaterThan(NumericConstant c)
123     {
124         if (!(c instanceof FloatConstant))
125             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
126         return IntConstant.v((this.value > ((FloatConstant)c).value) ? 1 : 0);
127     }
128
129     public NumericConstant greaterThanOrEqual(NumericConstant c)
130     {
131         if (!(c instanceof FloatConstant))
132             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
133         return IntConstant.v((this.value >= ((FloatConstant)c).value) ? 1 : 0);
134     }
135
136     public IntConstant cmpg(RealConstant c) {
137         if (!(c instanceof FloatConstant))
138             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
139         float cValue = ((FloatConstant) c).value;
140         if (this.value < cValue)
141             return IntConstant.v(-1);
142         else if (this.value == cValue)
143             return IntConstant.v(0);
144         else /* this or c could be NaN */
145             return IntConstant.v(1);
146     }
147     
148     public IntConstant cmpl(RealConstant c) {
149         if (!(c instanceof FloatConstant))
150             throw new IllegalArgumentException JavaDoc("FloatConstant expected");
151         float cValue = ((FloatConstant) c).value;
152         if (this.value > cValue)
153             return IntConstant.v(1);
154         else if (this.value == cValue)
155             return IntConstant.v(0);
156         else /* this or c could be NaN */
157             return IntConstant.v(-1);
158     }
159     
160     public NumericConstant negate()
161     {
162         return FloatConstant.v(-(this.value));
163     }
164
165     public String JavaDoc toString()
166     {
167         String JavaDoc floatString = new Float JavaDoc(value).toString();
168         
169         if(floatString.equals("NaN") ||
170             floatString.equals("Infinity") ||
171             floatString.equals("-Infinity"))
172             return "#" + floatString + "F";
173         else
174             return floatString + "F";
175     }
176
177     public Type getType()
178     {
179         return FloatType.v();
180     }
181
182     public void apply(Switch sw)
183     {
184         ((ConstantSwitch) sw).caseFloatConstant(this);
185     }
186 }
187
Popular Tags