1/*
2 * Copyright (C) 2015-2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "B3ConstFloatValue.h"
28
29#if ENABLE(B3_JIT)
30
31#include "B3ConstDoubleValue.h"
32#include "B3ProcedureInlines.h"
33#include "B3ValueInlines.h"
34
35namespace JSC { namespace B3 {
36
37ConstFloatValue::~ConstFloatValue()
38{
39}
40
41Value* ConstFloatValue::negConstant(Procedure& proc) const
42{
43 return proc.add<ConstFloatValue>(origin(), -m_value);
44}
45
46Value* ConstFloatValue::addConstant(Procedure& proc, int32_t other) const
47{
48 return proc.add<ConstFloatValue>(origin(), m_value + static_cast<float>(other));
49}
50
51Value* ConstFloatValue::addConstant(Procedure& proc, const Value* other) const
52{
53 if (!other->hasFloat())
54 return nullptr;
55 return proc.add<ConstFloatValue>(origin(), m_value + other->asFloat());
56}
57
58Value* ConstFloatValue::subConstant(Procedure& proc, const Value* other) const
59{
60 if (!other->hasFloat())
61 return nullptr;
62 return proc.add<ConstFloatValue>(origin(), m_value - other->asFloat());
63}
64
65Value* ConstFloatValue::mulConstant(Procedure& proc, const Value* other) const
66{
67 if (!other->hasFloat())
68 return nullptr;
69 return proc.add<ConstFloatValue>(origin(), m_value * other->asFloat());
70}
71
72Value* ConstFloatValue::bitAndConstant(Procedure& proc, const Value* other) const
73{
74 if (!other->hasFloat())
75 return nullptr;
76 float result = bitwise_cast<float>(bitwise_cast<uint32_t>(m_value) & bitwise_cast<uint32_t>(other->asFloat()));
77 return proc.add<ConstFloatValue>(origin(), result);
78}
79
80Value* ConstFloatValue::bitOrConstant(Procedure& proc, const Value* other) const
81{
82 if (!other->hasFloat())
83 return nullptr;
84 float result = bitwise_cast<float>(bitwise_cast<uint32_t>(m_value) | bitwise_cast<uint32_t>(other->asFloat()));
85 return proc.add<ConstFloatValue>(origin(), result);
86}
87
88Value* ConstFloatValue::bitXorConstant(Procedure& proc, const Value* other) const
89{
90 if (!other->hasFloat())
91 return nullptr;
92 float result = bitwise_cast<float>(bitwise_cast<uint32_t>(m_value) ^ bitwise_cast<uint32_t>(other->asFloat()));
93 return proc.add<ConstFloatValue>(origin(), result);
94}
95
96Value* ConstFloatValue::bitwiseCastConstant(Procedure& proc) const
97{
98 return proc.add<Const32Value>(origin(), bitwise_cast<int32_t>(m_value));
99}
100
101Value* ConstFloatValue::floatToDoubleConstant(Procedure& proc) const
102{
103 return proc.add<ConstDoubleValue>(origin(), static_cast<double>(m_value));
104}
105
106Value* ConstFloatValue::absConstant(Procedure& proc) const
107{
108 return proc.add<ConstFloatValue>(origin(), static_cast<float>(fabs(m_value)));
109}
110
111Value* ConstFloatValue::ceilConstant(Procedure& proc) const
112{
113 return proc.add<ConstFloatValue>(origin(), ceilf(m_value));
114}
115
116Value* ConstFloatValue::floorConstant(Procedure& proc) const
117{
118 return proc.add<ConstFloatValue>(origin(), floorf(m_value));
119}
120
121Value* ConstFloatValue::sqrtConstant(Procedure& proc) const
122{
123 return proc.add<ConstFloatValue>(origin(), static_cast<float>(sqrt(m_value)));
124}
125
126Value* ConstFloatValue::divConstant(Procedure& proc, const Value* other) const
127{
128 if (!other->hasFloat())
129 return nullptr;
130 return proc.add<ConstFloatValue>(origin(), m_value / other->asFloat());
131}
132
133TriState ConstFloatValue::equalConstant(const Value* other) const
134{
135 if (!other->hasFloat())
136 return MixedTriState;
137 return triState(m_value == other->asFloat());
138}
139
140TriState ConstFloatValue::notEqualConstant(const Value* other) const
141{
142 if (!other->hasFloat())
143 return MixedTriState;
144 return triState(m_value != other->asFloat());
145}
146
147TriState ConstFloatValue::lessThanConstant(const Value* other) const
148{
149 if (!other->hasFloat())
150 return MixedTriState;
151 return triState(m_value < other->asFloat());
152}
153
154TriState ConstFloatValue::greaterThanConstant(const Value* other) const
155{
156 if (!other->hasFloat())
157 return MixedTriState;
158 return triState(m_value > other->asFloat());
159}
160
161TriState ConstFloatValue::lessEqualConstant(const Value* other) const
162{
163 if (!other->hasFloat())
164 return MixedTriState;
165 return triState(m_value <= other->asFloat());
166}
167
168TriState ConstFloatValue::greaterEqualConstant(const Value* other) const
169{
170 if (!other->hasFloat())
171 return MixedTriState;
172 return triState(m_value >= other->asFloat());
173}
174
175TriState ConstFloatValue::equalOrUnorderedConstant(const Value* other) const
176{
177 if (std::isnan(m_value))
178 return TrueTriState;
179
180 if (!other->hasFloat())
181 return MixedTriState;
182 float otherValue = other->asFloat();
183 return triState(std::isunordered(m_value, otherValue) || m_value == otherValue);
184}
185
186void ConstFloatValue::dumpMeta(CommaPrinter& comma, PrintStream& out) const
187{
188 out.print(comma);
189 out.printf("%le", m_value);
190}
191
192} } // namespace JSC::B3
193
194#endif // ENABLE(B3_JIT)
195