1/*
2 * Copyright (C) 2015-2018 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 "B3MathExtras.h"
28
29#if ENABLE(B3_JIT)
30
31#include "B3BasicBlockInlines.h"
32#include "B3CCallValue.h"
33#include "B3Const32Value.h"
34#include "B3ConstDoubleValue.h"
35#include "B3ConstPtrValue.h"
36#include "B3UpsilonValue.h"
37#include "B3ValueInlines.h"
38#include "JSCPtrTag.h"
39#include "MathCommon.h"
40
41namespace JSC { namespace B3 {
42
43std::pair<BasicBlock*, Value*> powDoubleInt32(Procedure& procedure, BasicBlock* start, Origin origin, Value* x, Value* y)
44{
45 BasicBlock* functionCallCase = procedure.addBlock();
46 BasicBlock* loopPreHeaderCase = procedure.addBlock();
47 BasicBlock* loopTestForEvenCase = procedure.addBlock();
48 BasicBlock* loopOdd = procedure.addBlock();
49 BasicBlock* loopEvenOdd = procedure.addBlock();
50 BasicBlock* continuation = procedure.addBlock();
51
52 Value* shouldGoSlowPath = start->appendNew<Value>(procedure, Above, origin,
53 y,
54 start->appendNew<Const32Value>(procedure, origin, maxExponentForIntegerMathPow));
55 start->appendNew<Value>(procedure, Branch, origin, shouldGoSlowPath);
56 start->setSuccessors(FrequentedBlock(functionCallCase), FrequentedBlock(loopPreHeaderCase));
57
58 // Function call.
59 Value* yAsDouble = functionCallCase->appendNew<Value>(procedure, IToD, origin, y);
60 auto* powDouble = tagCFunctionPtr<double (*)(double, double)>(pow, B3CCallPtrTag);
61 Value* powResult = functionCallCase->appendNew<CCallValue>(
62 procedure, Double, origin,
63 functionCallCase->appendNew<ConstPtrValue>(procedure, origin, bitwise_cast<void*>(powDouble)),
64 x, yAsDouble);
65 UpsilonValue* powResultUpsilon = functionCallCase->appendNew<UpsilonValue>(procedure, origin, powResult);
66 functionCallCase->appendNew<Value>(procedure, Jump, origin);
67 functionCallCase->setSuccessors(FrequentedBlock(continuation));
68
69 // Loop pre-header.
70 Value* initialResult = loopPreHeaderCase->appendNew<ConstDoubleValue>(procedure, origin, 1.);
71 UpsilonValue* initialLoopValue = loopPreHeaderCase->appendNew<UpsilonValue>(procedure, origin, initialResult);
72 UpsilonValue* initialResultValue = loopPreHeaderCase->appendNew<UpsilonValue>(procedure, origin, initialResult);
73 UpsilonValue* initialSquaredInput = loopPreHeaderCase->appendNew<UpsilonValue>(procedure, origin, x);
74 UpsilonValue* initialLoopCounter = loopPreHeaderCase->appendNew<UpsilonValue>(procedure, origin, y);
75 loopPreHeaderCase->appendNew<Value>(procedure, Jump, origin);
76 loopPreHeaderCase->setSuccessors(FrequentedBlock(loopTestForEvenCase));
77
78 // Test if what is left of the counter is even.
79 Value* inLoopCounter = loopTestForEvenCase->appendNew<Value>(procedure, Phi, Int32, origin);
80 Value* inLoopSquaredInput = loopTestForEvenCase->appendNew<Value>(procedure, Phi, Double, origin);
81 Value* lastCounterBit = loopTestForEvenCase->appendNew<Value>(procedure, BitAnd, origin,
82 inLoopCounter,
83 loopTestForEvenCase->appendNew<Const32Value>(procedure, origin, 1));
84 loopTestForEvenCase->appendNew<Value>(procedure, Branch, origin, lastCounterBit);
85 loopTestForEvenCase->setSuccessors(FrequentedBlock(loopOdd), FrequentedBlock(loopEvenOdd));
86
87 // Counter is odd.
88 Value* inLoopResult = loopOdd->appendNew<Value>(procedure, Phi, Double, origin);
89 Value* updatedResult = loopOdd->appendNew<Value>(procedure, Mul, origin, inLoopResult, inLoopSquaredInput);
90 UpsilonValue* updatedLoopResultUpsilon = loopOdd->appendNew<UpsilonValue>(procedure, origin, updatedResult);
91 initialLoopValue->setPhi(inLoopResult);
92 updatedLoopResultUpsilon->setPhi(inLoopResult);
93 UpsilonValue* updatedLoopResult = loopOdd->appendNew<UpsilonValue>(procedure, origin, updatedResult);
94
95 loopOdd->appendNew<Value>(procedure, Jump, origin);
96 loopOdd->setSuccessors(FrequentedBlock(loopEvenOdd));
97
98 // Even value and following the Odd.
99 Value* squaredInput = loopEvenOdd->appendNew<Value>(procedure, Mul, origin, inLoopSquaredInput, inLoopSquaredInput);
100 UpsilonValue* squaredInputUpsilon = loopEvenOdd->appendNew<UpsilonValue>(procedure, origin, squaredInput);
101 initialSquaredInput->setPhi(inLoopSquaredInput);
102 squaredInputUpsilon->setPhi(inLoopSquaredInput);
103
104 Value* updatedCounter = loopEvenOdd->appendNew<Value>(procedure, ZShr, origin,
105 inLoopCounter,
106 loopEvenOdd->appendNew<Const32Value>(procedure, origin, 1));
107 UpsilonValue* updatedCounterUpsilon = loopEvenOdd->appendNew<UpsilonValue>(procedure, origin, updatedCounter);
108 initialLoopCounter->setPhi(inLoopCounter);
109 updatedCounterUpsilon->setPhi(inLoopCounter);
110
111 loopEvenOdd->appendNew<Value>(procedure, Branch, origin, updatedCounter);
112 loopEvenOdd->setSuccessors(FrequentedBlock(loopTestForEvenCase), FrequentedBlock(continuation));
113
114 // Inline loop.
115 Value* finalResultPhi = continuation->appendNew<Value>(procedure, Phi, Double, origin);
116 powResultUpsilon->setPhi(finalResultPhi);
117 initialResultValue->setPhi(finalResultPhi);
118 updatedLoopResult->setPhi(finalResultPhi);
119 return std::make_pair(continuation, finalResultPhi);
120}
121
122} } // namespace JSC::B3
123
124#endif // ENABLE(B3_JIT)
125
126