• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "optimizer/code_generator/encode.h"
17 #include "optimizer/ir_builder/inst_builder.h"
18 #include "bytecode_instruction-inl.h"
19 
20 namespace ark::compiler {
21 /*
22     FMOV: f -> n
23     return CMP(AND(SHR(n, FP_FRACT_SIZE), FP_EXP_MASK), FP_EXP_MASK)
24 
25     1. bitcast f1
26     2. shr v1, FP_FRACT_SIZE
27     3. and v2, FP_EXP_MASK
28     4. compare v3, FP_EXP_MASK
29 
30     fraction size is 23 bits for floats and 52 bits for doubles
31     exponent mask is 0xff (8 bits) for floats and 0x7ff (11 bits) for doubles
32  */
BuildIsFiniteIntrinsic(const BytecodeInstruction * bcInst,bool accRead)33 void InstBuilder::BuildIsFiniteIntrinsic(const BytecodeInstruction *bcInst, bool accRead)
34 {
35     auto methodIndex = bcInst->GetId(0).AsIndex();
36     auto methodId = GetRuntime()->ResolveMethodIndex(GetMethod(), methodIndex);
37     auto type = GetMethodArgumentType(methodId, 0);
38     auto itype = type == DataType::FLOAT32 ? DataType::INT32 : DataType::INT64;
39     // NOLINTNEXTLINE(readability-magic-numbers)
40     auto fpFractSize = type == DataType::FLOAT32 ? 23 : 52;
41     // NOLINTNEXTLINE(readability-magic-numbers)
42     auto fpExpMask = type == DataType::FLOAT32 ? 0xff : 0x7ff;
43 
44     auto bitcast =
45         GetGraph()->CreateInstBitcast(itype, GetPc(bcInst->GetAddress()), GetArgDefinition(bcInst, 0, accRead), type);
46     auto shift =
47         GetGraph()->CreateInstShr(itype, GetPc(bcInst->GetAddress()), bitcast, FindOrCreateConstant(fpFractSize));
48     auto mask = GetGraph()->CreateInstAnd(itype, GetPc(bcInst->GetAddress()), shift, FindOrCreateConstant(fpExpMask));
49     auto cmp = GetGraph()->CreateInstCompare(DataType::BOOL, GetPc(bcInst->GetAddress()), mask,
50                                              FindOrCreateConstant(fpExpMask), itype, ConditionCode::CC_NE);
51 
52     AddInstruction(bitcast, shift, mask, cmp);
53     UpdateDefinitionAcc(cmp);
54 }
55 
BuildStdRuntimeEquals(const BytecodeInstruction * bcInst,bool accRead)56 void InstBuilder::BuildStdRuntimeEquals(const BytecodeInstruction *bcInst, bool accRead)
57 {
58     auto cmp =
59         GetGraph()->CreateInstCompare(DataType::BOOL, GetPc(bcInst->GetAddress()), GetArgDefinition(bcInst, 1, accRead),
60                                       GetArgDefinition(bcInst, 2, accRead), DataType::REFERENCE, ConditionCode::CC_EQ);
61     AddInstruction(cmp);
62     UpdateDefinitionAcc(cmp);
63 }
64 
BuildSignbitIntrinsic(const BytecodeInstruction * bcInst,bool accRead)65 void InstBuilder::BuildSignbitIntrinsic(const BytecodeInstruction *bcInst, bool accRead)
66 {
67     auto bitcast = GetGraph()->CreateInstBitcast(DataType::INT64, GetPc(bcInst->GetAddress()),
68                                                  GetArgDefinition(bcInst, 0, accRead), DataType::FLOAT64);
69     constexpr auto SHIFT = 63;
70     auto res =
71         GetGraph()->CreateInstShr(DataType::INT64, GetPc(bcInst->GetAddress()), bitcast, FindOrCreateConstant(SHIFT));
72     AddInstruction(bitcast, res);
73     UpdateDefinitionAcc(res);
74 }
75 
76 }  // namespace ark::compiler
77