File: | llvm/include/llvm/Support/MathExtras.h |
Warning: | line 788, column 20 The result of the left shift is undefined due to shifting by '64', which is greater or equal to the width of type 'uint64_t' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //=== AArch64PostLegalizerCombiner.cpp --------------------------*- C++ -*-===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | /// | |||
9 | /// \file | |||
10 | /// Post-legalization combines on generic MachineInstrs. | |||
11 | /// | |||
12 | /// The combines here must preserve instruction legality. | |||
13 | /// | |||
14 | /// Lowering combines (e.g. pseudo matching) should be handled by | |||
15 | /// AArch64PostLegalizerLowering. | |||
16 | /// | |||
17 | /// Combines which don't rely on instruction legality should go in the | |||
18 | /// AArch64PreLegalizerCombiner. | |||
19 | /// | |||
20 | //===----------------------------------------------------------------------===// | |||
21 | ||||
22 | #include "AArch64TargetMachine.h" | |||
23 | #include "llvm/CodeGen/GlobalISel/Combiner.h" | |||
24 | #include "llvm/CodeGen/GlobalISel/CombinerHelper.h" | |||
25 | #include "llvm/CodeGen/GlobalISel/CombinerInfo.h" | |||
26 | #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h" | |||
27 | #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h" | |||
28 | #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h" | |||
29 | #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" | |||
30 | #include "llvm/CodeGen/GlobalISel/Utils.h" | |||
31 | #include "llvm/CodeGen/MachineDominators.h" | |||
32 | #include "llvm/CodeGen/MachineFunctionPass.h" | |||
33 | #include "llvm/CodeGen/MachineRegisterInfo.h" | |||
34 | #include "llvm/CodeGen/TargetOpcodes.h" | |||
35 | #include "llvm/CodeGen/TargetPassConfig.h" | |||
36 | #include "llvm/Support/Debug.h" | |||
37 | ||||
38 | #define DEBUG_TYPE"aarch64-postlegalizer-combiner" "aarch64-postlegalizer-combiner" | |||
39 | ||||
40 | using namespace llvm; | |||
41 | using namespace MIPatternMatch; | |||
42 | ||||
43 | /// This combine tries do what performExtractVectorEltCombine does in SDAG. | |||
44 | /// Rewrite for pairwise fadd pattern | |||
45 | /// (s32 (g_extract_vector_elt | |||
46 | /// (g_fadd (vXs32 Other) | |||
47 | /// (g_vector_shuffle (vXs32 Other) undef <1,X,...> )) 0)) | |||
48 | /// -> | |||
49 | /// (s32 (g_fadd (g_extract_vector_elt (vXs32 Other) 0) | |||
50 | /// (g_extract_vector_elt (vXs32 Other) 1)) | |||
51 | bool matchExtractVecEltPairwiseAdd( | |||
52 | MachineInstr &MI, MachineRegisterInfo &MRI, | |||
53 | std::tuple<unsigned, LLT, Register> &MatchInfo) { | |||
54 | Register Src1 = MI.getOperand(1).getReg(); | |||
55 | Register Src2 = MI.getOperand(2).getReg(); | |||
56 | LLT DstTy = MRI.getType(MI.getOperand(0).getReg()); | |||
57 | ||||
58 | auto Cst = getConstantVRegValWithLookThrough(Src2, MRI); | |||
59 | if (!Cst || Cst->Value != 0) | |||
60 | return false; | |||
61 | // SDAG also checks for FullFP16, but this looks to be beneficial anyway. | |||
62 | ||||
63 | // Now check for an fadd operation. TODO: expand this for integer add? | |||
64 | auto *FAddMI = getOpcodeDef(TargetOpcode::G_FADD, Src1, MRI); | |||
65 | if (!FAddMI) | |||
66 | return false; | |||
67 | ||||
68 | // If we add support for integer add, must restrict these types to just s64. | |||
69 | unsigned DstSize = DstTy.getSizeInBits(); | |||
70 | if (DstSize != 16 && DstSize != 32 && DstSize != 64) | |||
71 | return false; | |||
72 | ||||
73 | Register Src1Op1 = FAddMI->getOperand(1).getReg(); | |||
74 | Register Src1Op2 = FAddMI->getOperand(2).getReg(); | |||
75 | MachineInstr *Shuffle = | |||
76 | getOpcodeDef(TargetOpcode::G_SHUFFLE_VECTOR, Src1Op2, MRI); | |||
77 | MachineInstr *Other = MRI.getVRegDef(Src1Op1); | |||
78 | if (!Shuffle) { | |||
79 | Shuffle = getOpcodeDef(TargetOpcode::G_SHUFFLE_VECTOR, Src1Op1, MRI); | |||
80 | Other = MRI.getVRegDef(Src1Op2); | |||
81 | } | |||
82 | ||||
83 | // We're looking for a shuffle that moves the second element to index 0. | |||
84 | if (Shuffle && Shuffle->getOperand(3).getShuffleMask()[0] == 1 && | |||
85 | Other == MRI.getVRegDef(Shuffle->getOperand(1).getReg())) { | |||
86 | std::get<0>(MatchInfo) = TargetOpcode::G_FADD; | |||
87 | std::get<1>(MatchInfo) = DstTy; | |||
88 | std::get<2>(MatchInfo) = Other->getOperand(0).getReg(); | |||
89 | return true; | |||
90 | } | |||
91 | return false; | |||
92 | } | |||
93 | ||||
94 | bool applyExtractVecEltPairwiseAdd( | |||
95 | MachineInstr &MI, MachineRegisterInfo &MRI, MachineIRBuilder &B, | |||
96 | std::tuple<unsigned, LLT, Register> &MatchInfo) { | |||
97 | unsigned Opc = std::get<0>(MatchInfo); | |||
98 | assert(Opc == TargetOpcode::G_FADD && "Unexpected opcode!")(static_cast<void> (0)); | |||
99 | // We want to generate two extracts of elements 0 and 1, and add them. | |||
100 | LLT Ty = std::get<1>(MatchInfo); | |||
101 | Register Src = std::get<2>(MatchInfo); | |||
102 | LLT s64 = LLT::scalar(64); | |||
103 | B.setInstrAndDebugLoc(MI); | |||
104 | auto Elt0 = B.buildExtractVectorElement(Ty, Src, B.buildConstant(s64, 0)); | |||
105 | auto Elt1 = B.buildExtractVectorElement(Ty, Src, B.buildConstant(s64, 1)); | |||
106 | B.buildInstr(Opc, {MI.getOperand(0).getReg()}, {Elt0, Elt1}); | |||
107 | MI.eraseFromParent(); | |||
108 | return true; | |||
109 | } | |||
110 | ||||
111 | static bool isSignExtended(Register R, MachineRegisterInfo &MRI) { | |||
112 | // TODO: check if extended build vector as well. | |||
113 | unsigned Opc = MRI.getVRegDef(R)->getOpcode(); | |||
114 | return Opc == TargetOpcode::G_SEXT || Opc == TargetOpcode::G_SEXT_INREG; | |||
115 | } | |||
116 | ||||
117 | static bool isZeroExtended(Register R, MachineRegisterInfo &MRI) { | |||
118 | // TODO: check if extended build vector as well. | |||
119 | return MRI.getVRegDef(R)->getOpcode() == TargetOpcode::G_ZEXT; | |||
120 | } | |||
121 | ||||
122 | bool matchAArch64MulConstCombine( | |||
123 | MachineInstr &MI, MachineRegisterInfo &MRI, | |||
124 | std::function<void(MachineIRBuilder &B, Register DstReg)> &ApplyFn) { | |||
125 | assert(MI.getOpcode() == TargetOpcode::G_MUL)(static_cast<void> (0)); | |||
126 | Register LHS = MI.getOperand(1).getReg(); | |||
127 | Register RHS = MI.getOperand(2).getReg(); | |||
128 | Register Dst = MI.getOperand(0).getReg(); | |||
129 | const LLT Ty = MRI.getType(LHS); | |||
130 | ||||
131 | // The below optimizations require a constant RHS. | |||
132 | auto Const = getConstantVRegValWithLookThrough(RHS, MRI); | |||
133 | if (!Const) | |||
| ||||
134 | return false; | |||
135 | ||||
136 | const APInt ConstValue = Const->Value.sextOrSelf(Ty.getSizeInBits()); | |||
137 | // The following code is ported from AArch64ISelLowering. | |||
138 | // Multiplication of a power of two plus/minus one can be done more | |||
139 | // cheaply as as shift+add/sub. For now, this is true unilaterally. If | |||
140 | // future CPUs have a cheaper MADD instruction, this may need to be | |||
141 | // gated on a subtarget feature. For Cyclone, 32-bit MADD is 4 cycles and | |||
142 | // 64-bit is 5 cycles, so this is always a win. | |||
143 | // More aggressively, some multiplications N0 * C can be lowered to | |||
144 | // shift+add+shift if the constant C = A * B where A = 2^N + 1 and B = 2^M, | |||
145 | // e.g. 6=3*2=(2+1)*2. | |||
146 | // TODO: consider lowering more cases, e.g. C = 14, -6, -14 or even 45 | |||
147 | // which equals to (1+2)*16-(1+2). | |||
148 | // TrailingZeroes is used to test if the mul can be lowered to | |||
149 | // shift+add+shift. | |||
150 | unsigned TrailingZeroes = ConstValue.countTrailingZeros(); | |||
151 | if (TrailingZeroes) { | |||
152 | // Conservatively do not lower to shift+add+shift if the mul might be | |||
153 | // folded into smul or umul. | |||
154 | if (MRI.hasOneNonDBGUse(LHS) && | |||
155 | (isSignExtended(LHS, MRI) || isZeroExtended(LHS, MRI))) | |||
156 | return false; | |||
157 | // Conservatively do not lower to shift+add+shift if the mul might be | |||
158 | // folded into madd or msub. | |||
159 | if (MRI.hasOneNonDBGUse(Dst)) { | |||
160 | MachineInstr &UseMI = *MRI.use_instr_begin(Dst); | |||
161 | unsigned UseOpc = UseMI.getOpcode(); | |||
162 | if (UseOpc == TargetOpcode::G_ADD || UseOpc == TargetOpcode::G_PTR_ADD || | |||
163 | UseOpc == TargetOpcode::G_SUB) | |||
164 | return false; | |||
165 | } | |||
166 | } | |||
167 | // Use ShiftedConstValue instead of ConstValue to support both shift+add/sub | |||
168 | // and shift+add+shift. | |||
169 | APInt ShiftedConstValue = ConstValue.ashr(TrailingZeroes); | |||
170 | ||||
171 | unsigned ShiftAmt, AddSubOpc; | |||
172 | // Is the shifted value the LHS operand of the add/sub? | |||
173 | bool ShiftValUseIsLHS = true; | |||
174 | // Do we need to negate the result? | |||
175 | bool NegateResult = false; | |||
176 | ||||
177 | if (ConstValue.isNonNegative()) { | |||
178 | // (mul x, 2^N + 1) => (add (shl x, N), x) | |||
179 | // (mul x, 2^N - 1) => (sub (shl x, N), x) | |||
180 | // (mul x, (2^N + 1) * 2^M) => (shl (add (shl x, N), x), M) | |||
181 | APInt SCVMinus1 = ShiftedConstValue - 1; | |||
182 | APInt CVPlus1 = ConstValue + 1; | |||
183 | if (SCVMinus1.isPowerOf2()) { | |||
184 | ShiftAmt = SCVMinus1.logBase2(); | |||
185 | AddSubOpc = TargetOpcode::G_ADD; | |||
186 | } else if (CVPlus1.isPowerOf2()) { | |||
187 | ShiftAmt = CVPlus1.logBase2(); | |||
188 | AddSubOpc = TargetOpcode::G_SUB; | |||
189 | } else | |||
190 | return false; | |||
191 | } else { | |||
192 | // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) | |||
193 | // (mul x, -(2^N + 1)) => - (add (shl x, N), x) | |||
194 | APInt CVNegPlus1 = -ConstValue + 1; | |||
195 | APInt CVNegMinus1 = -ConstValue - 1; | |||
196 | if (CVNegPlus1.isPowerOf2()) { | |||
197 | ShiftAmt = CVNegPlus1.logBase2(); | |||
198 | AddSubOpc = TargetOpcode::G_SUB; | |||
199 | ShiftValUseIsLHS = false; | |||
200 | } else if (CVNegMinus1.isPowerOf2()) { | |||
201 | ShiftAmt = CVNegMinus1.logBase2(); | |||
202 | AddSubOpc = TargetOpcode::G_ADD; | |||
203 | NegateResult = true; | |||
204 | } else | |||
205 | return false; | |||
206 | } | |||
207 | ||||
208 | if (NegateResult && TrailingZeroes) | |||
209 | return false; | |||
210 | ||||
211 | ApplyFn = [=](MachineIRBuilder &B, Register DstReg) { | |||
212 | auto Shift = B.buildConstant(LLT::scalar(64), ShiftAmt); | |||
213 | auto ShiftedVal = B.buildShl(Ty, LHS, Shift); | |||
214 | ||||
215 | Register AddSubLHS = ShiftValUseIsLHS ? ShiftedVal.getReg(0) : LHS; | |||
216 | Register AddSubRHS = ShiftValUseIsLHS ? LHS : ShiftedVal.getReg(0); | |||
217 | auto Res = B.buildInstr(AddSubOpc, {Ty}, {AddSubLHS, AddSubRHS}); | |||
218 | assert(!(NegateResult && TrailingZeroes) &&(static_cast<void> (0)) | |||
219 | "NegateResult and TrailingZeroes cannot both be true for now.")(static_cast<void> (0)); | |||
220 | // Negate the result. | |||
221 | if (NegateResult) { | |||
222 | B.buildSub(DstReg, B.buildConstant(Ty, 0), Res); | |||
223 | return; | |||
224 | } | |||
225 | // Shift the result. | |||
226 | if (TrailingZeroes) { | |||
227 | B.buildShl(DstReg, Res, B.buildConstant(LLT::scalar(64), TrailingZeroes)); | |||
228 | return; | |||
229 | } | |||
230 | B.buildCopy(DstReg, Res.getReg(0)); | |||
231 | }; | |||
232 | return true; | |||
233 | } | |||
234 | ||||
235 | bool applyAArch64MulConstCombine( | |||
236 | MachineInstr &MI, MachineRegisterInfo &MRI, MachineIRBuilder &B, | |||
237 | std::function<void(MachineIRBuilder &B, Register DstReg)> &ApplyFn) { | |||
238 | B.setInstrAndDebugLoc(MI); | |||
239 | ApplyFn(B, MI.getOperand(0).getReg()); | |||
240 | MI.eraseFromParent(); | |||
241 | return true; | |||
242 | } | |||
243 | ||||
244 | /// Try to fold a G_MERGE_VALUES of 2 s32 sources, where the second source | |||
245 | /// is a zero, into a G_ZEXT of the first. | |||
246 | bool matchFoldMergeToZext(MachineInstr &MI, MachineRegisterInfo &MRI) { | |||
247 | auto &Merge = cast<GMerge>(MI); | |||
248 | LLT SrcTy = MRI.getType(Merge.getSourceReg(0)); | |||
249 | if (SrcTy != LLT::scalar(32) || Merge.getNumSources() != 2) | |||
250 | return false; | |||
251 | return mi_match(Merge.getSourceReg(1), MRI, m_SpecificICst(0)); | |||
252 | } | |||
253 | ||||
254 | void applyFoldMergeToZext(MachineInstr &MI, MachineRegisterInfo &MRI, | |||
255 | MachineIRBuilder &B, GISelChangeObserver &Observer) { | |||
256 | // Mutate %d(s64) = G_MERGE_VALUES %a(s32), 0(s32) | |||
257 | // -> | |||
258 | // %d(s64) = G_ZEXT %a(s32) | |||
259 | Observer.changingInstr(MI); | |||
260 | MI.setDesc(B.getTII().get(TargetOpcode::G_ZEXT)); | |||
261 | MI.RemoveOperand(2); | |||
262 | Observer.changedInstr(MI); | |||
263 | } | |||
264 | ||||
265 | #define AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_DEPS | |||
266 | #include "AArch64GenPostLegalizeGICombiner.inc" | |||
267 | #undef AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_DEPS | |||
268 | ||||
269 | namespace { | |||
270 | #define AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_H | |||
271 | #include "AArch64GenPostLegalizeGICombiner.inc" | |||
272 | #undef AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_H | |||
273 | ||||
274 | class AArch64PostLegalizerCombinerInfo : public CombinerInfo { | |||
275 | GISelKnownBits *KB; | |||
276 | MachineDominatorTree *MDT; | |||
277 | ||||
278 | public: | |||
279 | AArch64GenPostLegalizerCombinerHelperRuleConfig GeneratedRuleCfg; | |||
280 | ||||
281 | AArch64PostLegalizerCombinerInfo(bool EnableOpt, bool OptSize, bool MinSize, | |||
282 | GISelKnownBits *KB, | |||
283 | MachineDominatorTree *MDT) | |||
284 | : CombinerInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false, | |||
285 | /*LegalizerInfo*/ nullptr, EnableOpt, OptSize, MinSize), | |||
286 | KB(KB), MDT(MDT) { | |||
287 | if (!GeneratedRuleCfg.parseCommandLineOption()) | |||
288 | report_fatal_error("Invalid rule identifier"); | |||
289 | } | |||
290 | ||||
291 | virtual bool combine(GISelChangeObserver &Observer, MachineInstr &MI, | |||
292 | MachineIRBuilder &B) const override; | |||
293 | }; | |||
294 | ||||
295 | bool AArch64PostLegalizerCombinerInfo::combine(GISelChangeObserver &Observer, | |||
296 | MachineInstr &MI, | |||
297 | MachineIRBuilder &B) const { | |||
298 | const auto *LI = | |||
299 | MI.getParent()->getParent()->getSubtarget().getLegalizerInfo(); | |||
300 | CombinerHelper Helper(Observer, B, KB, MDT, LI); | |||
301 | AArch64GenPostLegalizerCombinerHelper Generated(GeneratedRuleCfg); | |||
302 | return Generated.tryCombineAll(Observer, MI, B, Helper); | |||
303 | } | |||
304 | ||||
305 | #define AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_CPP | |||
306 | #include "AArch64GenPostLegalizeGICombiner.inc" | |||
307 | #undef AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_CPP | |||
308 | ||||
309 | class AArch64PostLegalizerCombiner : public MachineFunctionPass { | |||
310 | public: | |||
311 | static char ID; | |||
312 | ||||
313 | AArch64PostLegalizerCombiner(bool IsOptNone = false); | |||
314 | ||||
315 | StringRef getPassName() const override { | |||
316 | return "AArch64PostLegalizerCombiner"; | |||
317 | } | |||
318 | ||||
319 | bool runOnMachineFunction(MachineFunction &MF) override; | |||
320 | void getAnalysisUsage(AnalysisUsage &AU) const override; | |||
321 | ||||
322 | private: | |||
323 | bool IsOptNone; | |||
324 | }; | |||
325 | } // end anonymous namespace | |||
326 | ||||
327 | void AArch64PostLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const { | |||
328 | AU.addRequired<TargetPassConfig>(); | |||
329 | AU.setPreservesCFG(); | |||
330 | getSelectionDAGFallbackAnalysisUsage(AU); | |||
331 | AU.addRequired<GISelKnownBitsAnalysis>(); | |||
332 | AU.addPreserved<GISelKnownBitsAnalysis>(); | |||
333 | if (!IsOptNone) { | |||
334 | AU.addRequired<MachineDominatorTree>(); | |||
335 | AU.addPreserved<MachineDominatorTree>(); | |||
336 | AU.addRequired<GISelCSEAnalysisWrapperPass>(); | |||
337 | AU.addPreserved<GISelCSEAnalysisWrapperPass>(); | |||
338 | } | |||
339 | MachineFunctionPass::getAnalysisUsage(AU); | |||
340 | } | |||
341 | ||||
342 | AArch64PostLegalizerCombiner::AArch64PostLegalizerCombiner(bool IsOptNone) | |||
343 | : MachineFunctionPass(ID), IsOptNone(IsOptNone) { | |||
344 | initializeAArch64PostLegalizerCombinerPass(*PassRegistry::getPassRegistry()); | |||
345 | } | |||
346 | ||||
347 | bool AArch64PostLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) { | |||
348 | if (MF.getProperties().hasProperty( | |||
349 | MachineFunctionProperties::Property::FailedISel)) | |||
350 | return false; | |||
351 | assert(MF.getProperties().hasProperty((static_cast<void> (0)) | |||
352 | MachineFunctionProperties::Property::Legalized) &&(static_cast<void> (0)) | |||
353 | "Expected a legalized function?")(static_cast<void> (0)); | |||
354 | auto *TPC = &getAnalysis<TargetPassConfig>(); | |||
355 | const Function &F = MF.getFunction(); | |||
356 | bool EnableOpt = | |||
357 | MF.getTarget().getOptLevel() != CodeGenOpt::None && !skipFunction(F); | |||
358 | GISelKnownBits *KB = &getAnalysis<GISelKnownBitsAnalysis>().get(MF); | |||
359 | MachineDominatorTree *MDT = | |||
360 | IsOptNone ? nullptr : &getAnalysis<MachineDominatorTree>(); | |||
361 | AArch64PostLegalizerCombinerInfo PCInfo(EnableOpt, F.hasOptSize(), | |||
362 | F.hasMinSize(), KB, MDT); | |||
363 | GISelCSEAnalysisWrapper &Wrapper = | |||
364 | getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper(); | |||
365 | auto *CSEInfo = &Wrapper.get(TPC->getCSEConfig()); | |||
366 | Combiner C(PCInfo, TPC); | |||
367 | return C.combineMachineInstrs(MF, CSEInfo); | |||
368 | } | |||
369 | ||||
370 | char AArch64PostLegalizerCombiner::ID = 0; | |||
371 | INITIALIZE_PASS_BEGIN(AArch64PostLegalizerCombiner, DEBUG_TYPE,static void *initializeAArch64PostLegalizerCombinerPassOnce(PassRegistry &Registry) { | |||
372 | "Combine AArch64 MachineInstrs after legalization", false,static void *initializeAArch64PostLegalizerCombinerPassOnce(PassRegistry &Registry) { | |||
373 | false)static void *initializeAArch64PostLegalizerCombinerPassOnce(PassRegistry &Registry) { | |||
374 | INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)initializeTargetPassConfigPass(Registry); | |||
375 | INITIALIZE_PASS_DEPENDENCY(GISelKnownBitsAnalysis)initializeGISelKnownBitsAnalysisPass(Registry); | |||
376 | INITIALIZE_PASS_END(AArch64PostLegalizerCombiner, DEBUG_TYPE,PassInfo *PI = new PassInfo( "Combine AArch64 MachineInstrs after legalization" , "aarch64-postlegalizer-combiner", &AArch64PostLegalizerCombiner ::ID, PassInfo::NormalCtor_t(callDefaultCtor<AArch64PostLegalizerCombiner >), false, false); Registry.registerPass(*PI, true); return PI; } static llvm::once_flag InitializeAArch64PostLegalizerCombinerPassFlag ; void llvm::initializeAArch64PostLegalizerCombinerPass(PassRegistry &Registry) { llvm::call_once(InitializeAArch64PostLegalizerCombinerPassFlag , initializeAArch64PostLegalizerCombinerPassOnce, std::ref(Registry )); } | |||
377 | "Combine AArch64 MachineInstrs after legalization", false,PassInfo *PI = new PassInfo( "Combine AArch64 MachineInstrs after legalization" , "aarch64-postlegalizer-combiner", &AArch64PostLegalizerCombiner ::ID, PassInfo::NormalCtor_t(callDefaultCtor<AArch64PostLegalizerCombiner >), false, false); Registry.registerPass(*PI, true); return PI; } static llvm::once_flag InitializeAArch64PostLegalizerCombinerPassFlag ; void llvm::initializeAArch64PostLegalizerCombinerPass(PassRegistry &Registry) { llvm::call_once(InitializeAArch64PostLegalizerCombinerPassFlag , initializeAArch64PostLegalizerCombinerPassOnce, std::ref(Registry )); } | |||
378 | false)PassInfo *PI = new PassInfo( "Combine AArch64 MachineInstrs after legalization" , "aarch64-postlegalizer-combiner", &AArch64PostLegalizerCombiner ::ID, PassInfo::NormalCtor_t(callDefaultCtor<AArch64PostLegalizerCombiner >), false, false); Registry.registerPass(*PI, true); return PI; } static llvm::once_flag InitializeAArch64PostLegalizerCombinerPassFlag ; void llvm::initializeAArch64PostLegalizerCombinerPass(PassRegistry &Registry) { llvm::call_once(InitializeAArch64PostLegalizerCombinerPassFlag , initializeAArch64PostLegalizerCombinerPassOnce, std::ref(Registry )); } | |||
379 | ||||
380 | namespace llvm { | |||
381 | FunctionPass *createAArch64PostLegalizerCombiner(bool IsOptNone) { | |||
382 | return new AArch64PostLegalizerCombiner(IsOptNone); | |||
383 | } | |||
384 | } // end namespace llvm |
1 | //===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | /// |
9 | /// \file |
10 | /// This file implements a class to represent arbitrary precision |
11 | /// integral constant values and operations on them. |
12 | /// |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #ifndef LLVM_ADT_APINT_H |
16 | #define LLVM_ADT_APINT_H |
17 | |
18 | #include "llvm/Support/Compiler.h" |
19 | #include "llvm/Support/MathExtras.h" |
20 | #include <cassert> |
21 | #include <climits> |
22 | #include <cstring> |
23 | #include <utility> |
24 | |
25 | namespace llvm { |
26 | class FoldingSetNodeID; |
27 | class StringRef; |
28 | class hash_code; |
29 | class raw_ostream; |
30 | |
31 | template <typename T> class SmallVectorImpl; |
32 | template <typename T> class ArrayRef; |
33 | template <typename T> class Optional; |
34 | template <typename T> struct DenseMapInfo; |
35 | |
36 | class APInt; |
37 | |
38 | inline APInt operator-(APInt); |
39 | |
40 | //===----------------------------------------------------------------------===// |
41 | // APInt Class |
42 | //===----------------------------------------------------------------------===// |
43 | |
44 | /// Class for arbitrary precision integers. |
45 | /// |
46 | /// APInt is a functional replacement for common case unsigned integer type like |
47 | /// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width |
48 | /// integer sizes and large integer value types such as 3-bits, 15-bits, or more |
49 | /// than 64-bits of precision. APInt provides a variety of arithmetic operators |
50 | /// and methods to manipulate integer values of any bit-width. It supports both |
51 | /// the typical integer arithmetic and comparison operations as well as bitwise |
52 | /// manipulation. |
53 | /// |
54 | /// The class has several invariants worth noting: |
55 | /// * All bit, byte, and word positions are zero-based. |
56 | /// * Once the bit width is set, it doesn't change except by the Truncate, |
57 | /// SignExtend, or ZeroExtend operations. |
58 | /// * All binary operators must be on APInt instances of the same bit width. |
59 | /// Attempting to use these operators on instances with different bit |
60 | /// widths will yield an assertion. |
61 | /// * The value is stored canonically as an unsigned value. For operations |
62 | /// where it makes a difference, there are both signed and unsigned variants |
63 | /// of the operation. For example, sdiv and udiv. However, because the bit |
64 | /// widths must be the same, operations such as Mul and Add produce the same |
65 | /// results regardless of whether the values are interpreted as signed or |
66 | /// not. |
67 | /// * In general, the class tries to follow the style of computation that LLVM |
68 | /// uses in its IR. This simplifies its use for LLVM. |
69 | /// |
70 | class LLVM_NODISCARD[[clang::warn_unused_result]] APInt { |
71 | public: |
72 | typedef uint64_t WordType; |
73 | |
74 | /// This enum is used to hold the constants we needed for APInt. |
75 | enum : unsigned { |
76 | /// Byte size of a word. |
77 | APINT_WORD_SIZE = sizeof(WordType), |
78 | /// Bits in a word. |
79 | APINT_BITS_PER_WORD = APINT_WORD_SIZE * CHAR_BIT8 |
80 | }; |
81 | |
82 | enum class Rounding { |
83 | DOWN, |
84 | TOWARD_ZERO, |
85 | UP, |
86 | }; |
87 | |
88 | static constexpr WordType WORDTYPE_MAX = ~WordType(0); |
89 | |
90 | private: |
91 | /// This union is used to store the integer value. When the |
92 | /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal. |
93 | union { |
94 | uint64_t VAL; ///< Used to store the <= 64 bits integer value. |
95 | uint64_t *pVal; ///< Used to store the >64 bits integer value. |
96 | } U; |
97 | |
98 | unsigned BitWidth; ///< The number of bits in this APInt. |
99 | |
100 | friend struct DenseMapInfo<APInt>; |
101 | |
102 | friend class APSInt; |
103 | |
104 | /// Fast internal constructor |
105 | /// |
106 | /// This constructor is used only internally for speed of construction of |
107 | /// temporaries. It is unsafe for general use so it is not public. |
108 | APInt(uint64_t *val, unsigned bits) : BitWidth(bits) { |
109 | U.pVal = val; |
110 | } |
111 | |
112 | /// Determine which word a bit is in. |
113 | /// |
114 | /// \returns the word position for the specified bit position. |
115 | static unsigned whichWord(unsigned bitPosition) { |
116 | return bitPosition / APINT_BITS_PER_WORD; |
117 | } |
118 | |
119 | /// Determine which bit in a word a bit is in. |
120 | /// |
121 | /// \returns the bit position in a word for the specified bit position |
122 | /// in the APInt. |
123 | static unsigned whichBit(unsigned bitPosition) { |
124 | return bitPosition % APINT_BITS_PER_WORD; |
125 | } |
126 | |
127 | /// Get a single bit mask. |
128 | /// |
129 | /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set |
130 | /// This method generates and returns a uint64_t (word) mask for a single |
131 | /// bit at a specific bit position. This is used to mask the bit in the |
132 | /// corresponding word. |
133 | static uint64_t maskBit(unsigned bitPosition) { |
134 | return 1ULL << whichBit(bitPosition); |
135 | } |
136 | |
137 | /// Clear unused high order bits |
138 | /// |
139 | /// This method is used internally to clear the top "N" bits in the high order |
140 | /// word that are not used by the APInt. This is needed after the most |
141 | /// significant word is assigned a value to ensure that those bits are |
142 | /// zero'd out. |
143 | APInt &clearUnusedBits() { |
144 | // Compute how many bits are used in the final word |
145 | unsigned WordBits = ((BitWidth-1) % APINT_BITS_PER_WORD) + 1; |
146 | |
147 | // Mask out the high bits. |
148 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - WordBits); |
149 | if (isSingleWord()) |
150 | U.VAL &= mask; |
151 | else |
152 | U.pVal[getNumWords() - 1] &= mask; |
153 | return *this; |
154 | } |
155 | |
156 | /// Get the word corresponding to a bit position |
157 | /// \returns the corresponding word for the specified bit position. |
158 | uint64_t getWord(unsigned bitPosition) const { |
159 | return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)]; |
160 | } |
161 | |
162 | /// Utility method to change the bit width of this APInt to new bit width, |
163 | /// allocating and/or deallocating as necessary. There is no guarantee on the |
164 | /// value of any bits upon return. Caller should populate the bits after. |
165 | void reallocate(unsigned NewBitWidth); |
166 | |
167 | /// Convert a char array into an APInt |
168 | /// |
169 | /// \param radix 2, 8, 10, 16, or 36 |
170 | /// Converts a string into a number. The string must be non-empty |
171 | /// and well-formed as a number of the given base. The bit-width |
172 | /// must be sufficient to hold the result. |
173 | /// |
174 | /// This is used by the constructors that take string arguments. |
175 | /// |
176 | /// StringRef::getAsInteger is superficially similar but (1) does |
177 | /// not assume that the string is well-formed and (2) grows the |
178 | /// result to hold the input. |
179 | void fromString(unsigned numBits, StringRef str, uint8_t radix); |
180 | |
181 | /// An internal division function for dividing APInts. |
182 | /// |
183 | /// This is used by the toString method to divide by the radix. It simply |
184 | /// provides a more convenient form of divide for internal use since KnuthDiv |
185 | /// has specific constraints on its inputs. If those constraints are not met |
186 | /// then it provides a simpler form of divide. |
187 | static void divide(const WordType *LHS, unsigned lhsWords, |
188 | const WordType *RHS, unsigned rhsWords, WordType *Quotient, |
189 | WordType *Remainder); |
190 | |
191 | /// out-of-line slow case for inline constructor |
192 | void initSlowCase(uint64_t val, bool isSigned); |
193 | |
194 | /// shared code between two array constructors |
195 | void initFromArray(ArrayRef<uint64_t> array); |
196 | |
197 | /// out-of-line slow case for inline copy constructor |
198 | void initSlowCase(const APInt &that); |
199 | |
200 | /// out-of-line slow case for shl |
201 | void shlSlowCase(unsigned ShiftAmt); |
202 | |
203 | /// out-of-line slow case for lshr. |
204 | void lshrSlowCase(unsigned ShiftAmt); |
205 | |
206 | /// out-of-line slow case for ashr. |
207 | void ashrSlowCase(unsigned ShiftAmt); |
208 | |
209 | /// out-of-line slow case for operator= |
210 | void AssignSlowCase(const APInt &RHS); |
211 | |
212 | /// out-of-line slow case for operator== |
213 | bool EqualSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__)); |
214 | |
215 | /// out-of-line slow case for countLeadingZeros |
216 | unsigned countLeadingZerosSlowCase() const LLVM_READONLY__attribute__((__pure__)); |
217 | |
218 | /// out-of-line slow case for countLeadingOnes. |
219 | unsigned countLeadingOnesSlowCase() const LLVM_READONLY__attribute__((__pure__)); |
220 | |
221 | /// out-of-line slow case for countTrailingZeros. |
222 | unsigned countTrailingZerosSlowCase() const LLVM_READONLY__attribute__((__pure__)); |
223 | |
224 | /// out-of-line slow case for countTrailingOnes |
225 | unsigned countTrailingOnesSlowCase() const LLVM_READONLY__attribute__((__pure__)); |
226 | |
227 | /// out-of-line slow case for countPopulation |
228 | unsigned countPopulationSlowCase() const LLVM_READONLY__attribute__((__pure__)); |
229 | |
230 | /// out-of-line slow case for intersects. |
231 | bool intersectsSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__)); |
232 | |
233 | /// out-of-line slow case for isSubsetOf. |
234 | bool isSubsetOfSlowCase(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__)); |
235 | |
236 | /// out-of-line slow case for setBits. |
237 | void setBitsSlowCase(unsigned loBit, unsigned hiBit); |
238 | |
239 | /// out-of-line slow case for flipAllBits. |
240 | void flipAllBitsSlowCase(); |
241 | |
242 | /// out-of-line slow case for operator&=. |
243 | void AndAssignSlowCase(const APInt& RHS); |
244 | |
245 | /// out-of-line slow case for operator|=. |
246 | void OrAssignSlowCase(const APInt& RHS); |
247 | |
248 | /// out-of-line slow case for operator^=. |
249 | void XorAssignSlowCase(const APInt& RHS); |
250 | |
251 | /// Unsigned comparison. Returns -1, 0, or 1 if this APInt is less than, equal |
252 | /// to, or greater than RHS. |
253 | int compare(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__)); |
254 | |
255 | /// Signed comparison. Returns -1, 0, or 1 if this APInt is less than, equal |
256 | /// to, or greater than RHS. |
257 | int compareSigned(const APInt &RHS) const LLVM_READONLY__attribute__((__pure__)); |
258 | |
259 | public: |
260 | /// \name Constructors |
261 | /// @{ |
262 | |
263 | /// Create a new APInt of numBits width, initialized as val. |
264 | /// |
265 | /// If isSigned is true then val is treated as if it were a signed value |
266 | /// (i.e. as an int64_t) and the appropriate sign extension to the bit width |
267 | /// will be done. Otherwise, no sign extension occurs (high order bits beyond |
268 | /// the range of val are zero filled). |
269 | /// |
270 | /// \param numBits the bit width of the constructed APInt |
271 | /// \param val the initial value of the APInt |
272 | /// \param isSigned how to treat signedness of val |
273 | APInt(unsigned numBits, uint64_t val, bool isSigned = false) |
274 | : BitWidth(numBits) { |
275 | assert(BitWidth && "bitwidth too small")(static_cast<void> (0)); |
276 | if (isSingleWord()) { |
277 | U.VAL = val; |
278 | clearUnusedBits(); |
279 | } else { |
280 | initSlowCase(val, isSigned); |
281 | } |
282 | } |
283 | |
284 | /// Construct an APInt of numBits width, initialized as bigVal[]. |
285 | /// |
286 | /// Note that bigVal.size() can be smaller or larger than the corresponding |
287 | /// bit width but any extraneous bits will be dropped. |
288 | /// |
289 | /// \param numBits the bit width of the constructed APInt |
290 | /// \param bigVal a sequence of words to form the initial value of the APInt |
291 | APInt(unsigned numBits, ArrayRef<uint64_t> bigVal); |
292 | |
293 | /// Equivalent to APInt(numBits, ArrayRef<uint64_t>(bigVal, numWords)), but |
294 | /// deprecated because this constructor is prone to ambiguity with the |
295 | /// APInt(unsigned, uint64_t, bool) constructor. |
296 | /// |
297 | /// If this overload is ever deleted, care should be taken to prevent calls |
298 | /// from being incorrectly captured by the APInt(unsigned, uint64_t, bool) |
299 | /// constructor. |
300 | APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]); |
301 | |
302 | /// Construct an APInt from a string representation. |
303 | /// |
304 | /// This constructor interprets the string \p str in the given radix. The |
305 | /// interpretation stops when the first character that is not suitable for the |
306 | /// radix is encountered, or the end of the string. Acceptable radix values |
307 | /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the |
308 | /// string to require more bits than numBits. |
309 | /// |
310 | /// \param numBits the bit width of the constructed APInt |
311 | /// \param str the string to be interpreted |
312 | /// \param radix the radix to use for the conversion |
313 | APInt(unsigned numBits, StringRef str, uint8_t radix); |
314 | |
315 | /// Simply makes *this a copy of that. |
316 | /// Copy Constructor. |
317 | APInt(const APInt &that) : BitWidth(that.BitWidth) { |
318 | if (isSingleWord()) |
319 | U.VAL = that.U.VAL; |
320 | else |
321 | initSlowCase(that); |
322 | } |
323 | |
324 | /// Move Constructor. |
325 | APInt(APInt &&that) : BitWidth(that.BitWidth) { |
326 | memcpy(&U, &that.U, sizeof(U)); |
327 | that.BitWidth = 0; |
328 | } |
329 | |
330 | /// Destructor. |
331 | ~APInt() { |
332 | if (needsCleanup()) |
333 | delete[] U.pVal; |
334 | } |
335 | |
336 | /// Default constructor that creates an uninteresting APInt |
337 | /// representing a 1-bit zero value. |
338 | /// |
339 | /// This is useful for object deserialization (pair this with the static |
340 | /// method Read). |
341 | explicit APInt() : BitWidth(1) { U.VAL = 0; } |
342 | |
343 | /// Returns whether this instance allocated memory. |
344 | bool needsCleanup() const { return !isSingleWord(); } |
345 | |
346 | /// Used to insert APInt objects, or objects that contain APInt objects, into |
347 | /// FoldingSets. |
348 | void Profile(FoldingSetNodeID &id) const; |
349 | |
350 | /// @} |
351 | /// \name Value Tests |
352 | /// @{ |
353 | |
354 | /// Determine if this APInt just has one word to store value. |
355 | /// |
356 | /// \returns true if the number of bits <= 64, false otherwise. |
357 | bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; } |
358 | |
359 | /// Determine sign of this APInt. |
360 | /// |
361 | /// This tests the high bit of this APInt to determine if it is set. |
362 | /// |
363 | /// \returns true if this APInt is negative, false otherwise |
364 | bool isNegative() const { return (*this)[BitWidth - 1]; } |
365 | |
366 | /// Determine if this APInt Value is non-negative (>= 0) |
367 | /// |
368 | /// This tests the high bit of the APInt to determine if it is unset. |
369 | bool isNonNegative() const { return !isNegative(); } |
370 | |
371 | /// Determine if sign bit of this APInt is set. |
372 | /// |
373 | /// This tests the high bit of this APInt to determine if it is set. |
374 | /// |
375 | /// \returns true if this APInt has its sign bit set, false otherwise. |
376 | bool isSignBitSet() const { return (*this)[BitWidth-1]; } |
377 | |
378 | /// Determine if sign bit of this APInt is clear. |
379 | /// |
380 | /// This tests the high bit of this APInt to determine if it is clear. |
381 | /// |
382 | /// \returns true if this APInt has its sign bit clear, false otherwise. |
383 | bool isSignBitClear() const { return !isSignBitSet(); } |
384 | |
385 | /// Determine if this APInt Value is positive. |
386 | /// |
387 | /// This tests if the value of this APInt is positive (> 0). Note |
388 | /// that 0 is not a positive value. |
389 | /// |
390 | /// \returns true if this APInt is positive. |
391 | bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); } |
392 | |
393 | /// Determine if this APInt Value is non-positive (<= 0). |
394 | /// |
395 | /// \returns true if this APInt is non-positive. |
396 | bool isNonPositive() const { return !isStrictlyPositive(); } |
397 | |
398 | /// Determine if all bits are set |
399 | /// |
400 | /// This checks to see if the value has all bits of the APInt are set or not. |
401 | bool isAllOnesValue() const { |
402 | if (isSingleWord()) |
403 | return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth); |
404 | return countTrailingOnesSlowCase() == BitWidth; |
405 | } |
406 | |
407 | /// Determine if all bits are clear |
408 | /// |
409 | /// This checks to see if the value has all bits of the APInt are clear or |
410 | /// not. |
411 | bool isNullValue() const { return !*this; } |
412 | |
413 | /// Determine if this is a value of 1. |
414 | /// |
415 | /// This checks to see if the value of this APInt is one. |
416 | bool isOneValue() const { |
417 | if (isSingleWord()) |
418 | return U.VAL == 1; |
419 | return countLeadingZerosSlowCase() == BitWidth - 1; |
420 | } |
421 | |
422 | /// Determine if this is the largest unsigned value. |
423 | /// |
424 | /// This checks to see if the value of this APInt is the maximum unsigned |
425 | /// value for the APInt's bit width. |
426 | bool isMaxValue() const { return isAllOnesValue(); } |
427 | |
428 | /// Determine if this is the largest signed value. |
429 | /// |
430 | /// This checks to see if the value of this APInt is the maximum signed |
431 | /// value for the APInt's bit width. |
432 | bool isMaxSignedValue() const { |
433 | if (isSingleWord()) |
434 | return U.VAL == ((WordType(1) << (BitWidth - 1)) - 1); |
435 | return !isNegative() && countTrailingOnesSlowCase() == BitWidth - 1; |
436 | } |
437 | |
438 | /// Determine if this is the smallest unsigned value. |
439 | /// |
440 | /// This checks to see if the value of this APInt is the minimum unsigned |
441 | /// value for the APInt's bit width. |
442 | bool isMinValue() const { return isNullValue(); } |
443 | |
444 | /// Determine if this is the smallest signed value. |
445 | /// |
446 | /// This checks to see if the value of this APInt is the minimum signed |
447 | /// value for the APInt's bit width. |
448 | bool isMinSignedValue() const { |
449 | if (isSingleWord()) |
450 | return U.VAL == (WordType(1) << (BitWidth - 1)); |
451 | return isNegative() && countTrailingZerosSlowCase() == BitWidth - 1; |
452 | } |
453 | |
454 | /// Check if this APInt has an N-bits unsigned integer value. |
455 | bool isIntN(unsigned N) const { |
456 | assert(N && "N == 0 ???")(static_cast<void> (0)); |
457 | return getActiveBits() <= N; |
458 | } |
459 | |
460 | /// Check if this APInt has an N-bits signed integer value. |
461 | bool isSignedIntN(unsigned N) const { |
462 | assert(N && "N == 0 ???")(static_cast<void> (0)); |
463 | return getMinSignedBits() <= N; |
464 | } |
465 | |
466 | /// Check if this APInt's value is a power of two greater than zero. |
467 | /// |
468 | /// \returns true if the argument APInt value is a power of two > 0. |
469 | bool isPowerOf2() const { |
470 | if (isSingleWord()) |
471 | return isPowerOf2_64(U.VAL); |
472 | return countPopulationSlowCase() == 1; |
473 | } |
474 | |
475 | /// Check if the APInt's value is returned by getSignMask. |
476 | /// |
477 | /// \returns true if this is the value returned by getSignMask. |
478 | bool isSignMask() const { return isMinSignedValue(); } |
479 | |
480 | /// Convert APInt to a boolean value. |
481 | /// |
482 | /// This converts the APInt to a boolean value as a test against zero. |
483 | bool getBoolValue() const { return !!*this; } |
484 | |
485 | /// If this value is smaller than the specified limit, return it, otherwise |
486 | /// return the limit value. This causes the value to saturate to the limit. |
487 | uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX(18446744073709551615UL)) const { |
488 | return ugt(Limit) ? Limit : getZExtValue(); |
489 | } |
490 | |
491 | /// Check if the APInt consists of a repeated bit pattern. |
492 | /// |
493 | /// e.g. 0x01010101 satisfies isSplat(8). |
494 | /// \param SplatSizeInBits The size of the pattern in bits. Must divide bit |
495 | /// width without remainder. |
496 | bool isSplat(unsigned SplatSizeInBits) const; |
497 | |
498 | /// \returns true if this APInt value is a sequence of \param numBits ones |
499 | /// starting at the least significant bit with the remainder zero. |
500 | bool isMask(unsigned numBits) const { |
501 | assert(numBits != 0 && "numBits must be non-zero")(static_cast<void> (0)); |
502 | assert(numBits <= BitWidth && "numBits out of range")(static_cast<void> (0)); |
503 | if (isSingleWord()) |
504 | return U.VAL == (WORDTYPE_MAX >> (APINT_BITS_PER_WORD - numBits)); |
505 | unsigned Ones = countTrailingOnesSlowCase(); |
506 | return (numBits == Ones) && |
507 | ((Ones + countLeadingZerosSlowCase()) == BitWidth); |
508 | } |
509 | |
510 | /// \returns true if this APInt is a non-empty sequence of ones starting at |
511 | /// the least significant bit with the remainder zero. |
512 | /// Ex. isMask(0x0000FFFFU) == true. |
513 | bool isMask() const { |
514 | if (isSingleWord()) |
515 | return isMask_64(U.VAL); |
516 | unsigned Ones = countTrailingOnesSlowCase(); |
517 | return (Ones > 0) && ((Ones + countLeadingZerosSlowCase()) == BitWidth); |
518 | } |
519 | |
520 | /// Return true if this APInt value contains a sequence of ones with |
521 | /// the remainder zero. |
522 | bool isShiftedMask() const { |
523 | if (isSingleWord()) |
524 | return isShiftedMask_64(U.VAL); |
525 | unsigned Ones = countPopulationSlowCase(); |
526 | unsigned LeadZ = countLeadingZerosSlowCase(); |
527 | return (Ones + LeadZ + countTrailingZeros()) == BitWidth; |
528 | } |
529 | |
530 | /// @} |
531 | /// \name Value Generators |
532 | /// @{ |
533 | |
534 | /// Gets maximum unsigned value of APInt for specific bit width. |
535 | static APInt getMaxValue(unsigned numBits) { |
536 | return getAllOnesValue(numBits); |
537 | } |
538 | |
539 | /// Gets maximum signed value of APInt for a specific bit width. |
540 | static APInt getSignedMaxValue(unsigned numBits) { |
541 | APInt API = getAllOnesValue(numBits); |
542 | API.clearBit(numBits - 1); |
543 | return API; |
544 | } |
545 | |
546 | /// Gets minimum unsigned value of APInt for a specific bit width. |
547 | static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); } |
548 | |
549 | /// Gets minimum signed value of APInt for a specific bit width. |
550 | static APInt getSignedMinValue(unsigned numBits) { |
551 | APInt API(numBits, 0); |
552 | API.setBit(numBits - 1); |
553 | return API; |
554 | } |
555 | |
556 | /// Get the SignMask for a specific bit width. |
557 | /// |
558 | /// This is just a wrapper function of getSignedMinValue(), and it helps code |
559 | /// readability when we want to get a SignMask. |
560 | static APInt getSignMask(unsigned BitWidth) { |
561 | return getSignedMinValue(BitWidth); |
562 | } |
563 | |
564 | /// Get the all-ones value. |
565 | /// |
566 | /// \returns the all-ones value for an APInt of the specified bit-width. |
567 | static APInt getAllOnesValue(unsigned numBits) { |
568 | return APInt(numBits, WORDTYPE_MAX, true); |
569 | } |
570 | |
571 | /// Get the '0' value. |
572 | /// |
573 | /// \returns the '0' value for an APInt of the specified bit-width. |
574 | static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); } |
575 | |
576 | /// Compute an APInt containing numBits highbits from this APInt. |
577 | /// |
578 | /// Get an APInt with the same BitWidth as this APInt, just zero mask |
579 | /// the low bits and right shift to the least significant bit. |
580 | /// |
581 | /// \returns the high "numBits" bits of this APInt. |
582 | APInt getHiBits(unsigned numBits) const; |
583 | |
584 | /// Compute an APInt containing numBits lowbits from this APInt. |
585 | /// |
586 | /// Get an APInt with the same BitWidth as this APInt, just zero mask |
587 | /// the high bits. |
588 | /// |
589 | /// \returns the low "numBits" bits of this APInt. |
590 | APInt getLoBits(unsigned numBits) const; |
591 | |
592 | /// Return an APInt with exactly one bit set in the result. |
593 | static APInt getOneBitSet(unsigned numBits, unsigned BitNo) { |
594 | APInt Res(numBits, 0); |
595 | Res.setBit(BitNo); |
596 | return Res; |
597 | } |
598 | |
599 | /// Get a value with a block of bits set. |
600 | /// |
601 | /// Constructs an APInt value that has a contiguous range of bits set. The |
602 | /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other |
603 | /// bits will be zero. For example, with parameters(32, 0, 16) you would get |
604 | /// 0x0000FFFF. Please call getBitsSetWithWrap if \p loBit may be greater than |
605 | /// \p hiBit. |
606 | /// |
607 | /// \param numBits the intended bit width of the result |
608 | /// \param loBit the index of the lowest bit set. |
609 | /// \param hiBit the index of the highest bit set. |
610 | /// |
611 | /// \returns An APInt value with the requested bits set. |
612 | static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) { |
613 | assert(loBit <= hiBit && "loBit greater than hiBit")(static_cast<void> (0)); |
614 | APInt Res(numBits, 0); |
615 | Res.setBits(loBit, hiBit); |
616 | return Res; |
617 | } |
618 | |
619 | /// Wrap version of getBitsSet. |
620 | /// If \p hiBit is bigger than \p loBit, this is same with getBitsSet. |
621 | /// If \p hiBit is not bigger than \p loBit, the set bits "wrap". For example, |
622 | /// with parameters (32, 28, 4), you would get 0xF000000F. |
623 | /// If \p hiBit is equal to \p loBit, you would get a result with all bits |
624 | /// set. |
625 | static APInt getBitsSetWithWrap(unsigned numBits, unsigned loBit, |
626 | unsigned hiBit) { |
627 | APInt Res(numBits, 0); |
628 | Res.setBitsWithWrap(loBit, hiBit); |
629 | return Res; |
630 | } |
631 | |
632 | /// Get a value with upper bits starting at loBit set. |
633 | /// |
634 | /// Constructs an APInt value that has a contiguous range of bits set. The |
635 | /// bits from loBit (inclusive) to numBits (exclusive) will be set. All other |
636 | /// bits will be zero. For example, with parameters(32, 12) you would get |
637 | /// 0xFFFFF000. |
638 | /// |
639 | /// \param numBits the intended bit width of the result |
640 | /// \param loBit the index of the lowest bit to set. |
641 | /// |
642 | /// \returns An APInt value with the requested bits set. |
643 | static APInt getBitsSetFrom(unsigned numBits, unsigned loBit) { |
644 | APInt Res(numBits, 0); |
645 | Res.setBitsFrom(loBit); |
646 | return Res; |
647 | } |
648 | |
649 | /// Get a value with high bits set |
650 | /// |
651 | /// Constructs an APInt value that has the top hiBitsSet bits set. |
652 | /// |
653 | /// \param numBits the bitwidth of the result |
654 | /// \param hiBitsSet the number of high-order bits set in the result. |
655 | static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) { |
656 | APInt Res(numBits, 0); |
657 | Res.setHighBits(hiBitsSet); |
658 | return Res; |
659 | } |
660 | |
661 | /// Get a value with low bits set |
662 | /// |
663 | /// Constructs an APInt value that has the bottom loBitsSet bits set. |
664 | /// |
665 | /// \param numBits the bitwidth of the result |
666 | /// \param loBitsSet the number of low-order bits set in the result. |
667 | static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) { |
668 | APInt Res(numBits, 0); |
669 | Res.setLowBits(loBitsSet); |
670 | return Res; |
671 | } |
672 | |
673 | /// Return a value containing V broadcasted over NewLen bits. |
674 | static APInt getSplat(unsigned NewLen, const APInt &V); |
675 | |
676 | /// Determine if two APInts have the same value, after zero-extending |
677 | /// one of them (if needed!) to ensure that the bit-widths match. |
678 | static bool isSameValue(const APInt &I1, const APInt &I2) { |
679 | if (I1.getBitWidth() == I2.getBitWidth()) |
680 | return I1 == I2; |
681 | |
682 | if (I1.getBitWidth() > I2.getBitWidth()) |
683 | return I1 == I2.zext(I1.getBitWidth()); |
684 | |
685 | return I1.zext(I2.getBitWidth()) == I2; |
686 | } |
687 | |
688 | /// Overload to compute a hash_code for an APInt value. |
689 | friend hash_code hash_value(const APInt &Arg); |
690 | |
691 | /// This function returns a pointer to the internal storage of the APInt. |
692 | /// This is useful for writing out the APInt in binary form without any |
693 | /// conversions. |
694 | const uint64_t *getRawData() const { |
695 | if (isSingleWord()) |
696 | return &U.VAL; |
697 | return &U.pVal[0]; |
698 | } |
699 | |
700 | /// @} |
701 | /// \name Unary Operators |
702 | /// @{ |
703 | |
704 | /// Postfix increment operator. |
705 | /// |
706 | /// Increments *this by 1. |
707 | /// |
708 | /// \returns a new APInt value representing the original value of *this. |
709 | APInt operator++(int) { |
710 | APInt API(*this); |
711 | ++(*this); |
712 | return API; |
713 | } |
714 | |
715 | /// Prefix increment operator. |
716 | /// |
717 | /// \returns *this incremented by one |
718 | APInt &operator++(); |
719 | |
720 | /// Postfix decrement operator. |
721 | /// |
722 | /// Decrements *this by 1. |
723 | /// |
724 | /// \returns a new APInt value representing the original value of *this. |
725 | APInt operator--(int) { |
726 | APInt API(*this); |
727 | --(*this); |
728 | return API; |
729 | } |
730 | |
731 | /// Prefix decrement operator. |
732 | /// |
733 | /// \returns *this decremented by one. |
734 | APInt &operator--(); |
735 | |
736 | /// Logical negation operator. |
737 | /// |
738 | /// Performs logical negation operation on this APInt. |
739 | /// |
740 | /// \returns true if *this is zero, false otherwise. |
741 | bool operator!() const { |
742 | if (isSingleWord()) |
743 | return U.VAL == 0; |
744 | return countLeadingZerosSlowCase() == BitWidth; |
745 | } |
746 | |
747 | /// @} |
748 | /// \name Assignment Operators |
749 | /// @{ |
750 | |
751 | /// Copy assignment operator. |
752 | /// |
753 | /// \returns *this after assignment of RHS. |
754 | APInt &operator=(const APInt &RHS) { |
755 | // If the bitwidths are the same, we can avoid mucking with memory |
756 | if (isSingleWord() && RHS.isSingleWord()) { |
757 | U.VAL = RHS.U.VAL; |
758 | BitWidth = RHS.BitWidth; |
759 | return clearUnusedBits(); |
760 | } |
761 | |
762 | AssignSlowCase(RHS); |
763 | return *this; |
764 | } |
765 | |
766 | /// Move assignment operator. |
767 | APInt &operator=(APInt &&that) { |
768 | #ifdef EXPENSIVE_CHECKS |
769 | // Some std::shuffle implementations still do self-assignment. |
770 | if (this == &that) |
771 | return *this; |
772 | #endif |
773 | assert(this != &that && "Self-move not supported")(static_cast<void> (0)); |
774 | if (!isSingleWord()) |
775 | delete[] U.pVal; |
776 | |
777 | // Use memcpy so that type based alias analysis sees both VAL and pVal |
778 | // as modified. |
779 | memcpy(&U, &that.U, sizeof(U)); |
780 | |
781 | BitWidth = that.BitWidth; |
782 | that.BitWidth = 0; |
783 | |
784 | return *this; |
785 | } |
786 | |
787 | /// Assignment operator. |
788 | /// |
789 | /// The RHS value is assigned to *this. If the significant bits in RHS exceed |
790 | /// the bit width, the excess bits are truncated. If the bit width is larger |
791 | /// than 64, the value is zero filled in the unspecified high order bits. |
792 | /// |
793 | /// \returns *this after assignment of RHS value. |
794 | APInt &operator=(uint64_t RHS) { |
795 | if (isSingleWord()) { |
796 | U.VAL = RHS; |
797 | return clearUnusedBits(); |
798 | } |
799 | U.pVal[0] = RHS; |
800 | memset(U.pVal + 1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
801 | return *this; |
802 | } |
803 | |
804 | /// Bitwise AND assignment operator. |
805 | /// |
806 | /// Performs a bitwise AND operation on this APInt and RHS. The result is |
807 | /// assigned to *this. |
808 | /// |
809 | /// \returns *this after ANDing with RHS. |
810 | APInt &operator&=(const APInt &RHS) { |
811 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast<void> (0)); |
812 | if (isSingleWord()) |
813 | U.VAL &= RHS.U.VAL; |
814 | else |
815 | AndAssignSlowCase(RHS); |
816 | return *this; |
817 | } |
818 | |
819 | /// Bitwise AND assignment operator. |
820 | /// |
821 | /// Performs a bitwise AND operation on this APInt and RHS. RHS is |
822 | /// logically zero-extended or truncated to match the bit-width of |
823 | /// the LHS. |
824 | APInt &operator&=(uint64_t RHS) { |
825 | if (isSingleWord()) { |
826 | U.VAL &= RHS; |
827 | return *this; |
828 | } |
829 | U.pVal[0] &= RHS; |
830 | memset(U.pVal+1, 0, (getNumWords() - 1) * APINT_WORD_SIZE); |
831 | return *this; |
832 | } |
833 | |
834 | /// Bitwise OR assignment operator. |
835 | /// |
836 | /// Performs a bitwise OR operation on this APInt and RHS. The result is |
837 | /// assigned *this; |
838 | /// |
839 | /// \returns *this after ORing with RHS. |
840 | APInt &operator|=(const APInt &RHS) { |
841 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast<void> (0)); |
842 | if (isSingleWord()) |
843 | U.VAL |= RHS.U.VAL; |
844 | else |
845 | OrAssignSlowCase(RHS); |
846 | return *this; |
847 | } |
848 | |
849 | /// Bitwise OR assignment operator. |
850 | /// |
851 | /// Performs a bitwise OR operation on this APInt and RHS. RHS is |
852 | /// logically zero-extended or truncated to match the bit-width of |
853 | /// the LHS. |
854 | APInt &operator|=(uint64_t RHS) { |
855 | if (isSingleWord()) { |
856 | U.VAL |= RHS; |
857 | return clearUnusedBits(); |
858 | } |
859 | U.pVal[0] |= RHS; |
860 | return *this; |
861 | } |
862 | |
863 | /// Bitwise XOR assignment operator. |
864 | /// |
865 | /// Performs a bitwise XOR operation on this APInt and RHS. The result is |
866 | /// assigned to *this. |
867 | /// |
868 | /// \returns *this after XORing with RHS. |
869 | APInt &operator^=(const APInt &RHS) { |
870 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast<void> (0)); |
871 | if (isSingleWord()) |
872 | U.VAL ^= RHS.U.VAL; |
873 | else |
874 | XorAssignSlowCase(RHS); |
875 | return *this; |
876 | } |
877 | |
878 | /// Bitwise XOR assignment operator. |
879 | /// |
880 | /// Performs a bitwise XOR operation on this APInt and RHS. RHS is |
881 | /// logically zero-extended or truncated to match the bit-width of |
882 | /// the LHS. |
883 | APInt &operator^=(uint64_t RHS) { |
884 | if (isSingleWord()) { |
885 | U.VAL ^= RHS; |
886 | return clearUnusedBits(); |
887 | } |
888 | U.pVal[0] ^= RHS; |
889 | return *this; |
890 | } |
891 | |
892 | /// Multiplication assignment operator. |
893 | /// |
894 | /// Multiplies this APInt by RHS and assigns the result to *this. |
895 | /// |
896 | /// \returns *this |
897 | APInt &operator*=(const APInt &RHS); |
898 | APInt &operator*=(uint64_t RHS); |
899 | |
900 | /// Addition assignment operator. |
901 | /// |
902 | /// Adds RHS to *this and assigns the result to *this. |
903 | /// |
904 | /// \returns *this |
905 | APInt &operator+=(const APInt &RHS); |
906 | APInt &operator+=(uint64_t RHS); |
907 | |
908 | /// Subtraction assignment operator. |
909 | /// |
910 | /// Subtracts RHS from *this and assigns the result to *this. |
911 | /// |
912 | /// \returns *this |
913 | APInt &operator-=(const APInt &RHS); |
914 | APInt &operator-=(uint64_t RHS); |
915 | |
916 | /// Left-shift assignment function. |
917 | /// |
918 | /// Shifts *this left by shiftAmt and assigns the result to *this. |
919 | /// |
920 | /// \returns *this after shifting left by ShiftAmt |
921 | APInt &operator<<=(unsigned ShiftAmt) { |
922 | assert(ShiftAmt <= BitWidth && "Invalid shift amount")(static_cast<void> (0)); |
923 | if (isSingleWord()) { |
924 | if (ShiftAmt == BitWidth) |
925 | U.VAL = 0; |
926 | else |
927 | U.VAL <<= ShiftAmt; |
928 | return clearUnusedBits(); |
929 | } |
930 | shlSlowCase(ShiftAmt); |
931 | return *this; |
932 | } |
933 | |
934 | /// Left-shift assignment function. |
935 | /// |
936 | /// Shifts *this left by shiftAmt and assigns the result to *this. |
937 | /// |
938 | /// \returns *this after shifting left by ShiftAmt |
939 | APInt &operator<<=(const APInt &ShiftAmt); |
940 | |
941 | /// @} |
942 | /// \name Binary Operators |
943 | /// @{ |
944 | |
945 | /// Multiplication operator. |
946 | /// |
947 | /// Multiplies this APInt by RHS and returns the result. |
948 | APInt operator*(const APInt &RHS) const; |
949 | |
950 | /// Left logical shift operator. |
951 | /// |
952 | /// Shifts this APInt left by \p Bits and returns the result. |
953 | APInt operator<<(unsigned Bits) const { return shl(Bits); } |
954 | |
955 | /// Left logical shift operator. |
956 | /// |
957 | /// Shifts this APInt left by \p Bits and returns the result. |
958 | APInt operator<<(const APInt &Bits) const { return shl(Bits); } |
959 | |
960 | /// Arithmetic right-shift function. |
961 | /// |
962 | /// Arithmetic right-shift this APInt by shiftAmt. |
963 | APInt ashr(unsigned ShiftAmt) const { |
964 | APInt R(*this); |
965 | R.ashrInPlace(ShiftAmt); |
966 | return R; |
967 | } |
968 | |
969 | /// Arithmetic right-shift this APInt by ShiftAmt in place. |
970 | void ashrInPlace(unsigned ShiftAmt) { |
971 | assert(ShiftAmt <= BitWidth && "Invalid shift amount")(static_cast<void> (0)); |
972 | if (isSingleWord()) { |
973 | int64_t SExtVAL = SignExtend64(U.VAL, BitWidth); |
974 | if (ShiftAmt == BitWidth) |
975 | U.VAL = SExtVAL >> (APINT_BITS_PER_WORD - 1); // Fill with sign bit. |
976 | else |
977 | U.VAL = SExtVAL >> ShiftAmt; |
978 | clearUnusedBits(); |
979 | return; |
980 | } |
981 | ashrSlowCase(ShiftAmt); |
982 | } |
983 | |
984 | /// Logical right-shift function. |
985 | /// |
986 | /// Logical right-shift this APInt by shiftAmt. |
987 | APInt lshr(unsigned shiftAmt) const { |
988 | APInt R(*this); |
989 | R.lshrInPlace(shiftAmt); |
990 | return R; |
991 | } |
992 | |
993 | /// Logical right-shift this APInt by ShiftAmt in place. |
994 | void lshrInPlace(unsigned ShiftAmt) { |
995 | assert(ShiftAmt <= BitWidth && "Invalid shift amount")(static_cast<void> (0)); |
996 | if (isSingleWord()) { |
997 | if (ShiftAmt == BitWidth) |
998 | U.VAL = 0; |
999 | else |
1000 | U.VAL >>= ShiftAmt; |
1001 | return; |
1002 | } |
1003 | lshrSlowCase(ShiftAmt); |
1004 | } |
1005 | |
1006 | /// Left-shift function. |
1007 | /// |
1008 | /// Left-shift this APInt by shiftAmt. |
1009 | APInt shl(unsigned shiftAmt) const { |
1010 | APInt R(*this); |
1011 | R <<= shiftAmt; |
1012 | return R; |
1013 | } |
1014 | |
1015 | /// Rotate left by rotateAmt. |
1016 | APInt rotl(unsigned rotateAmt) const; |
1017 | |
1018 | /// Rotate right by rotateAmt. |
1019 | APInt rotr(unsigned rotateAmt) const; |
1020 | |
1021 | /// Arithmetic right-shift function. |
1022 | /// |
1023 | /// Arithmetic right-shift this APInt by shiftAmt. |
1024 | APInt ashr(const APInt &ShiftAmt) const { |
1025 | APInt R(*this); |
1026 | R.ashrInPlace(ShiftAmt); |
1027 | return R; |
1028 | } |
1029 | |
1030 | /// Arithmetic right-shift this APInt by shiftAmt in place. |
1031 | void ashrInPlace(const APInt &shiftAmt); |
1032 | |
1033 | /// Logical right-shift function. |
1034 | /// |
1035 | /// Logical right-shift this APInt by shiftAmt. |
1036 | APInt lshr(const APInt &ShiftAmt) const { |
1037 | APInt R(*this); |
1038 | R.lshrInPlace(ShiftAmt); |
1039 | return R; |
1040 | } |
1041 | |
1042 | /// Logical right-shift this APInt by ShiftAmt in place. |
1043 | void lshrInPlace(const APInt &ShiftAmt); |
1044 | |
1045 | /// Left-shift function. |
1046 | /// |
1047 | /// Left-shift this APInt by shiftAmt. |
1048 | APInt shl(const APInt &ShiftAmt) const { |
1049 | APInt R(*this); |
1050 | R <<= ShiftAmt; |
1051 | return R; |
1052 | } |
1053 | |
1054 | /// Rotate left by rotateAmt. |
1055 | APInt rotl(const APInt &rotateAmt) const; |
1056 | |
1057 | /// Rotate right by rotateAmt. |
1058 | APInt rotr(const APInt &rotateAmt) const; |
1059 | |
1060 | /// Unsigned division operation. |
1061 | /// |
1062 | /// Perform an unsigned divide operation on this APInt by RHS. Both this and |
1063 | /// RHS are treated as unsigned quantities for purposes of this division. |
1064 | /// |
1065 | /// \returns a new APInt value containing the division result, rounded towards |
1066 | /// zero. |
1067 | APInt udiv(const APInt &RHS) const; |
1068 | APInt udiv(uint64_t RHS) const; |
1069 | |
1070 | /// Signed division function for APInt. |
1071 | /// |
1072 | /// Signed divide this APInt by APInt RHS. |
1073 | /// |
1074 | /// The result is rounded towards zero. |
1075 | APInt sdiv(const APInt &RHS) const; |
1076 | APInt sdiv(int64_t RHS) const; |
1077 | |
1078 | /// Unsigned remainder operation. |
1079 | /// |
1080 | /// Perform an unsigned remainder operation on this APInt with RHS being the |
1081 | /// divisor. Both this and RHS are treated as unsigned quantities for purposes |
1082 | /// of this operation. Note that this is a true remainder operation and not a |
1083 | /// modulo operation because the sign follows the sign of the dividend which |
1084 | /// is *this. |
1085 | /// |
1086 | /// \returns a new APInt value containing the remainder result |
1087 | APInt urem(const APInt &RHS) const; |
1088 | uint64_t urem(uint64_t RHS) const; |
1089 | |
1090 | /// Function for signed remainder operation. |
1091 | /// |
1092 | /// Signed remainder operation on APInt. |
1093 | APInt srem(const APInt &RHS) const; |
1094 | int64_t srem(int64_t RHS) const; |
1095 | |
1096 | /// Dual division/remainder interface. |
1097 | /// |
1098 | /// Sometimes it is convenient to divide two APInt values and obtain both the |
1099 | /// quotient and remainder. This function does both operations in the same |
1100 | /// computation making it a little more efficient. The pair of input arguments |
1101 | /// may overlap with the pair of output arguments. It is safe to call |
1102 | /// udivrem(X, Y, X, Y), for example. |
1103 | static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, |
1104 | APInt &Remainder); |
1105 | static void udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient, |
1106 | uint64_t &Remainder); |
1107 | |
1108 | static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, |
1109 | APInt &Remainder); |
1110 | static void sdivrem(const APInt &LHS, int64_t RHS, APInt &Quotient, |
1111 | int64_t &Remainder); |
1112 | |
1113 | // Operations that return overflow indicators. |
1114 | APInt sadd_ov(const APInt &RHS, bool &Overflow) const; |
1115 | APInt uadd_ov(const APInt &RHS, bool &Overflow) const; |
1116 | APInt ssub_ov(const APInt &RHS, bool &Overflow) const; |
1117 | APInt usub_ov(const APInt &RHS, bool &Overflow) const; |
1118 | APInt sdiv_ov(const APInt &RHS, bool &Overflow) const; |
1119 | APInt smul_ov(const APInt &RHS, bool &Overflow) const; |
1120 | APInt umul_ov(const APInt &RHS, bool &Overflow) const; |
1121 | APInt sshl_ov(const APInt &Amt, bool &Overflow) const; |
1122 | APInt ushl_ov(const APInt &Amt, bool &Overflow) const; |
1123 | |
1124 | // Operations that saturate |
1125 | APInt sadd_sat(const APInt &RHS) const; |
1126 | APInt uadd_sat(const APInt &RHS) const; |
1127 | APInt ssub_sat(const APInt &RHS) const; |
1128 | APInt usub_sat(const APInt &RHS) const; |
1129 | APInt smul_sat(const APInt &RHS) const; |
1130 | APInt umul_sat(const APInt &RHS) const; |
1131 | APInt sshl_sat(const APInt &RHS) const; |
1132 | APInt ushl_sat(const APInt &RHS) const; |
1133 | |
1134 | /// Array-indexing support. |
1135 | /// |
1136 | /// \returns the bit value at bitPosition |
1137 | bool operator[](unsigned bitPosition) const { |
1138 | assert(bitPosition < getBitWidth() && "Bit position out of bounds!")(static_cast<void> (0)); |
1139 | return (maskBit(bitPosition) & getWord(bitPosition)) != 0; |
1140 | } |
1141 | |
1142 | /// @} |
1143 | /// \name Comparison Operators |
1144 | /// @{ |
1145 | |
1146 | /// Equality operator. |
1147 | /// |
1148 | /// Compares this APInt with RHS for the validity of the equality |
1149 | /// relationship. |
1150 | bool operator==(const APInt &RHS) const { |
1151 | assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths")(static_cast<void> (0)); |
1152 | if (isSingleWord()) |
1153 | return U.VAL == RHS.U.VAL; |
1154 | return EqualSlowCase(RHS); |
1155 | } |
1156 | |
1157 | /// Equality operator. |
1158 | /// |
1159 | /// Compares this APInt with a uint64_t for the validity of the equality |
1160 | /// relationship. |
1161 | /// |
1162 | /// \returns true if *this == Val |
1163 | bool operator==(uint64_t Val) const { |
1164 | return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() == Val; |
1165 | } |
1166 | |
1167 | /// Equality comparison. |
1168 | /// |
1169 | /// Compares this APInt with RHS for the validity of the equality |
1170 | /// relationship. |
1171 | /// |
1172 | /// \returns true if *this == Val |
1173 | bool eq(const APInt &RHS) const { return (*this) == RHS; } |
1174 | |
1175 | /// Inequality operator. |
1176 | /// |
1177 | /// Compares this APInt with RHS for the validity of the inequality |
1178 | /// relationship. |
1179 | /// |
1180 | /// \returns true if *this != Val |
1181 | bool operator!=(const APInt &RHS) const { return !((*this) == RHS); } |
1182 | |
1183 | /// Inequality operator. |
1184 | /// |
1185 | /// Compares this APInt with a uint64_t for the validity of the inequality |
1186 | /// relationship. |
1187 | /// |
1188 | /// \returns true if *this != Val |
1189 | bool operator!=(uint64_t Val) const { return !((*this) == Val); } |
1190 | |
1191 | /// Inequality comparison |
1192 | /// |
1193 | /// Compares this APInt with RHS for the validity of the inequality |
1194 | /// relationship. |
1195 | /// |
1196 | /// \returns true if *this != Val |
1197 | bool ne(const APInt &RHS) const { return !((*this) == RHS); } |
1198 | |
1199 | /// Unsigned less than comparison |
1200 | /// |
1201 | /// Regards both *this and RHS as unsigned quantities and compares them for |
1202 | /// the validity of the less-than relationship. |
1203 | /// |
1204 | /// \returns true if *this < RHS when both are considered unsigned. |
1205 | bool ult(const APInt &RHS) const { return compare(RHS) < 0; } |
1206 | |
1207 | /// Unsigned less than comparison |
1208 | /// |
1209 | /// Regards both *this as an unsigned quantity and compares it with RHS for |
1210 | /// the validity of the less-than relationship. |
1211 | /// |
1212 | /// \returns true if *this < RHS when considered unsigned. |
1213 | bool ult(uint64_t RHS) const { |
1214 | // Only need to check active bits if not a single word. |
1215 | return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() < RHS; |
1216 | } |
1217 | |
1218 | /// Signed less than comparison |
1219 | /// |
1220 | /// Regards both *this and RHS as signed quantities and compares them for |
1221 | /// validity of the less-than relationship. |
1222 | /// |
1223 | /// \returns true if *this < RHS when both are considered signed. |
1224 | bool slt(const APInt &RHS) const { return compareSigned(RHS) < 0; } |
1225 | |
1226 | /// Signed less than comparison |
1227 | /// |
1228 | /// Regards both *this as a signed quantity and compares it with RHS for |
1229 | /// the validity of the less-than relationship. |
1230 | /// |
1231 | /// \returns true if *this < RHS when considered signed. |
1232 | bool slt(int64_t RHS) const { |
1233 | return (!isSingleWord() && getMinSignedBits() > 64) ? isNegative() |
1234 | : getSExtValue() < RHS; |
1235 | } |
1236 | |
1237 | /// Unsigned less or equal comparison |
1238 | /// |
1239 | /// Regards both *this and RHS as unsigned quantities and compares them for |
1240 | /// validity of the less-or-equal relationship. |
1241 | /// |
1242 | /// \returns true if *this <= RHS when both are considered unsigned. |
1243 | bool ule(const APInt &RHS) const { return compare(RHS) <= 0; } |
1244 | |
1245 | /// Unsigned less or equal comparison |
1246 | /// |
1247 | /// Regards both *this as an unsigned quantity and compares it with RHS for |
1248 | /// the validity of the less-or-equal relationship. |
1249 | /// |
1250 | /// \returns true if *this <= RHS when considered unsigned. |
1251 | bool ule(uint64_t RHS) const { return !ugt(RHS); } |
1252 | |
1253 | /// Signed less or equal comparison |
1254 | /// |
1255 | /// Regards both *this and RHS as signed quantities and compares them for |
1256 | /// validity of the less-or-equal relationship. |
1257 | /// |
1258 | /// \returns true if *this <= RHS when both are considered signed. |
1259 | bool sle(const APInt &RHS) const { return compareSigned(RHS) <= 0; } |
1260 | |
1261 | /// Signed less or equal comparison |
1262 | /// |
1263 | /// Regards both *this as a signed quantity and compares it with RHS for the |
1264 | /// validity of the less-or-equal relationship. |
1265 | /// |
1266 | /// \returns true if *this <= RHS when considered signed. |
1267 | bool sle(uint64_t RHS) const { return !sgt(RHS); } |
1268 | |
1269 | /// Unsigned greater than comparison |
1270 | /// |
1271 | /// Regards both *this and RHS as unsigned quantities and compares them for |
1272 | /// the validity of the greater-than relationship. |
1273 | /// |
1274 | /// \returns true if *this > RHS when both are considered unsigned. |
1275 | bool ugt(const APInt &RHS) const { return !ule(RHS); } |
1276 | |
1277 | /// Unsigned greater than comparison |
1278 | /// |
1279 | /// Regards both *this as an unsigned quantity and compares it with RHS for |
1280 | /// the validity of the greater-than relationship. |
1281 | /// |
1282 | /// \returns true if *this > RHS when considered unsigned. |
1283 | bool ugt(uint64_t RHS) const { |
1284 | // Only need to check active bits if not a single word. |
1285 | return (!isSingleWord() && getActiveBits() > 64) || getZExtValue() > RHS; |
1286 | } |
1287 | |
1288 | /// Signed greater than comparison |
1289 | /// |
1290 | /// Regards both *this and RHS as signed quantities and compares them for the |
1291 | /// validity of the greater-than relationship. |
1292 | /// |
1293 | /// \returns true if *this > RHS when both are considered signed. |
1294 | bool sgt(const APInt &RHS) const { return !sle(RHS); } |
1295 | |
1296 | /// Signed greater than comparison |
1297 | /// |
1298 | /// Regards both *this as a signed quantity and compares it with RHS for |
1299 | /// the validity of the greater-than relationship. |
1300 | /// |
1301 | /// \returns true if *this > RHS when considered signed. |
1302 | bool sgt(int64_t RHS) const { |
1303 | return (!isSingleWord() && getMinSignedBits() > 64) ? !isNegative() |
1304 | : getSExtValue() > RHS; |
1305 | } |
1306 | |
1307 | /// Unsigned greater or equal comparison |
1308 | /// |
1309 | /// Regards both *this and RHS as unsigned quantities and compares them for |
1310 | /// validity of the greater-or-equal relationship. |
1311 | /// |
1312 | /// \returns true if *this >= RHS when both are considered unsigned. |
1313 | bool uge(const APInt &RHS) const { return !ult(RHS); } |
1314 | |
1315 | /// Unsigned greater or equal comparison |
1316 | /// |
1317 | /// Regards both *this as an unsigned quantity and compares it with RHS for |
1318 | /// the validity of the greater-or-equal relationship. |
1319 | /// |
1320 | /// \returns true if *this >= RHS when considered unsigned. |
1321 | bool uge(uint64_t RHS) const { return !ult(RHS); } |
1322 | |
1323 | /// Signed greater or equal comparison |
1324 | /// |
1325 | /// Regards both *this and RHS as signed quantities and compares them for |
1326 | /// validity of the greater-or-equal relationship. |
1327 | /// |
1328 | /// \returns true if *this >= RHS when both are considered signed. |
1329 | bool sge(const APInt &RHS) const { return !slt(RHS); } |
1330 | |
1331 | /// Signed greater or equal comparison |
1332 | /// |
1333 | /// Regards both *this as a signed quantity and compares it with RHS for |
1334 | /// the validity of the greater-or-equal relationship. |
1335 | /// |
1336 | /// \returns true if *this >= RHS when considered signed. |
1337 | bool sge(int64_t RHS) const { return !slt(RHS); } |
1338 | |
1339 | /// This operation tests if there are any pairs of corresponding bits |
1340 | /// between this APInt and RHS that are both set. |
1341 | bool intersects(const APInt &RHS) const { |
1342 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast<void> (0)); |
1343 | if (isSingleWord()) |
1344 | return (U.VAL & RHS.U.VAL) != 0; |
1345 | return intersectsSlowCase(RHS); |
1346 | } |
1347 | |
1348 | /// This operation checks that all bits set in this APInt are also set in RHS. |
1349 | bool isSubsetOf(const APInt &RHS) const { |
1350 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same")(static_cast<void> (0)); |
1351 | if (isSingleWord()) |
1352 | return (U.VAL & ~RHS.U.VAL) == 0; |
1353 | return isSubsetOfSlowCase(RHS); |
1354 | } |
1355 | |
1356 | /// @} |
1357 | /// \name Resizing Operators |
1358 | /// @{ |
1359 | |
1360 | /// Truncate to new width. |
1361 | /// |
1362 | /// Truncate the APInt to a specified width. It is an error to specify a width |
1363 | /// that is greater than or equal to the current width. |
1364 | APInt trunc(unsigned width) const; |
1365 | |
1366 | /// Truncate to new width with unsigned saturation. |
1367 | /// |
1368 | /// If the APInt, treated as unsigned integer, can be losslessly truncated to |
1369 | /// the new bitwidth, then return truncated APInt. Else, return max value. |
1370 | APInt truncUSat(unsigned width) const; |
1371 | |
1372 | /// Truncate to new width with signed saturation. |
1373 | /// |
1374 | /// If this APInt, treated as signed integer, can be losslessly truncated to |
1375 | /// the new bitwidth, then return truncated APInt. Else, return either |
1376 | /// signed min value if the APInt was negative, or signed max value. |
1377 | APInt truncSSat(unsigned width) const; |
1378 | |
1379 | /// Sign extend to a new width. |
1380 | /// |
1381 | /// This operation sign extends the APInt to a new width. If the high order |
1382 | /// bit is set, the fill on the left will be done with 1 bits, otherwise zero. |
1383 | /// It is an error to specify a width that is less than or equal to the |
1384 | /// current width. |
1385 | APInt sext(unsigned width) const; |
1386 | |
1387 | /// Zero extend to a new width. |
1388 | /// |
1389 | /// This operation zero extends the APInt to a new width. The high order bits |
1390 | /// are filled with 0 bits. It is an error to specify a width that is less |
1391 | /// than or equal to the current width. |
1392 | APInt zext(unsigned width) const; |
1393 | |
1394 | /// Sign extend or truncate to width |
1395 | /// |
1396 | /// Make this APInt have the bit width given by \p width. The value is sign |
1397 | /// extended, truncated, or left alone to make it that width. |
1398 | APInt sextOrTrunc(unsigned width) const; |
1399 | |
1400 | /// Zero extend or truncate to width |
1401 | /// |
1402 | /// Make this APInt have the bit width given by \p width. The value is zero |
1403 | /// extended, truncated, or left alone to make it that width. |
1404 | APInt zextOrTrunc(unsigned width) const; |
1405 | |
1406 | /// Truncate to width |
1407 | /// |
1408 | /// Make this APInt have the bit width given by \p width. The value is |
1409 | /// truncated or left alone to make it that width. |
1410 | APInt truncOrSelf(unsigned width) const; |
1411 | |
1412 | /// Sign extend or truncate to width |
1413 | /// |
1414 | /// Make this APInt have the bit width given by \p width. The value is sign |
1415 | /// extended, or left alone to make it that width. |
1416 | APInt sextOrSelf(unsigned width) const; |
1417 | |
1418 | /// Zero extend or truncate to width |
1419 | /// |
1420 | /// Make this APInt have the bit width given by \p width. The value is zero |
1421 | /// extended, or left alone to make it that width. |
1422 | APInt zextOrSelf(unsigned width) const; |
1423 | |
1424 | /// @} |
1425 | /// \name Bit Manipulation Operators |
1426 | /// @{ |
1427 | |
1428 | /// Set every bit to 1. |
1429 | void setAllBits() { |
1430 | if (isSingleWord()) |
1431 | U.VAL = WORDTYPE_MAX; |
1432 | else |
1433 | // Set all the bits in all the words. |
1434 | memset(U.pVal, -1, getNumWords() * APINT_WORD_SIZE); |
1435 | // Clear the unused ones |
1436 | clearUnusedBits(); |
1437 | } |
1438 | |
1439 | /// Set a given bit to 1. |
1440 | /// |
1441 | /// Set the given bit to 1 whose position is given as "bitPosition". |
1442 | void setBit(unsigned BitPosition) { |
1443 | assert(BitPosition < BitWidth && "BitPosition out of range")(static_cast<void> (0)); |
1444 | WordType Mask = maskBit(BitPosition); |
1445 | if (isSingleWord()) |
1446 | U.VAL |= Mask; |
1447 | else |
1448 | U.pVal[whichWord(BitPosition)] |= Mask; |
1449 | } |
1450 | |
1451 | /// Set the sign bit to 1. |
1452 | void setSignBit() { |
1453 | setBit(BitWidth - 1); |
1454 | } |
1455 | |
1456 | /// Set a given bit to a given value. |
1457 | void setBitVal(unsigned BitPosition, bool BitValue) { |
1458 | if (BitValue) |
1459 | setBit(BitPosition); |
1460 | else |
1461 | clearBit(BitPosition); |
1462 | } |
1463 | |
1464 | /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1. |
1465 | /// This function handles "wrap" case when \p loBit >= \p hiBit, and calls |
1466 | /// setBits when \p loBit < \p hiBit. |
1467 | /// For \p loBit == \p hiBit wrap case, set every bit to 1. |
1468 | void setBitsWithWrap(unsigned loBit, unsigned hiBit) { |
1469 | assert(hiBit <= BitWidth && "hiBit out of range")(static_cast<void> (0)); |
1470 | assert(loBit <= BitWidth && "loBit out of range")(static_cast<void> (0)); |
1471 | if (loBit < hiBit) { |
1472 | setBits(loBit, hiBit); |
1473 | return; |
1474 | } |
1475 | setLowBits(hiBit); |
1476 | setHighBits(BitWidth - loBit); |
1477 | } |
1478 | |
1479 | /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1. |
1480 | /// This function handles case when \p loBit <= \p hiBit. |
1481 | void setBits(unsigned loBit, unsigned hiBit) { |
1482 | assert(hiBit <= BitWidth && "hiBit out of range")(static_cast<void> (0)); |
1483 | assert(loBit <= BitWidth && "loBit out of range")(static_cast<void> (0)); |
1484 | assert(loBit <= hiBit && "loBit greater than hiBit")(static_cast<void> (0)); |
1485 | if (loBit == hiBit) |
1486 | return; |
1487 | if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) { |
1488 | uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit)); |
1489 | mask <<= loBit; |
1490 | if (isSingleWord()) |
1491 | U.VAL |= mask; |
1492 | else |
1493 | U.pVal[0] |= mask; |
1494 | } else { |
1495 | setBitsSlowCase(loBit, hiBit); |
1496 | } |
1497 | } |
1498 | |
1499 | /// Set the top bits starting from loBit. |
1500 | void setBitsFrom(unsigned loBit) { |
1501 | return setBits(loBit, BitWidth); |
1502 | } |
1503 | |
1504 | /// Set the bottom loBits bits. |
1505 | void setLowBits(unsigned loBits) { |
1506 | return setBits(0, loBits); |
1507 | } |
1508 | |
1509 | /// Set the top hiBits bits. |
1510 | void setHighBits(unsigned hiBits) { |
1511 | return setBits(BitWidth - hiBits, BitWidth); |
1512 | } |
1513 | |
1514 | /// Set every bit to 0. |
1515 | void clearAllBits() { |
1516 | if (isSingleWord()) |
1517 | U.VAL = 0; |
1518 | else |
1519 | memset(U.pVal, 0, getNumWords() * APINT_WORD_SIZE); |
1520 | } |
1521 | |
1522 | /// Set a given bit to 0. |
1523 | /// |
1524 | /// Set the given bit to 0 whose position is given as "bitPosition". |
1525 | void clearBit(unsigned BitPosition) { |
1526 | assert(BitPosition < BitWidth && "BitPosition out of range")(static_cast<void> (0)); |
1527 | WordType Mask = ~maskBit(BitPosition); |
1528 | if (isSingleWord()) |
1529 | U.VAL &= Mask; |
1530 | else |
1531 | U.pVal[whichWord(BitPosition)] &= Mask; |
1532 | } |
1533 | |
1534 | /// Set bottom loBits bits to 0. |
1535 | void clearLowBits(unsigned loBits) { |
1536 | assert(loBits <= BitWidth && "More bits than bitwidth")(static_cast<void> (0)); |
1537 | APInt Keep = getHighBitsSet(BitWidth, BitWidth - loBits); |
1538 | *this &= Keep; |
1539 | } |
1540 | |
1541 | /// Set the sign bit to 0. |
1542 | void clearSignBit() { |
1543 | clearBit(BitWidth - 1); |
1544 | } |
1545 | |
1546 | /// Toggle every bit to its opposite value. |
1547 | void flipAllBits() { |
1548 | if (isSingleWord()) { |
1549 | U.VAL ^= WORDTYPE_MAX; |
1550 | clearUnusedBits(); |
1551 | } else { |
1552 | flipAllBitsSlowCase(); |
1553 | } |
1554 | } |
1555 | |
1556 | /// Toggles a given bit to its opposite value. |
1557 | /// |
1558 | /// Toggle a given bit to its opposite value whose position is given |
1559 | /// as "bitPosition". |
1560 | void flipBit(unsigned bitPosition); |
1561 | |
1562 | /// Negate this APInt in place. |
1563 | void negate() { |
1564 | flipAllBits(); |
1565 | ++(*this); |
1566 | } |
1567 | |
1568 | /// Insert the bits from a smaller APInt starting at bitPosition. |
1569 | void insertBits(const APInt &SubBits, unsigned bitPosition); |
1570 | void insertBits(uint64_t SubBits, unsigned bitPosition, unsigned numBits); |
1571 | |
1572 | /// Return an APInt with the extracted bits [bitPosition,bitPosition+numBits). |
1573 | APInt extractBits(unsigned numBits, unsigned bitPosition) const; |
1574 | uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const; |
1575 | |
1576 | /// @} |
1577 | /// \name Value Characterization Functions |
1578 | /// @{ |
1579 | |
1580 | /// Return the number of bits in the APInt. |
1581 | unsigned getBitWidth() const { return BitWidth; } |
1582 | |
1583 | /// Get the number of words. |
1584 | /// |
1585 | /// Here one word's bitwidth equals to that of uint64_t. |
1586 | /// |
1587 | /// \returns the number of words to hold the integer value of this APInt. |
1588 | unsigned getNumWords() const { return getNumWords(BitWidth); } |
1589 | |
1590 | /// Get the number of words. |
1591 | /// |
1592 | /// *NOTE* Here one word's bitwidth equals to that of uint64_t. |
1593 | /// |
1594 | /// \returns the number of words to hold the integer value with a given bit |
1595 | /// width. |
1596 | static unsigned getNumWords(unsigned BitWidth) { |
1597 | return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; |
1598 | } |
1599 | |
1600 | /// Compute the number of active bits in the value |
1601 | /// |
1602 | /// This function returns the number of active bits which is defined as the |
1603 | /// bit width minus the number of leading zeros. This is used in several |
1604 | /// computations to see how "wide" the value is. |
1605 | unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); } |
1606 | |
1607 | /// Compute the number of active words in the value of this APInt. |
1608 | /// |
1609 | /// This is used in conjunction with getActiveData to extract the raw value of |
1610 | /// the APInt. |
1611 | unsigned getActiveWords() const { |
1612 | unsigned numActiveBits = getActiveBits(); |
1613 | return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1; |
1614 | } |
1615 | |
1616 | /// Get the minimum bit size for this signed APInt |
1617 | /// |
1618 | /// Computes the minimum bit width for this APInt while considering it to be a |
1619 | /// signed (and probably negative) value. If the value is not negative, this |
1620 | /// function returns the same value as getActiveBits()+1. Otherwise, it |
1621 | /// returns the smallest bit width that will retain the negative value. For |
1622 | /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so |
1623 | /// for -1, this function will always return 1. |
1624 | unsigned getMinSignedBits() const { return BitWidth - getNumSignBits() + 1; } |
1625 | |
1626 | /// Get zero extended value |
1627 | /// |
1628 | /// This method attempts to return the value of this APInt as a zero extended |
1629 | /// uint64_t. The bitwidth must be <= 64 or the value must fit within a |
1630 | /// uint64_t. Otherwise an assertion will result. |
1631 | uint64_t getZExtValue() const { |
1632 | if (isSingleWord()) |
1633 | return U.VAL; |
1634 | assert(getActiveBits() <= 64 && "Too many bits for uint64_t")(static_cast<void> (0)); |
1635 | return U.pVal[0]; |
1636 | } |
1637 | |
1638 | /// Get sign extended value |
1639 | /// |
1640 | /// This method attempts to return the value of this APInt as a sign extended |
1641 | /// int64_t. The bit width must be <= 64 or the value must fit within an |
1642 | /// int64_t. Otherwise an assertion will result. |
1643 | int64_t getSExtValue() const { |
1644 | if (isSingleWord()) |
1645 | return SignExtend64(U.VAL, BitWidth); |
1646 | assert(getMinSignedBits() <= 64 && "Too many bits for int64_t")(static_cast<void> (0)); |
1647 | return int64_t(U.pVal[0]); |
1648 | } |
1649 | |
1650 | /// Get bits required for string value. |
1651 | /// |
1652 | /// This method determines how many bits are required to hold the APInt |
1653 | /// equivalent of the string given by \p str. |
1654 | static unsigned getBitsNeeded(StringRef str, uint8_t radix); |
1655 | |
1656 | /// The APInt version of the countLeadingZeros functions in |
1657 | /// MathExtras.h. |
1658 | /// |
1659 | /// It counts the number of zeros from the most significant bit to the first |
1660 | /// one bit. |
1661 | /// |
1662 | /// \returns BitWidth if the value is zero, otherwise returns the number of |
1663 | /// zeros from the most significant bit to the first one bits. |
1664 | unsigned countLeadingZeros() const { |
1665 | if (isSingleWord()) { |
1666 | unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth; |
1667 | return llvm::countLeadingZeros(U.VAL) - unusedBits; |
1668 | } |
1669 | return countLeadingZerosSlowCase(); |
1670 | } |
1671 | |
1672 | /// Count the number of leading one bits. |
1673 | /// |
1674 | /// This function is an APInt version of the countLeadingOnes |
1675 | /// functions in MathExtras.h. It counts the number of ones from the most |
1676 | /// significant bit to the first zero bit. |
1677 | /// |
1678 | /// \returns 0 if the high order bit is not set, otherwise returns the number |
1679 | /// of 1 bits from the most significant to the least |
1680 | unsigned countLeadingOnes() const { |
1681 | if (isSingleWord()) |
1682 | return llvm::countLeadingOnes(U.VAL << (APINT_BITS_PER_WORD - BitWidth)); |
1683 | return countLeadingOnesSlowCase(); |
1684 | } |
1685 | |
1686 | /// Computes the number of leading bits of this APInt that are equal to its |
1687 | /// sign bit. |
1688 | unsigned getNumSignBits() const { |
1689 | return isNegative() ? countLeadingOnes() : countLeadingZeros(); |
1690 | } |
1691 | |
1692 | /// Count the number of trailing zero bits. |
1693 | /// |
1694 | /// This function is an APInt version of the countTrailingZeros |
1695 | /// functions in MathExtras.h. It counts the number of zeros from the least |
1696 | /// significant bit to the first set bit. |
1697 | /// |
1698 | /// \returns BitWidth if the value is zero, otherwise returns the number of |
1699 | /// zeros from the least significant bit to the first one bit. |
1700 | unsigned countTrailingZeros() const { |
1701 | if (isSingleWord()) { |
1702 | unsigned TrailingZeros = llvm::countTrailingZeros(U.VAL); |
1703 | return (TrailingZeros > BitWidth ? BitWidth : TrailingZeros); |
1704 | } |
1705 | return countTrailingZerosSlowCase(); |
1706 | } |
1707 | |
1708 | /// Count the number of trailing one bits. |
1709 | /// |
1710 | /// This function is an APInt version of the countTrailingOnes |
1711 | /// functions in MathExtras.h. It counts the number of ones from the least |
1712 | /// significant bit to the first zero bit. |
1713 | /// |
1714 | /// \returns BitWidth if the value is all ones, otherwise returns the number |
1715 | /// of ones from the least significant bit to the first zero bit. |
1716 | unsigned countTrailingOnes() const { |
1717 | if (isSingleWord()) |
1718 | return llvm::countTrailingOnes(U.VAL); |
1719 | return countTrailingOnesSlowCase(); |
1720 | } |
1721 | |
1722 | /// Count the number of bits set. |
1723 | /// |
1724 | /// This function is an APInt version of the countPopulation functions |
1725 | /// in MathExtras.h. It counts the number of 1 bits in the APInt value. |
1726 | /// |
1727 | /// \returns 0 if the value is zero, otherwise returns the number of set bits. |
1728 | unsigned countPopulation() const { |
1729 | if (isSingleWord()) |
1730 | return llvm::countPopulation(U.VAL); |
1731 | return countPopulationSlowCase(); |
1732 | } |
1733 | |
1734 | /// @} |
1735 | /// \name Conversion Functions |
1736 | /// @{ |
1737 | void print(raw_ostream &OS, bool isSigned) const; |
1738 | |
1739 | /// Converts an APInt to a string and append it to Str. Str is commonly a |
1740 | /// SmallString. |
1741 | void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed, |
1742 | bool formatAsCLiteral = false) const; |
1743 | |
1744 | /// Considers the APInt to be unsigned and converts it into a string in the |
1745 | /// radix given. The radix can be 2, 8, 10 16, or 36. |
1746 | void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { |
1747 | toString(Str, Radix, false, false); |
1748 | } |
1749 | |
1750 | /// Considers the APInt to be signed and converts it into a string in the |
1751 | /// radix given. The radix can be 2, 8, 10, 16, or 36. |
1752 | void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const { |
1753 | toString(Str, Radix, true, false); |
1754 | } |
1755 | |
1756 | /// \returns a byte-swapped representation of this APInt Value. |
1757 | APInt byteSwap() const; |
1758 | |
1759 | /// \returns the value with the bit representation reversed of this APInt |
1760 | /// Value. |
1761 | APInt reverseBits() const; |
1762 | |
1763 | /// Converts this APInt to a double value. |
1764 | double roundToDouble(bool isSigned) const; |
1765 | |
1766 | /// Converts this unsigned APInt to a double value. |
1767 | double roundToDouble() const { return roundToDouble(false); } |
1768 | |
1769 | /// Converts this signed APInt to a double value. |
1770 | double signedRoundToDouble() const { return roundToDouble(true); } |
1771 | |
1772 | /// Converts APInt bits to a double |
1773 | /// |
1774 | /// The conversion does not do a translation from integer to double, it just |
1775 | /// re-interprets the bits as a double. Note that it is valid to do this on |
1776 | /// any bit width. Exactly 64 bits will be translated. |
1777 | double bitsToDouble() const { |
1778 | return BitsToDouble(getWord(0)); |
1779 | } |
1780 | |
1781 | /// Converts APInt bits to a float |
1782 | /// |
1783 | /// The conversion does not do a translation from integer to float, it just |
1784 | /// re-interprets the bits as a float. Note that it is valid to do this on |
1785 | /// any bit width. Exactly 32 bits will be translated. |
1786 | float bitsToFloat() const { |
1787 | return BitsToFloat(static_cast<uint32_t>(getWord(0))); |
1788 | } |
1789 | |
1790 | /// Converts a double to APInt bits. |
1791 | /// |
1792 | /// The conversion does not do a translation from double to integer, it just |
1793 | /// re-interprets the bits of the double. |
1794 | static APInt doubleToBits(double V) { |
1795 | return APInt(sizeof(double) * CHAR_BIT8, DoubleToBits(V)); |
1796 | } |
1797 | |
1798 | /// Converts a float to APInt bits. |
1799 | /// |
1800 | /// The conversion does not do a translation from float to integer, it just |
1801 | /// re-interprets the bits of the float. |
1802 | static APInt floatToBits(float V) { |
1803 | return APInt(sizeof(float) * CHAR_BIT8, FloatToBits(V)); |
1804 | } |
1805 | |
1806 | /// @} |
1807 | /// \name Mathematics Operations |
1808 | /// @{ |
1809 | |
1810 | /// \returns the floor log base 2 of this APInt. |
1811 | unsigned logBase2() const { return getActiveBits() - 1; } |
1812 | |
1813 | /// \returns the ceil log base 2 of this APInt. |
1814 | unsigned ceilLogBase2() const { |
1815 | APInt temp(*this); |
1816 | --temp; |
1817 | return temp.getActiveBits(); |
1818 | } |
1819 | |
1820 | /// \returns the nearest log base 2 of this APInt. Ties round up. |
1821 | /// |
1822 | /// NOTE: When we have a BitWidth of 1, we define: |
1823 | /// |
1824 | /// log2(0) = UINT32_MAX |
1825 | /// log2(1) = 0 |
1826 | /// |
1827 | /// to get around any mathematical concerns resulting from |
1828 | /// referencing 2 in a space where 2 does no exist. |
1829 | unsigned nearestLogBase2() const { |
1830 | // Special case when we have a bitwidth of 1. If VAL is 1, then we |
1831 | // get 0. If VAL is 0, we get WORDTYPE_MAX which gets truncated to |
1832 | // UINT32_MAX. |
1833 | if (BitWidth == 1) |
1834 | return U.VAL - 1; |
1835 | |
1836 | // Handle the zero case. |
1837 | if (isNullValue()) |
1838 | return UINT32_MAX(4294967295U); |
1839 | |
1840 | // The non-zero case is handled by computing: |
1841 | // |
1842 | // nearestLogBase2(x) = logBase2(x) + x[logBase2(x)-1]. |
1843 | // |
1844 | // where x[i] is referring to the value of the ith bit of x. |
1845 | unsigned lg = logBase2(); |
1846 | return lg + unsigned((*this)[lg - 1]); |
1847 | } |
1848 | |
1849 | /// \returns the log base 2 of this APInt if its an exact power of two, -1 |
1850 | /// otherwise |
1851 | int32_t exactLogBase2() const { |
1852 | if (!isPowerOf2()) |
1853 | return -1; |
1854 | return logBase2(); |
1855 | } |
1856 | |
1857 | /// Compute the square root |
1858 | APInt sqrt() const; |
1859 | |
1860 | /// Get the absolute value; |
1861 | /// |
1862 | /// If *this is < 0 then return -(*this), otherwise *this; |
1863 | APInt abs() const { |
1864 | if (isNegative()) |
1865 | return -(*this); |
1866 | return *this; |
1867 | } |
1868 | |
1869 | /// \returns the multiplicative inverse for a given modulo. |
1870 | APInt multiplicativeInverse(const APInt &modulo) const; |
1871 | |
1872 | /// @} |
1873 | /// \name Support for division by constant |
1874 | /// @{ |
1875 | |
1876 | /// Calculate the magic number for signed division by a constant. |
1877 | struct ms; |
1878 | ms magic() const; |
1879 | |
1880 | /// Calculate the magic number for unsigned division by a constant. |
1881 | struct mu; |
1882 | mu magicu(unsigned LeadingZeros = 0) const; |
1883 | |
1884 | /// @} |
1885 | /// \name Building-block Operations for APInt and APFloat |
1886 | /// @{ |
1887 | |
1888 | // These building block operations operate on a representation of arbitrary |
1889 | // precision, two's-complement, bignum integer values. They should be |
1890 | // sufficient to implement APInt and APFloat bignum requirements. Inputs are |
1891 | // generally a pointer to the base of an array of integer parts, representing |
1892 | // an unsigned bignum, and a count of how many parts there are. |
1893 | |
1894 | /// Sets the least significant part of a bignum to the input value, and zeroes |
1895 | /// out higher parts. |
1896 | static void tcSet(WordType *, WordType, unsigned); |
1897 | |
1898 | /// Assign one bignum to another. |
1899 | static void tcAssign(WordType *, const WordType *, unsigned); |
1900 | |
1901 | /// Returns true if a bignum is zero, false otherwise. |
1902 | static bool tcIsZero(const WordType *, unsigned); |
1903 | |
1904 | /// Extract the given bit of a bignum; returns 0 or 1. Zero-based. |
1905 | static int tcExtractBit(const WordType *, unsigned bit); |
1906 | |
1907 | /// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to |
1908 | /// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least |
1909 | /// significant bit of DST. All high bits above srcBITS in DST are |
1910 | /// zero-filled. |
1911 | static void tcExtract(WordType *, unsigned dstCount, |
1912 | const WordType *, unsigned srcBits, |
1913 | unsigned srcLSB); |
1914 | |
1915 | /// Set the given bit of a bignum. Zero-based. |
1916 | static void tcSetBit(WordType *, unsigned bit); |
1917 | |
1918 | /// Clear the given bit of a bignum. Zero-based. |
1919 | static void tcClearBit(WordType *, unsigned bit); |
1920 | |
1921 | /// Returns the bit number of the least or most significant set bit of a |
1922 | /// number. If the input number has no bits set -1U is returned. |
1923 | static unsigned tcLSB(const WordType *, unsigned n); |
1924 | static unsigned tcMSB(const WordType *parts, unsigned n); |
1925 | |
1926 | /// Negate a bignum in-place. |
1927 | static void tcNegate(WordType *, unsigned); |
1928 | |
1929 | /// DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag. |
1930 | static WordType tcAdd(WordType *, const WordType *, |
1931 | WordType carry, unsigned); |
1932 | /// DST += RHS. Returns the carry flag. |
1933 | static WordType tcAddPart(WordType *, WordType, unsigned); |
1934 | |
1935 | /// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag. |
1936 | static WordType tcSubtract(WordType *, const WordType *, |
1937 | WordType carry, unsigned); |
1938 | /// DST -= RHS. Returns the carry flag. |
1939 | static WordType tcSubtractPart(WordType *, WordType, unsigned); |
1940 | |
1941 | /// DST += SRC * MULTIPLIER + PART if add is true |
1942 | /// DST = SRC * MULTIPLIER + PART if add is false |
1943 | /// |
1944 | /// Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC they must |
1945 | /// start at the same point, i.e. DST == SRC. |
1946 | /// |
1947 | /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned. |
1948 | /// Otherwise DST is filled with the least significant DSTPARTS parts of the |
1949 | /// result, and if all of the omitted higher parts were zero return zero, |
1950 | /// otherwise overflow occurred and return one. |
1951 | static int tcMultiplyPart(WordType *dst, const WordType *src, |
1952 | WordType multiplier, WordType carry, |
1953 | unsigned srcParts, unsigned dstParts, |
1954 | bool add); |
1955 | |
1956 | /// DST = LHS * RHS, where DST has the same width as the operands and is |
1957 | /// filled with the least significant parts of the result. Returns one if |
1958 | /// overflow occurred, otherwise zero. DST must be disjoint from both |
1959 | /// operands. |
1960 | static int tcMultiply(WordType *, const WordType *, const WordType *, |
1961 | unsigned); |
1962 | |
1963 | /// DST = LHS * RHS, where DST has width the sum of the widths of the |
1964 | /// operands. No overflow occurs. DST must be disjoint from both operands. |
1965 | static void tcFullMultiply(WordType *, const WordType *, |
1966 | const WordType *, unsigned, unsigned); |
1967 | |
1968 | /// If RHS is zero LHS and REMAINDER are left unchanged, return one. |
1969 | /// Otherwise set LHS to LHS / RHS with the fractional part discarded, set |
1970 | /// REMAINDER to the remainder, return zero. i.e. |
1971 | /// |
1972 | /// OLD_LHS = RHS * LHS + REMAINDER |
1973 | /// |
1974 | /// SCRATCH is a bignum of the same size as the operands and result for use by |
1975 | /// the routine; its contents need not be initialized and are destroyed. LHS, |
1976 | /// REMAINDER and SCRATCH must be distinct. |
1977 | static int tcDivide(WordType *lhs, const WordType *rhs, |
1978 | WordType *remainder, WordType *scratch, |
1979 | unsigned parts); |
1980 | |
1981 | /// Shift a bignum left Count bits. Shifted in bits are zero. There are no |
1982 | /// restrictions on Count. |
1983 | static void tcShiftLeft(WordType *, unsigned Words, unsigned Count); |
1984 | |
1985 | /// Shift a bignum right Count bits. Shifted in bits are zero. There are no |
1986 | /// restrictions on Count. |
1987 | static void tcShiftRight(WordType *, unsigned Words, unsigned Count); |
1988 | |
1989 | /// The obvious AND, OR and XOR and complement operations. |
1990 | static void tcAnd(WordType *, const WordType *, unsigned); |
1991 | static void tcOr(WordType *, const WordType *, unsigned); |
1992 | static void tcXor(WordType *, const WordType *, unsigned); |
1993 | static void tcComplement(WordType *, unsigned); |
1994 | |
1995 | /// Comparison (unsigned) of two bignums. |
1996 | static int tcCompare(const WordType *, const WordType *, unsigned); |
1997 | |
1998 | /// Increment a bignum in-place. Return the carry flag. |
1999 | static WordType tcIncrement(WordType *dst, unsigned parts) { |
2000 | return tcAddPart(dst, 1, parts); |
2001 | } |
2002 | |
2003 | /// Decrement a bignum in-place. Return the borrow flag. |
2004 | static WordType tcDecrement(WordType *dst, unsigned parts) { |
2005 | return tcSubtractPart(dst, 1, parts); |
2006 | } |
2007 | |
2008 | /// Set the least significant BITS and clear the rest. |
2009 | static void tcSetLeastSignificantBits(WordType *, unsigned, unsigned bits); |
2010 | |
2011 | /// debug method |
2012 | void dump() const; |
2013 | |
2014 | /// @} |
2015 | }; |
2016 | |
2017 | /// Magic data for optimising signed division by a constant. |
2018 | struct APInt::ms { |
2019 | APInt m; ///< magic number |
2020 | unsigned s; ///< shift amount |
2021 | }; |
2022 | |
2023 | /// Magic data for optimising unsigned division by a constant. |
2024 | struct APInt::mu { |
2025 | APInt m; ///< magic number |
2026 | bool a; ///< add indicator |
2027 | unsigned s; ///< shift amount |
2028 | }; |
2029 | |
2030 | inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; } |
2031 | |
2032 | inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; } |
2033 | |
2034 | /// Unary bitwise complement operator. |
2035 | /// |
2036 | /// \returns an APInt that is the bitwise complement of \p v. |
2037 | inline APInt operator~(APInt v) { |
2038 | v.flipAllBits(); |
2039 | return v; |
2040 | } |
2041 | |
2042 | inline APInt operator&(APInt a, const APInt &b) { |
2043 | a &= b; |
2044 | return a; |
2045 | } |
2046 | |
2047 | inline APInt operator&(const APInt &a, APInt &&b) { |
2048 | b &= a; |
2049 | return std::move(b); |
2050 | } |
2051 | |
2052 | inline APInt operator&(APInt a, uint64_t RHS) { |
2053 | a &= RHS; |
2054 | return a; |
2055 | } |
2056 | |
2057 | inline APInt operator&(uint64_t LHS, APInt b) { |
2058 | b &= LHS; |
2059 | return b; |
2060 | } |
2061 | |
2062 | inline APInt operator|(APInt a, const APInt &b) { |
2063 | a |= b; |
2064 | return a; |
2065 | } |
2066 | |
2067 | inline APInt operator|(const APInt &a, APInt &&b) { |
2068 | b |= a; |
2069 | return std::move(b); |
2070 | } |
2071 | |
2072 | inline APInt operator|(APInt a, uint64_t RHS) { |
2073 | a |= RHS; |
2074 | return a; |
2075 | } |
2076 | |
2077 | inline APInt operator|(uint64_t LHS, APInt b) { |
2078 | b |= LHS; |
2079 | return b; |
2080 | } |
2081 | |
2082 | inline APInt operator^(APInt a, const APInt &b) { |
2083 | a ^= b; |
2084 | return a; |
2085 | } |
2086 | |
2087 | inline APInt operator^(const APInt &a, APInt &&b) { |
2088 | b ^= a; |
2089 | return std::move(b); |
2090 | } |
2091 | |
2092 | inline APInt operator^(APInt a, uint64_t RHS) { |
2093 | a ^= RHS; |
2094 | return a; |
2095 | } |
2096 | |
2097 | inline APInt operator^(uint64_t LHS, APInt b) { |
2098 | b ^= LHS; |
2099 | return b; |
2100 | } |
2101 | |
2102 | inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) { |
2103 | I.print(OS, true); |
2104 | return OS; |
2105 | } |
2106 | |
2107 | inline APInt operator-(APInt v) { |
2108 | v.negate(); |
2109 | return v; |
2110 | } |
2111 | |
2112 | inline APInt operator+(APInt a, const APInt &b) { |
2113 | a += b; |
2114 | return a; |
2115 | } |
2116 | |
2117 | inline APInt operator+(const APInt &a, APInt &&b) { |
2118 | b += a; |
2119 | return std::move(b); |
2120 | } |
2121 | |
2122 | inline APInt operator+(APInt a, uint64_t RHS) { |
2123 | a += RHS; |
2124 | return a; |
2125 | } |
2126 | |
2127 | inline APInt operator+(uint64_t LHS, APInt b) { |
2128 | b += LHS; |
2129 | return b; |
2130 | } |
2131 | |
2132 | inline APInt operator-(APInt a, const APInt &b) { |
2133 | a -= b; |
2134 | return a; |
2135 | } |
2136 | |
2137 | inline APInt operator-(const APInt &a, APInt &&b) { |
2138 | b.negate(); |
2139 | b += a; |
2140 | return std::move(b); |
2141 | } |
2142 | |
2143 | inline APInt operator-(APInt a, uint64_t RHS) { |
2144 | a -= RHS; |
2145 | return a; |
2146 | } |
2147 | |
2148 | inline APInt operator-(uint64_t LHS, APInt b) { |
2149 | b.negate(); |
2150 | b += LHS; |
2151 | return b; |
2152 | } |
2153 | |
2154 | inline APInt operator*(APInt a, uint64_t RHS) { |
2155 | a *= RHS; |
2156 | return a; |
2157 | } |
2158 | |
2159 | inline APInt operator*(uint64_t LHS, APInt b) { |
2160 | b *= LHS; |
2161 | return b; |
2162 | } |
2163 | |
2164 | |
2165 | namespace APIntOps { |
2166 | |
2167 | /// Determine the smaller of two APInts considered to be signed. |
2168 | inline const APInt &smin(const APInt &A, const APInt &B) { |
2169 | return A.slt(B) ? A : B; |
2170 | } |
2171 | |
2172 | /// Determine the larger of two APInts considered to be signed. |
2173 | inline const APInt &smax(const APInt &A, const APInt &B) { |
2174 | return A.sgt(B) ? A : B; |
2175 | } |
2176 | |
2177 | /// Determine the smaller of two APInts considered to be unsigned. |
2178 | inline const APInt &umin(const APInt &A, const APInt &B) { |
2179 | return A.ult(B) ? A : B; |
2180 | } |
2181 | |
2182 | /// Determine the larger of two APInts considered to be unsigned. |
2183 | inline const APInt &umax(const APInt &A, const APInt &B) { |
2184 | return A.ugt(B) ? A : B; |
2185 | } |
2186 | |
2187 | /// Compute GCD of two unsigned APInt values. |
2188 | /// |
2189 | /// This function returns the greatest common divisor of the two APInt values |
2190 | /// using Stein's algorithm. |
2191 | /// |
2192 | /// \returns the greatest common divisor of A and B. |
2193 | APInt GreatestCommonDivisor(APInt A, APInt B); |
2194 | |
2195 | /// Converts the given APInt to a double value. |
2196 | /// |
2197 | /// Treats the APInt as an unsigned value for conversion purposes. |
2198 | inline double RoundAPIntToDouble(const APInt &APIVal) { |
2199 | return APIVal.roundToDouble(); |
2200 | } |
2201 | |
2202 | /// Converts the given APInt to a double value. |
2203 | /// |
2204 | /// Treats the APInt as a signed value for conversion purposes. |
2205 | inline double RoundSignedAPIntToDouble(const APInt &APIVal) { |
2206 | return APIVal.signedRoundToDouble(); |
2207 | } |
2208 | |
2209 | /// Converts the given APInt to a float value. |
2210 | inline float RoundAPIntToFloat(const APInt &APIVal) { |
2211 | return float(RoundAPIntToDouble(APIVal)); |
2212 | } |
2213 | |
2214 | /// Converts the given APInt to a float value. |
2215 | /// |
2216 | /// Treats the APInt as a signed value for conversion purposes. |
2217 | inline float RoundSignedAPIntToFloat(const APInt &APIVal) { |
2218 | return float(APIVal.signedRoundToDouble()); |
2219 | } |
2220 | |
2221 | /// Converts the given double value into a APInt. |
2222 | /// |
2223 | /// This function convert a double value to an APInt value. |
2224 | APInt RoundDoubleToAPInt(double Double, unsigned width); |
2225 | |
2226 | /// Converts a float value into a APInt. |
2227 | /// |
2228 | /// Converts a float value into an APInt value. |
2229 | inline APInt RoundFloatToAPInt(float Float, unsigned width) { |
2230 | return RoundDoubleToAPInt(double(Float), width); |
2231 | } |
2232 | |
2233 | /// Return A unsign-divided by B, rounded by the given rounding mode. |
2234 | APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM); |
2235 | |
2236 | /// Return A sign-divided by B, rounded by the given rounding mode. |
2237 | APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM); |
2238 | |
2239 | /// Let q(n) = An^2 + Bn + C, and BW = bit width of the value range |
2240 | /// (e.g. 32 for i32). |
2241 | /// This function finds the smallest number n, such that |
2242 | /// (a) n >= 0 and q(n) = 0, or |
2243 | /// (b) n >= 1 and q(n-1) and q(n), when evaluated in the set of all |
2244 | /// integers, belong to two different intervals [Rk, Rk+R), |
2245 | /// where R = 2^BW, and k is an integer. |
2246 | /// The idea here is to find when q(n) "overflows" 2^BW, while at the |
2247 | /// same time "allowing" subtraction. In unsigned modulo arithmetic a |
2248 | /// subtraction (treated as addition of negated numbers) would always |
2249 | /// count as an overflow, but here we want to allow values to decrease |
2250 | /// and increase as long as they are within the same interval. |
2251 | /// Specifically, adding of two negative numbers should not cause an |
2252 | /// overflow (as long as the magnitude does not exceed the bit width). |
2253 | /// On the other hand, given a positive number, adding a negative |
2254 | /// number to it can give a negative result, which would cause the |
2255 | /// value to go from [-2^BW, 0) to [0, 2^BW). In that sense, zero is |
2256 | /// treated as a special case of an overflow. |
2257 | /// |
2258 | /// This function returns None if after finding k that minimizes the |
2259 | /// positive solution to q(n) = kR, both solutions are contained between |
2260 | /// two consecutive integers. |
2261 | /// |
2262 | /// There are cases where q(n) > T, and q(n+1) < T (assuming evaluation |
2263 | /// in arithmetic modulo 2^BW, and treating the values as signed) by the |
2264 | /// virtue of *signed* overflow. This function will *not* find such an n, |
2265 | /// however it may find a value of n satisfying the inequalities due to |
2266 | /// an *unsigned* overflow (if the values are treated as unsigned). |
2267 | /// To find a solution for a signed overflow, treat it as a problem of |
2268 | /// finding an unsigned overflow with a range with of BW-1. |
2269 | /// |
2270 | /// The returned value may have a different bit width from the input |
2271 | /// coefficients. |
2272 | Optional<APInt> SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, |
2273 | unsigned RangeWidth); |
2274 | |
2275 | /// Compare two values, and if they are different, return the position of the |
2276 | /// most significant bit that is different in the values. |
2277 | Optional<unsigned> GetMostSignificantDifferentBit(const APInt &A, |
2278 | const APInt &B); |
2279 | |
2280 | } // End of APIntOps namespace |
2281 | |
2282 | // See friend declaration above. This additional declaration is required in |
2283 | // order to compile LLVM with IBM xlC compiler. |
2284 | hash_code hash_value(const APInt &Arg); |
2285 | |
2286 | /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst |
2287 | /// with the integer held in IntVal. |
2288 | void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes); |
2289 | |
2290 | /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting |
2291 | /// from Src into IntVal, which is assumed to be wide enough and to hold zero. |
2292 | void LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, unsigned LoadBytes); |
2293 | |
2294 | /// Provide DenseMapInfo for APInt. |
2295 | template <> struct DenseMapInfo<APInt> { |
2296 | static inline APInt getEmptyKey() { |
2297 | APInt V(nullptr, 0); |
2298 | V.U.VAL = 0; |
2299 | return V; |
2300 | } |
2301 | |
2302 | static inline APInt getTombstoneKey() { |
2303 | APInt V(nullptr, 0); |
2304 | V.U.VAL = 1; |
2305 | return V; |
2306 | } |
2307 | |
2308 | static unsigned getHashValue(const APInt &Key); |
2309 | |
2310 | static bool isEqual(const APInt &LHS, const APInt &RHS) { |
2311 | return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS; |
2312 | } |
2313 | }; |
2314 | |
2315 | } // namespace llvm |
2316 | |
2317 | #endif |
1 | //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file contains some functions that are useful for math stuff. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #ifndef LLVM_SUPPORT_MATHEXTRAS_H | |||
14 | #define LLVM_SUPPORT_MATHEXTRAS_H | |||
15 | ||||
16 | #include "llvm/Support/Compiler.h" | |||
17 | #include <cassert> | |||
18 | #include <climits> | |||
19 | #include <cmath> | |||
20 | #include <cstdint> | |||
21 | #include <cstring> | |||
22 | #include <limits> | |||
23 | #include <type_traits> | |||
24 | ||||
25 | #ifdef __ANDROID_NDK__ | |||
26 | #include <android/api-level.h> | |||
27 | #endif | |||
28 | ||||
29 | #ifdef _MSC_VER | |||
30 | // Declare these intrinsics manually rather including intrin.h. It's very | |||
31 | // expensive, and MathExtras.h is popular. | |||
32 | // #include <intrin.h> | |||
33 | extern "C" { | |||
34 | unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask); | |||
35 | unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask); | |||
36 | unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask); | |||
37 | unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask); | |||
38 | } | |||
39 | #endif | |||
40 | ||||
41 | namespace llvm { | |||
42 | ||||
43 | /// The behavior an operation has on an input of 0. | |||
44 | enum ZeroBehavior { | |||
45 | /// The returned value is undefined. | |||
46 | ZB_Undefined, | |||
47 | /// The returned value is numeric_limits<T>::max() | |||
48 | ZB_Max, | |||
49 | /// The returned value is numeric_limits<T>::digits | |||
50 | ZB_Width | |||
51 | }; | |||
52 | ||||
53 | /// Mathematical constants. | |||
54 | namespace numbers { | |||
55 | // TODO: Track C++20 std::numbers. | |||
56 | // TODO: Favor using the hexadecimal FP constants (requires C++17). | |||
57 | constexpr double e = 2.7182818284590452354, // (0x1.5bf0a8b145749P+1) https://oeis.org/A001113 | |||
58 | egamma = .57721566490153286061, // (0x1.2788cfc6fb619P-1) https://oeis.org/A001620 | |||
59 | ln2 = .69314718055994530942, // (0x1.62e42fefa39efP-1) https://oeis.org/A002162 | |||
60 | ln10 = 2.3025850929940456840, // (0x1.24bb1bbb55516P+1) https://oeis.org/A002392 | |||
61 | log2e = 1.4426950408889634074, // (0x1.71547652b82feP+0) | |||
62 | log10e = .43429448190325182765, // (0x1.bcb7b1526e50eP-2) | |||
63 | pi = 3.1415926535897932385, // (0x1.921fb54442d18P+1) https://oeis.org/A000796 | |||
64 | inv_pi = .31830988618379067154, // (0x1.45f306bc9c883P-2) https://oeis.org/A049541 | |||
65 | sqrtpi = 1.7724538509055160273, // (0x1.c5bf891b4ef6bP+0) https://oeis.org/A002161 | |||
66 | inv_sqrtpi = .56418958354775628695, // (0x1.20dd750429b6dP-1) https://oeis.org/A087197 | |||
67 | sqrt2 = 1.4142135623730950488, // (0x1.6a09e667f3bcdP+0) https://oeis.org/A00219 | |||
68 | inv_sqrt2 = .70710678118654752440, // (0x1.6a09e667f3bcdP-1) | |||
69 | sqrt3 = 1.7320508075688772935, // (0x1.bb67ae8584caaP+0) https://oeis.org/A002194 | |||
70 | inv_sqrt3 = .57735026918962576451, // (0x1.279a74590331cP-1) | |||
71 | phi = 1.6180339887498948482; // (0x1.9e3779b97f4a8P+0) https://oeis.org/A001622 | |||
72 | constexpr float ef = 2.71828183F, // (0x1.5bf0a8P+1) https://oeis.org/A001113 | |||
73 | egammaf = .577215665F, // (0x1.2788d0P-1) https://oeis.org/A001620 | |||
74 | ln2f = .693147181F, // (0x1.62e430P-1) https://oeis.org/A002162 | |||
75 | ln10f = 2.30258509F, // (0x1.26bb1cP+1) https://oeis.org/A002392 | |||
76 | log2ef = 1.44269504F, // (0x1.715476P+0) | |||
77 | log10ef = .434294482F, // (0x1.bcb7b2P-2) | |||
78 | pif = 3.14159265F, // (0x1.921fb6P+1) https://oeis.org/A000796 | |||
79 | inv_pif = .318309886F, // (0x1.45f306P-2) https://oeis.org/A049541 | |||
80 | sqrtpif = 1.77245385F, // (0x1.c5bf8aP+0) https://oeis.org/A002161 | |||
81 | inv_sqrtpif = .564189584F, // (0x1.20dd76P-1) https://oeis.org/A087197 | |||
82 | sqrt2f = 1.41421356F, // (0x1.6a09e6P+0) https://oeis.org/A002193 | |||
83 | inv_sqrt2f = .707106781F, // (0x1.6a09e6P-1) | |||
84 | sqrt3f = 1.73205081F, // (0x1.bb67aeP+0) https://oeis.org/A002194 | |||
85 | inv_sqrt3f = .577350269F, // (0x1.279a74P-1) | |||
86 | phif = 1.61803399F; // (0x1.9e377aP+0) https://oeis.org/A001622 | |||
87 | } // namespace numbers | |||
88 | ||||
89 | namespace detail { | |||
90 | template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter { | |||
91 | static unsigned count(T Val, ZeroBehavior) { | |||
92 | if (!Val) | |||
93 | return std::numeric_limits<T>::digits; | |||
94 | if (Val & 0x1) | |||
95 | return 0; | |||
96 | ||||
97 | // Bisection method. | |||
98 | unsigned ZeroBits = 0; | |||
99 | T Shift = std::numeric_limits<T>::digits >> 1; | |||
100 | T Mask = std::numeric_limits<T>::max() >> Shift; | |||
101 | while (Shift) { | |||
102 | if ((Val & Mask) == 0) { | |||
103 | Val >>= Shift; | |||
104 | ZeroBits |= Shift; | |||
105 | } | |||
106 | Shift >>= 1; | |||
107 | Mask >>= Shift; | |||
108 | } | |||
109 | return ZeroBits; | |||
110 | } | |||
111 | }; | |||
112 | ||||
113 | #if defined(__GNUC__4) || defined(_MSC_VER) | |||
114 | template <typename T> struct TrailingZerosCounter<T, 4> { | |||
115 | static unsigned count(T Val, ZeroBehavior ZB) { | |||
116 | if (ZB != ZB_Undefined && Val == 0) | |||
117 | return 32; | |||
118 | ||||
119 | #if __has_builtin(__builtin_ctz)1 || defined(__GNUC__4) | |||
120 | return __builtin_ctz(Val); | |||
121 | #elif defined(_MSC_VER) | |||
122 | unsigned long Index; | |||
123 | _BitScanForward(&Index, Val); | |||
124 | return Index; | |||
125 | #endif | |||
126 | } | |||
127 | }; | |||
128 | ||||
129 | #if !defined(_MSC_VER) || defined(_M_X64) | |||
130 | template <typename T> struct TrailingZerosCounter<T, 8> { | |||
131 | static unsigned count(T Val, ZeroBehavior ZB) { | |||
132 | if (ZB != ZB_Undefined && Val == 0) | |||
133 | return 64; | |||
134 | ||||
135 | #if __has_builtin(__builtin_ctzll)1 || defined(__GNUC__4) | |||
136 | return __builtin_ctzll(Val); | |||
137 | #elif defined(_MSC_VER) | |||
138 | unsigned long Index; | |||
139 | _BitScanForward64(&Index, Val); | |||
140 | return Index; | |||
141 | #endif | |||
142 | } | |||
143 | }; | |||
144 | #endif | |||
145 | #endif | |||
146 | } // namespace detail | |||
147 | ||||
148 | /// Count number of 0's from the least significant bit to the most | |||
149 | /// stopping at the first 1. | |||
150 | /// | |||
151 | /// Only unsigned integral types are allowed. | |||
152 | /// | |||
153 | /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are | |||
154 | /// valid arguments. | |||
155 | template <typename T> | |||
156 | unsigned countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) { | |||
157 | static_assert(std::numeric_limits<T>::is_integer && | |||
158 | !std::numeric_limits<T>::is_signed, | |||
159 | "Only unsigned integral types are allowed."); | |||
160 | return llvm::detail::TrailingZerosCounter<T, sizeof(T)>::count(Val, ZB); | |||
161 | } | |||
162 | ||||
163 | namespace detail { | |||
164 | template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter { | |||
165 | static unsigned count(T Val, ZeroBehavior) { | |||
166 | if (!Val) | |||
167 | return std::numeric_limits<T>::digits; | |||
168 | ||||
169 | // Bisection method. | |||
170 | unsigned ZeroBits = 0; | |||
171 | for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) { | |||
172 | T Tmp = Val >> Shift; | |||
173 | if (Tmp) | |||
174 | Val = Tmp; | |||
175 | else | |||
176 | ZeroBits |= Shift; | |||
177 | } | |||
178 | return ZeroBits; | |||
179 | } | |||
180 | }; | |||
181 | ||||
182 | #if defined(__GNUC__4) || defined(_MSC_VER) | |||
183 | template <typename T> struct LeadingZerosCounter<T, 4> { | |||
184 | static unsigned count(T Val, ZeroBehavior ZB) { | |||
185 | if (ZB != ZB_Undefined && Val == 0) | |||
186 | return 32; | |||
187 | ||||
188 | #if __has_builtin(__builtin_clz)1 || defined(__GNUC__4) | |||
189 | return __builtin_clz(Val); | |||
190 | #elif defined(_MSC_VER) | |||
191 | unsigned long Index; | |||
192 | _BitScanReverse(&Index, Val); | |||
193 | return Index ^ 31; | |||
194 | #endif | |||
195 | } | |||
196 | }; | |||
197 | ||||
198 | #if !defined(_MSC_VER) || defined(_M_X64) | |||
199 | template <typename T> struct LeadingZerosCounter<T, 8> { | |||
200 | static unsigned count(T Val, ZeroBehavior ZB) { | |||
201 | if (ZB != ZB_Undefined && Val == 0) | |||
202 | return 64; | |||
203 | ||||
204 | #if __has_builtin(__builtin_clzll)1 || defined(__GNUC__4) | |||
205 | return __builtin_clzll(Val); | |||
206 | #elif defined(_MSC_VER) | |||
207 | unsigned long Index; | |||
208 | _BitScanReverse64(&Index, Val); | |||
209 | return Index ^ 63; | |||
210 | #endif | |||
211 | } | |||
212 | }; | |||
213 | #endif | |||
214 | #endif | |||
215 | } // namespace detail | |||
216 | ||||
217 | /// Count number of 0's from the most significant bit to the least | |||
218 | /// stopping at the first 1. | |||
219 | /// | |||
220 | /// Only unsigned integral types are allowed. | |||
221 | /// | |||
222 | /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are | |||
223 | /// valid arguments. | |||
224 | template <typename T> | |||
225 | unsigned countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) { | |||
226 | static_assert(std::numeric_limits<T>::is_integer && | |||
227 | !std::numeric_limits<T>::is_signed, | |||
228 | "Only unsigned integral types are allowed."); | |||
229 | return llvm::detail::LeadingZerosCounter<T, sizeof(T)>::count(Val, ZB); | |||
230 | } | |||
231 | ||||
232 | /// Get the index of the first set bit starting from the least | |||
233 | /// significant bit. | |||
234 | /// | |||
235 | /// Only unsigned integral types are allowed. | |||
236 | /// | |||
237 | /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are | |||
238 | /// valid arguments. | |||
239 | template <typename T> T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) { | |||
240 | if (ZB == ZB_Max && Val == 0) | |||
241 | return std::numeric_limits<T>::max(); | |||
242 | ||||
243 | return countTrailingZeros(Val, ZB_Undefined); | |||
244 | } | |||
245 | ||||
246 | /// Create a bitmask with the N right-most bits set to 1, and all other | |||
247 | /// bits set to 0. Only unsigned types are allowed. | |||
248 | template <typename T> T maskTrailingOnes(unsigned N) { | |||
249 | static_assert(std::is_unsigned<T>::value, "Invalid type!"); | |||
250 | const unsigned Bits = CHAR_BIT8 * sizeof(T); | |||
251 | assert(N <= Bits && "Invalid bit index")(static_cast<void> (0)); | |||
252 | return N == 0 ? 0 : (T(-1) >> (Bits - N)); | |||
253 | } | |||
254 | ||||
255 | /// Create a bitmask with the N left-most bits set to 1, and all other | |||
256 | /// bits set to 0. Only unsigned types are allowed. | |||
257 | template <typename T> T maskLeadingOnes(unsigned N) { | |||
258 | return ~maskTrailingOnes<T>(CHAR_BIT8 * sizeof(T) - N); | |||
259 | } | |||
260 | ||||
261 | /// Create a bitmask with the N right-most bits set to 0, and all other | |||
262 | /// bits set to 1. Only unsigned types are allowed. | |||
263 | template <typename T> T maskTrailingZeros(unsigned N) { | |||
264 | return maskLeadingOnes<T>(CHAR_BIT8 * sizeof(T) - N); | |||
265 | } | |||
266 | ||||
267 | /// Create a bitmask with the N left-most bits set to 0, and all other | |||
268 | /// bits set to 1. Only unsigned types are allowed. | |||
269 | template <typename T> T maskLeadingZeros(unsigned N) { | |||
270 | return maskTrailingOnes<T>(CHAR_BIT8 * sizeof(T) - N); | |||
271 | } | |||
272 | ||||
273 | /// Get the index of the last set bit starting from the least | |||
274 | /// significant bit. | |||
275 | /// | |||
276 | /// Only unsigned integral types are allowed. | |||
277 | /// | |||
278 | /// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are | |||
279 | /// valid arguments. | |||
280 | template <typename T> T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) { | |||
281 | if (ZB == ZB_Max && Val == 0) | |||
282 | return std::numeric_limits<T>::max(); | |||
283 | ||||
284 | // Use ^ instead of - because both gcc and llvm can remove the associated ^ | |||
285 | // in the __builtin_clz intrinsic on x86. | |||
286 | return countLeadingZeros(Val, ZB_Undefined) ^ | |||
287 | (std::numeric_limits<T>::digits - 1); | |||
288 | } | |||
289 | ||||
290 | /// Macro compressed bit reversal table for 256 bits. | |||
291 | /// | |||
292 | /// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable | |||
293 | static const unsigned char BitReverseTable256[256] = { | |||
294 | #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64 | |||
295 | #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16) | |||
296 | #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4) | |||
297 | R6(0), R6(2), R6(1), R6(3) | |||
298 | #undef R2 | |||
299 | #undef R4 | |||
300 | #undef R6 | |||
301 | }; | |||
302 | ||||
303 | /// Reverse the bits in \p Val. | |||
304 | template <typename T> | |||
305 | T reverseBits(T Val) { | |||
306 | unsigned char in[sizeof(Val)]; | |||
307 | unsigned char out[sizeof(Val)]; | |||
308 | std::memcpy(in, &Val, sizeof(Val)); | |||
309 | for (unsigned i = 0; i < sizeof(Val); ++i) | |||
310 | out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]]; | |||
311 | std::memcpy(&Val, out, sizeof(Val)); | |||
312 | return Val; | |||
313 | } | |||
314 | ||||
315 | #if __has_builtin(__builtin_bitreverse8)1 | |||
316 | template<> | |||
317 | inline uint8_t reverseBits<uint8_t>(uint8_t Val) { | |||
318 | return __builtin_bitreverse8(Val); | |||
319 | } | |||
320 | #endif | |||
321 | ||||
322 | #if __has_builtin(__builtin_bitreverse16)1 | |||
323 | template<> | |||
324 | inline uint16_t reverseBits<uint16_t>(uint16_t Val) { | |||
325 | return __builtin_bitreverse16(Val); | |||
326 | } | |||
327 | #endif | |||
328 | ||||
329 | #if __has_builtin(__builtin_bitreverse32)1 | |||
330 | template<> | |||
331 | inline uint32_t reverseBits<uint32_t>(uint32_t Val) { | |||
332 | return __builtin_bitreverse32(Val); | |||
333 | } | |||
334 | #endif | |||
335 | ||||
336 | #if __has_builtin(__builtin_bitreverse64)1 | |||
337 | template<> | |||
338 | inline uint64_t reverseBits<uint64_t>(uint64_t Val) { | |||
339 | return __builtin_bitreverse64(Val); | |||
340 | } | |||
341 | #endif | |||
342 | ||||
343 | // NOTE: The following support functions use the _32/_64 extensions instead of | |||
344 | // type overloading so that signed and unsigned integers can be used without | |||
345 | // ambiguity. | |||
346 | ||||
347 | /// Return the high 32 bits of a 64 bit value. | |||
348 | constexpr inline uint32_t Hi_32(uint64_t Value) { | |||
349 | return static_cast<uint32_t>(Value >> 32); | |||
350 | } | |||
351 | ||||
352 | /// Return the low 32 bits of a 64 bit value. | |||
353 | constexpr inline uint32_t Lo_32(uint64_t Value) { | |||
354 | return static_cast<uint32_t>(Value); | |||
355 | } | |||
356 | ||||
357 | /// Make a 64-bit integer from a high / low pair of 32-bit integers. | |||
358 | constexpr inline uint64_t Make_64(uint32_t High, uint32_t Low) { | |||
359 | return ((uint64_t)High << 32) | (uint64_t)Low; | |||
360 | } | |||
361 | ||||
362 | /// Checks if an integer fits into the given bit width. | |||
363 | template <unsigned N> constexpr inline bool isInt(int64_t x) { | |||
364 | return N >= 64 || (-(INT64_C(1)1L<<(N-1)) <= x && x < (INT64_C(1)1L<<(N-1))); | |||
365 | } | |||
366 | // Template specializations to get better code for common cases. | |||
367 | template <> constexpr inline bool isInt<8>(int64_t x) { | |||
368 | return static_cast<int8_t>(x) == x; | |||
369 | } | |||
370 | template <> constexpr inline bool isInt<16>(int64_t x) { | |||
371 | return static_cast<int16_t>(x) == x; | |||
372 | } | |||
373 | template <> constexpr inline bool isInt<32>(int64_t x) { | |||
374 | return static_cast<int32_t>(x) == x; | |||
375 | } | |||
376 | ||||
377 | /// Checks if a signed integer is an N bit number shifted left by S. | |||
378 | template <unsigned N, unsigned S> | |||
379 | constexpr inline bool isShiftedInt(int64_t x) { | |||
380 | static_assert( | |||
381 | N > 0, "isShiftedInt<0> doesn't make sense (refers to a 0-bit number."); | |||
382 | static_assert(N + S <= 64, "isShiftedInt<N, S> with N + S > 64 is too wide."); | |||
383 | return isInt<N + S>(x) && (x % (UINT64_C(1)1UL << S) == 0); | |||
384 | } | |||
385 | ||||
386 | /// Checks if an unsigned integer fits into the given bit width. | |||
387 | /// | |||
388 | /// This is written as two functions rather than as simply | |||
389 | /// | |||
390 | /// return N >= 64 || X < (UINT64_C(1) << N); | |||
391 | /// | |||
392 | /// to keep MSVC from (incorrectly) warning on isUInt<64> that we're shifting | |||
393 | /// left too many places. | |||
394 | template <unsigned N> | |||
395 | constexpr inline std::enable_if_t<(N < 64), bool> isUInt(uint64_t X) { | |||
396 | static_assert(N > 0, "isUInt<0> doesn't make sense"); | |||
397 | return X < (UINT64_C(1)1UL << (N)); | |||
398 | } | |||
399 | template <unsigned N> | |||
400 | constexpr inline std::enable_if_t<N >= 64, bool> isUInt(uint64_t) { | |||
401 | return true; | |||
402 | } | |||
403 | ||||
404 | // Template specializations to get better code for common cases. | |||
405 | template <> constexpr inline bool isUInt<8>(uint64_t x) { | |||
406 | return static_cast<uint8_t>(x) == x; | |||
407 | } | |||
408 | template <> constexpr inline bool isUInt<16>(uint64_t x) { | |||
409 | return static_cast<uint16_t>(x) == x; | |||
410 | } | |||
411 | template <> constexpr inline bool isUInt<32>(uint64_t x) { | |||
412 | return static_cast<uint32_t>(x) == x; | |||
413 | } | |||
414 | ||||
415 | /// Checks if a unsigned integer is an N bit number shifted left by S. | |||
416 | template <unsigned N, unsigned S> | |||
417 | constexpr inline bool isShiftedUInt(uint64_t x) { | |||
418 | static_assert( | |||
419 | N > 0, "isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)"); | |||
420 | static_assert(N + S <= 64, | |||
421 | "isShiftedUInt<N, S> with N + S > 64 is too wide."); | |||
422 | // Per the two static_asserts above, S must be strictly less than 64. So | |||
423 | // 1 << S is not undefined behavior. | |||
424 | return isUInt<N + S>(x) && (x % (UINT64_C(1)1UL << S) == 0); | |||
425 | } | |||
426 | ||||
427 | /// Gets the maximum value for a N-bit unsigned integer. | |||
428 | inline uint64_t maxUIntN(uint64_t N) { | |||
429 | assert(N > 0 && N <= 64 && "integer width out of range")(static_cast<void> (0)); | |||
430 | ||||
431 | // uint64_t(1) << 64 is undefined behavior, so we can't do | |||
432 | // (uint64_t(1) << N) - 1 | |||
433 | // without checking first that N != 64. But this works and doesn't have a | |||
434 | // branch. | |||
435 | return UINT64_MAX(18446744073709551615UL) >> (64 - N); | |||
436 | } | |||
437 | ||||
438 | /// Gets the minimum value for a N-bit signed integer. | |||
439 | inline int64_t minIntN(int64_t N) { | |||
440 | assert(N > 0 && N <= 64 && "integer width out of range")(static_cast<void> (0)); | |||
441 | ||||
442 | return UINT64_C(1)1UL + ~(UINT64_C(1)1UL << (N - 1)); | |||
443 | } | |||
444 | ||||
445 | /// Gets the maximum value for a N-bit signed integer. | |||
446 | inline int64_t maxIntN(int64_t N) { | |||
447 | assert(N > 0 && N <= 64 && "integer width out of range")(static_cast<void> (0)); | |||
448 | ||||
449 | // This relies on two's complement wraparound when N == 64, so we convert to | |||
450 | // int64_t only at the very end to avoid UB. | |||
451 | return (UINT64_C(1)1UL << (N - 1)) - 1; | |||
452 | } | |||
453 | ||||
454 | /// Checks if an unsigned integer fits into the given (dynamic) bit width. | |||
455 | inline bool isUIntN(unsigned N, uint64_t x) { | |||
456 | return N >= 64 || x <= maxUIntN(N); | |||
457 | } | |||
458 | ||||
459 | /// Checks if an signed integer fits into the given (dynamic) bit width. | |||
460 | inline bool isIntN(unsigned N, int64_t x) { | |||
461 | return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N)); | |||
462 | } | |||
463 | ||||
464 | /// Return true if the argument is a non-empty sequence of ones starting at the | |||
465 | /// least significant bit with the remainder zero (32 bit version). | |||
466 | /// Ex. isMask_32(0x0000FFFFU) == true. | |||
467 | constexpr inline bool isMask_32(uint32_t Value) { | |||
468 | return Value && ((Value + 1) & Value) == 0; | |||
469 | } | |||
470 | ||||
471 | /// Return true if the argument is a non-empty sequence of ones starting at the | |||
472 | /// least significant bit with the remainder zero (64 bit version). | |||
473 | constexpr inline bool isMask_64(uint64_t Value) { | |||
474 | return Value && ((Value + 1) & Value) == 0; | |||
475 | } | |||
476 | ||||
477 | /// Return true if the argument contains a non-empty sequence of ones with the | |||
478 | /// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true. | |||
479 | constexpr inline bool isShiftedMask_32(uint32_t Value) { | |||
480 | return Value && isMask_32((Value - 1) | Value); | |||
481 | } | |||
482 | ||||
483 | /// Return true if the argument contains a non-empty sequence of ones with the | |||
484 | /// remainder zero (64 bit version.) | |||
485 | constexpr inline bool isShiftedMask_64(uint64_t Value) { | |||
486 | return Value && isMask_64((Value - 1) | Value); | |||
487 | } | |||
488 | ||||
489 | /// Return true if the argument is a power of two > 0. | |||
490 | /// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.) | |||
491 | constexpr inline bool isPowerOf2_32(uint32_t Value) { | |||
492 | return Value && !(Value & (Value - 1)); | |||
493 | } | |||
494 | ||||
495 | /// Return true if the argument is a power of two > 0 (64 bit edition.) | |||
496 | constexpr inline bool isPowerOf2_64(uint64_t Value) { | |||
497 | return Value && !(Value & (Value - 1)); | |||
498 | } | |||
499 | ||||
500 | /// Count the number of ones from the most significant bit to the first | |||
501 | /// zero bit. | |||
502 | /// | |||
503 | /// Ex. countLeadingOnes(0xFF0FFF00) == 8. | |||
504 | /// Only unsigned integral types are allowed. | |||
505 | /// | |||
506 | /// \param ZB the behavior on an input of all ones. Only ZB_Width and | |||
507 | /// ZB_Undefined are valid arguments. | |||
508 | template <typename T> | |||
509 | unsigned countLeadingOnes(T Value, ZeroBehavior ZB = ZB_Width) { | |||
510 | static_assert(std::numeric_limits<T>::is_integer && | |||
511 | !std::numeric_limits<T>::is_signed, | |||
512 | "Only unsigned integral types are allowed."); | |||
513 | return countLeadingZeros<T>(~Value, ZB); | |||
514 | } | |||
515 | ||||
516 | /// Count the number of ones from the least significant bit to the first | |||
517 | /// zero bit. | |||
518 | /// | |||
519 | /// Ex. countTrailingOnes(0x00FF00FF) == 8. | |||
520 | /// Only unsigned integral types are allowed. | |||
521 | /// | |||
522 | /// \param ZB the behavior on an input of all ones. Only ZB_Width and | |||
523 | /// ZB_Undefined are valid arguments. | |||
524 | template <typename T> | |||
525 | unsigned countTrailingOnes(T Value, ZeroBehavior ZB = ZB_Width) { | |||
526 | static_assert(std::numeric_limits<T>::is_integer && | |||
527 | !std::numeric_limits<T>::is_signed, | |||
528 | "Only unsigned integral types are allowed."); | |||
529 | return countTrailingZeros<T>(~Value, ZB); | |||
530 | } | |||
531 | ||||
532 | namespace detail { | |||
533 | template <typename T, std::size_t SizeOfT> struct PopulationCounter { | |||
534 | static unsigned count(T Value) { | |||
535 | // Generic version, forward to 32 bits. | |||
536 | static_assert(SizeOfT <= 4, "Not implemented!"); | |||
537 | #if defined(__GNUC__4) | |||
538 | return __builtin_popcount(Value); | |||
539 | #else | |||
540 | uint32_t v = Value; | |||
541 | v = v - ((v >> 1) & 0x55555555); | |||
542 | v = (v & 0x33333333) + ((v >> 2) & 0x33333333); | |||
543 | return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; | |||
544 | #endif | |||
545 | } | |||
546 | }; | |||
547 | ||||
548 | template <typename T> struct PopulationCounter<T, 8> { | |||
549 | static unsigned count(T Value) { | |||
550 | #if defined(__GNUC__4) | |||
551 | return __builtin_popcountll(Value); | |||
552 | #else | |||
553 | uint64_t v = Value; | |||
554 | v = v - ((v >> 1) & 0x5555555555555555ULL); | |||
555 | v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL); | |||
556 | v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL; | |||
557 | return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56); | |||
558 | #endif | |||
559 | } | |||
560 | }; | |||
561 | } // namespace detail | |||
562 | ||||
563 | /// Count the number of set bits in a value. | |||
564 | /// Ex. countPopulation(0xF000F000) = 8 | |||
565 | /// Returns 0 if the word is zero. | |||
566 | template <typename T> | |||
567 | inline unsigned countPopulation(T Value) { | |||
568 | static_assert(std::numeric_limits<T>::is_integer && | |||
569 | !std::numeric_limits<T>::is_signed, | |||
570 | "Only unsigned integral types are allowed."); | |||
571 | return detail::PopulationCounter<T, sizeof(T)>::count(Value); | |||
572 | } | |||
573 | ||||
574 | /// Compile time Log2. | |||
575 | /// Valid only for positive powers of two. | |||
576 | template <size_t kValue> constexpr inline size_t CTLog2() { | |||
577 | static_assert(kValue > 0 && llvm::isPowerOf2_64(kValue), | |||
578 | "Value is not a valid power of 2"); | |||
579 | return 1 + CTLog2<kValue / 2>(); | |||
580 | } | |||
581 | ||||
582 | template <> constexpr inline size_t CTLog2<1>() { return 0; } | |||
583 | ||||
584 | /// Return the log base 2 of the specified value. | |||
585 | inline double Log2(double Value) { | |||
586 | #if defined(__ANDROID_API__) && __ANDROID_API__ < 18 | |||
587 | return __builtin_log(Value) / __builtin_log(2.0); | |||
588 | #else | |||
589 | return log2(Value); | |||
590 | #endif | |||
591 | } | |||
592 | ||||
593 | /// Return the floor log base 2 of the specified value, -1 if the value is zero. | |||
594 | /// (32 bit edition.) | |||
595 | /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2 | |||
596 | inline unsigned Log2_32(uint32_t Value) { | |||
597 | return 31 - countLeadingZeros(Value); | |||
598 | } | |||
599 | ||||
600 | /// Return the floor log base 2 of the specified value, -1 if the value is zero. | |||
601 | /// (64 bit edition.) | |||
602 | inline unsigned Log2_64(uint64_t Value) { | |||
603 | return 63 - countLeadingZeros(Value); | |||
604 | } | |||
605 | ||||
606 | /// Return the ceil log base 2 of the specified value, 32 if the value is zero. | |||
607 | /// (32 bit edition). | |||
608 | /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3 | |||
609 | inline unsigned Log2_32_Ceil(uint32_t Value) { | |||
610 | return 32 - countLeadingZeros(Value - 1); | |||
611 | } | |||
612 | ||||
613 | /// Return the ceil log base 2 of the specified value, 64 if the value is zero. | |||
614 | /// (64 bit edition.) | |||
615 | inline unsigned Log2_64_Ceil(uint64_t Value) { | |||
616 | return 64 - countLeadingZeros(Value - 1); | |||
617 | } | |||
618 | ||||
619 | /// Return the greatest common divisor of the values using Euclid's algorithm. | |||
620 | template <typename T> | |||
621 | inline T greatestCommonDivisor(T A, T B) { | |||
622 | while (B) { | |||
623 | T Tmp = B; | |||
624 | B = A % B; | |||
625 | A = Tmp; | |||
626 | } | |||
627 | return A; | |||
628 | } | |||
629 | ||||
630 | inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) { | |||
631 | return greatestCommonDivisor<uint64_t>(A, B); | |||
632 | } | |||
633 | ||||
634 | /// This function takes a 64-bit integer and returns the bit equivalent double. | |||
635 | inline double BitsToDouble(uint64_t Bits) { | |||
636 | double D; | |||
637 | static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes"); | |||
638 | memcpy(&D, &Bits, sizeof(Bits)); | |||
639 | return D; | |||
640 | } | |||
641 | ||||
642 | /// This function takes a 32-bit integer and returns the bit equivalent float. | |||
643 | inline float BitsToFloat(uint32_t Bits) { | |||
644 | float F; | |||
645 | static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes"); | |||
646 | memcpy(&F, &Bits, sizeof(Bits)); | |||
647 | return F; | |||
648 | } | |||
649 | ||||
650 | /// This function takes a double and returns the bit equivalent 64-bit integer. | |||
651 | /// Note that copying doubles around changes the bits of NaNs on some hosts, | |||
652 | /// notably x86, so this routine cannot be used if these bits are needed. | |||
653 | inline uint64_t DoubleToBits(double Double) { | |||
654 | uint64_t Bits; | |||
655 | static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes"); | |||
656 | memcpy(&Bits, &Double, sizeof(Double)); | |||
657 | return Bits; | |||
658 | } | |||
659 | ||||
660 | /// This function takes a float and returns the bit equivalent 32-bit integer. | |||
661 | /// Note that copying floats around changes the bits of NaNs on some hosts, | |||
662 | /// notably x86, so this routine cannot be used if these bits are needed. | |||
663 | inline uint32_t FloatToBits(float Float) { | |||
664 | uint32_t Bits; | |||
665 | static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes"); | |||
666 | memcpy(&Bits, &Float, sizeof(Float)); | |||
667 | return Bits; | |||
668 | } | |||
669 | ||||
670 | /// A and B are either alignments or offsets. Return the minimum alignment that | |||
671 | /// may be assumed after adding the two together. | |||
672 | constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) { | |||
673 | // The largest power of 2 that divides both A and B. | |||
674 | // | |||
675 | // Replace "-Value" by "1+~Value" in the following commented code to avoid | |||
676 | // MSVC warning C4146 | |||
677 | // return (A | B) & -(A | B); | |||
678 | return (A | B) & (1 + ~(A | B)); | |||
679 | } | |||
680 | ||||
681 | /// Returns the next power of two (in 64-bits) that is strictly greater than A. | |||
682 | /// Returns zero on overflow. | |||
683 | inline uint64_t NextPowerOf2(uint64_t A) { | |||
684 | A |= (A >> 1); | |||
685 | A |= (A >> 2); | |||
686 | A |= (A >> 4); | |||
687 | A |= (A >> 8); | |||
688 | A |= (A >> 16); | |||
689 | A |= (A >> 32); | |||
690 | return A + 1; | |||
691 | } | |||
692 | ||||
693 | /// Returns the power of two which is less than or equal to the given value. | |||
694 | /// Essentially, it is a floor operation across the domain of powers of two. | |||
695 | inline uint64_t PowerOf2Floor(uint64_t A) { | |||
696 | if (!A) return 0; | |||
697 | return 1ull << (63 - countLeadingZeros(A, ZB_Undefined)); | |||
698 | } | |||
699 | ||||
700 | /// Returns the power of two which is greater than or equal to the given value. | |||
701 | /// Essentially, it is a ceil operation across the domain of powers of two. | |||
702 | inline uint64_t PowerOf2Ceil(uint64_t A) { | |||
703 | if (!A) | |||
704 | return 0; | |||
705 | return NextPowerOf2(A - 1); | |||
706 | } | |||
707 | ||||
708 | /// Returns the next integer (mod 2**64) that is greater than or equal to | |||
709 | /// \p Value and is a multiple of \p Align. \p Align must be non-zero. | |||
710 | /// | |||
711 | /// If non-zero \p Skew is specified, the return value will be a minimal | |||
712 | /// integer that is greater than or equal to \p Value and equal to | |||
713 | /// \p Align * N + \p Skew for some integer N. If \p Skew is larger than | |||
714 | /// \p Align, its value is adjusted to '\p Skew mod \p Align'. | |||
715 | /// | |||
716 | /// Examples: | |||
717 | /// \code | |||
718 | /// alignTo(5, 8) = 8 | |||
719 | /// alignTo(17, 8) = 24 | |||
720 | /// alignTo(~0LL, 8) = 0 | |||
721 | /// alignTo(321, 255) = 510 | |||
722 | /// | |||
723 | /// alignTo(5, 8, 7) = 7 | |||
724 | /// alignTo(17, 8, 1) = 17 | |||
725 | /// alignTo(~0LL, 8, 3) = 3 | |||
726 | /// alignTo(321, 255, 42) = 552 | |||
727 | /// \endcode | |||
728 | inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) { | |||
729 | assert(Align != 0u && "Align can't be 0.")(static_cast<void> (0)); | |||
730 | Skew %= Align; | |||
731 | return (Value + Align - 1 - Skew) / Align * Align + Skew; | |||
732 | } | |||
733 | ||||
734 | /// Returns the next integer (mod 2**64) that is greater than or equal to | |||
735 | /// \p Value and is a multiple of \c Align. \c Align must be non-zero. | |||
736 | template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) { | |||
737 | static_assert(Align != 0u, "Align must be non-zero"); | |||
738 | return (Value + Align - 1) / Align * Align; | |||
739 | } | |||
740 | ||||
741 | /// Returns the integer ceil(Numerator / Denominator). | |||
742 | inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) { | |||
743 | return alignTo(Numerator, Denominator) / Denominator; | |||
744 | } | |||
745 | ||||
746 | /// Returns the integer nearest(Numerator / Denominator). | |||
747 | inline uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator) { | |||
748 | return (Numerator + (Denominator / 2)) / Denominator; | |||
749 | } | |||
750 | ||||
751 | /// Returns the largest uint64_t less than or equal to \p Value and is | |||
752 | /// \p Skew mod \p Align. \p Align must be non-zero | |||
753 | inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) { | |||
754 | assert(Align != 0u && "Align can't be 0.")(static_cast<void> (0)); | |||
755 | Skew %= Align; | |||
756 | return (Value - Skew) / Align * Align + Skew; | |||
757 | } | |||
758 | ||||
759 | /// Sign-extend the number in the bottom B bits of X to a 32-bit integer. | |||
760 | /// Requires 0 < B <= 32. | |||
761 | template <unsigned B> constexpr inline int32_t SignExtend32(uint32_t X) { | |||
762 | static_assert(B > 0, "Bit width can't be 0."); | |||
763 | static_assert(B <= 32, "Bit width out of range."); | |||
764 | return int32_t(X << (32 - B)) >> (32 - B); | |||
765 | } | |||
766 | ||||
767 | /// Sign-extend the number in the bottom B bits of X to a 32-bit integer. | |||
768 | /// Requires 0 < B <= 32. | |||
769 | inline int32_t SignExtend32(uint32_t X, unsigned B) { | |||
770 | assert(B > 0 && "Bit width can't be 0.")(static_cast<void> (0)); | |||
771 | assert(B <= 32 && "Bit width out of range.")(static_cast<void> (0)); | |||
772 | return int32_t(X << (32 - B)) >> (32 - B); | |||
773 | } | |||
774 | ||||
775 | /// Sign-extend the number in the bottom B bits of X to a 64-bit integer. | |||
776 | /// Requires 0 < B <= 64. | |||
777 | template <unsigned B> constexpr inline int64_t SignExtend64(uint64_t x) { | |||
778 | static_assert(B > 0, "Bit width can't be 0."); | |||
779 | static_assert(B <= 64, "Bit width out of range."); | |||
780 | return int64_t(x << (64 - B)) >> (64 - B); | |||
781 | } | |||
782 | ||||
783 | /// Sign-extend the number in the bottom B bits of X to a 64-bit integer. | |||
784 | /// Requires 0 < B <= 64. | |||
785 | inline int64_t SignExtend64(uint64_t X, unsigned B) { | |||
786 | assert(B > 0 && "Bit width can't be 0.")(static_cast<void> (0)); | |||
787 | assert(B <= 64 && "Bit width out of range.")(static_cast<void> (0)); | |||
788 | return int64_t(X << (64 - B)) >> (64 - B); | |||
| ||||
789 | } | |||
790 | ||||
791 | /// Subtract two unsigned integers, X and Y, of type T and return the absolute | |||
792 | /// value of the result. | |||
793 | template <typename T> | |||
794 | std::enable_if_t<std::is_unsigned<T>::value, T> AbsoluteDifference(T X, T Y) { | |||
795 | return X > Y ? (X - Y) : (Y - X); | |||
796 | } | |||
797 | ||||
798 | /// Add two unsigned integers, X and Y, of type T. Clamp the result to the | |||
799 | /// maximum representable value of T on overflow. ResultOverflowed indicates if | |||
800 | /// the result is larger than the maximum representable value of type T. | |||
801 | template <typename T> | |||
802 | std::enable_if_t<std::is_unsigned<T>::value, T> | |||
803 | SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) { | |||
804 | bool Dummy; | |||
805 | bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; | |||
806 | // Hacker's Delight, p. 29 | |||
807 | T Z = X + Y; | |||
808 | Overflowed = (Z < X || Z < Y); | |||
809 | if (Overflowed) | |||
810 | return std::numeric_limits<T>::max(); | |||
811 | else | |||
812 | return Z; | |||
813 | } | |||
814 | ||||
815 | /// Multiply two unsigned integers, X and Y, of type T. Clamp the result to the | |||
816 | /// maximum representable value of T on overflow. ResultOverflowed indicates if | |||
817 | /// the result is larger than the maximum representable value of type T. | |||
818 | template <typename T> | |||
819 | std::enable_if_t<std::is_unsigned<T>::value, T> | |||
820 | SaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) { | |||
821 | bool Dummy; | |||
822 | bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; | |||
823 | ||||
824 | // Hacker's Delight, p. 30 has a different algorithm, but we don't use that | |||
825 | // because it fails for uint16_t (where multiplication can have undefined | |||
826 | // behavior due to promotion to int), and requires a division in addition | |||
827 | // to the multiplication. | |||
828 | ||||
829 | Overflowed = false; | |||
830 | ||||
831 | // Log2(Z) would be either Log2Z or Log2Z + 1. | |||
832 | // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z | |||
833 | // will necessarily be less than Log2Max as desired. | |||
834 | int Log2Z = Log2_64(X) + Log2_64(Y); | |||
835 | const T Max = std::numeric_limits<T>::max(); | |||
836 | int Log2Max = Log2_64(Max); | |||
837 | if (Log2Z < Log2Max) { | |||
838 | return X * Y; | |||
839 | } | |||
840 | if (Log2Z > Log2Max) { | |||
841 | Overflowed = true; | |||
842 | return Max; | |||
843 | } | |||
844 | ||||
845 | // We're going to use the top bit, and maybe overflow one | |||
846 | // bit past it. Multiply all but the bottom bit then add | |||
847 | // that on at the end. | |||
848 | T Z = (X >> 1) * Y; | |||
849 | if (Z & ~(Max >> 1)) { | |||
850 | Overflowed = true; | |||
851 | return Max; | |||
852 | } | |||
853 | Z <<= 1; | |||
854 | if (X & 1) | |||
855 | return SaturatingAdd(Z, Y, ResultOverflowed); | |||
856 | ||||
857 | return Z; | |||
858 | } | |||
859 | ||||
860 | /// Multiply two unsigned integers, X and Y, and add the unsigned integer, A to | |||
861 | /// the product. Clamp the result to the maximum representable value of T on | |||
862 | /// overflow. ResultOverflowed indicates if the result is larger than the | |||
863 | /// maximum representable value of type T. | |||
864 | template <typename T> | |||
865 | std::enable_if_t<std::is_unsigned<T>::value, T> | |||
866 | SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) { | |||
867 | bool Dummy; | |||
868 | bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; | |||
869 | ||||
870 | T Product = SaturatingMultiply(X, Y, &Overflowed); | |||
871 | if (Overflowed) | |||
872 | return Product; | |||
873 | ||||
874 | return SaturatingAdd(A, Product, &Overflowed); | |||
875 | } | |||
876 | ||||
877 | /// Use this rather than HUGE_VALF; the latter causes warnings on MSVC. | |||
878 | extern const float huge_valf; | |||
879 | ||||
880 | ||||
881 | /// Add two signed integers, computing the two's complement truncated result, | |||
882 | /// returning true if overflow occured. | |||
883 | template <typename T> | |||
884 | std::enable_if_t<std::is_signed<T>::value, T> AddOverflow(T X, T Y, T &Result) { | |||
885 | #if __has_builtin(__builtin_add_overflow)1 | |||
886 | return __builtin_add_overflow(X, Y, &Result); | |||
887 | #else | |||
888 | // Perform the unsigned addition. | |||
889 | using U = std::make_unsigned_t<T>; | |||
890 | const U UX = static_cast<U>(X); | |||
891 | const U UY = static_cast<U>(Y); | |||
892 | const U UResult = UX + UY; | |||
893 | ||||
894 | // Convert to signed. | |||
895 | Result = static_cast<T>(UResult); | |||
896 | ||||
897 | // Adding two positive numbers should result in a positive number. | |||
898 | if (X > 0 && Y > 0) | |||
899 | return Result <= 0; | |||
900 | // Adding two negatives should result in a negative number. | |||
901 | if (X < 0 && Y < 0) | |||
902 | return Result >= 0; | |||
903 | return false; | |||
904 | #endif | |||
905 | } | |||
906 | ||||
907 | /// Subtract two signed integers, computing the two's complement truncated | |||
908 | /// result, returning true if an overflow ocurred. | |||
909 | template <typename T> | |||
910 | std::enable_if_t<std::is_signed<T>::value, T> SubOverflow(T X, T Y, T &Result) { | |||
911 | #if __has_builtin(__builtin_sub_overflow)1 | |||
912 | return __builtin_sub_overflow(X, Y, &Result); | |||
913 | #else | |||
914 | // Perform the unsigned addition. | |||
915 | using U = std::make_unsigned_t<T>; | |||
916 | const U UX = static_cast<U>(X); | |||
917 | const U UY = static_cast<U>(Y); | |||
918 | const U UResult = UX - UY; | |||
919 | ||||
920 | // Convert to signed. | |||
921 | Result = static_cast<T>(UResult); | |||
922 | ||||
923 | // Subtracting a positive number from a negative results in a negative number. | |||
924 | if (X <= 0 && Y > 0) | |||
925 | return Result >= 0; | |||
926 | // Subtracting a negative number from a positive results in a positive number. | |||
927 | if (X >= 0 && Y < 0) | |||
928 | return Result <= 0; | |||
929 | return false; | |||
930 | #endif | |||
931 | } | |||
932 | ||||
933 | /// Multiply two signed integers, computing the two's complement truncated | |||
934 | /// result, returning true if an overflow ocurred. | |||
935 | template <typename T> | |||
936 | std::enable_if_t<std::is_signed<T>::value, T> MulOverflow(T X, T Y, T &Result) { | |||
937 | // Perform the unsigned multiplication on absolute values. | |||
938 | using U = std::make_unsigned_t<T>; | |||
939 | const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X); | |||
940 | const U UY = Y < 0 ? (0 - static_cast<U>(Y)) : static_cast<U>(Y); | |||
941 | const U UResult = UX * UY; | |||
942 | ||||
943 | // Convert to signed. | |||
944 | const bool IsNegative = (X < 0) ^ (Y < 0); | |||
945 | Result = IsNegative ? (0 - UResult) : UResult; | |||
946 | ||||
947 | // If any of the args was 0, result is 0 and no overflow occurs. | |||
948 | if (UX == 0 || UY == 0) | |||
949 | return false; | |||
950 | ||||
951 | // UX and UY are in [1, 2^n], where n is the number of digits. | |||
952 | // Check how the max allowed absolute value (2^n for negative, 2^(n-1) for | |||
953 | // positive) divided by an argument compares to the other. | |||
954 | if (IsNegative) | |||
955 | return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY; | |||
956 | else | |||
957 | return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY; | |||
958 | } | |||
959 | ||||
960 | } // End llvm namespace | |||
961 | ||||
962 | #endif |