diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -1638,25 +1638,7 @@ /// /// to get around any mathematical concerns resulting from /// referencing 2 in a space where 2 does no exist. - unsigned nearestLogBase2() const { - // Special case when we have a bitwidth of 1. If VAL is 1, then we - // get 0. If VAL is 0, we get WORDTYPE_MAX which gets truncated to - // UINT32_MAX. - if (BitWidth == 1) - return U.VAL - 1; - - // Handle the zero case. - if (isNullValue()) - return UINT32_MAX; - - // The non-zero case is handled by computing: - // - // nearestLogBase2(x) = logBase2(x) + x[logBase2(x)-1]. - // - // where x[i] is referring to the value of the ith bit of x. - unsigned lg = logBase2(); - return lg + unsigned((*this)[lg - 1]); - } + unsigned nearestLogBase2() const; /// \returns the log base 2 of this APInt if its an exact power of two, -1 /// otherwise @@ -1666,12 +1648,12 @@ return logBase2(); } - /// Compute the square root + /// Compute the square root. APInt sqrt() const; - /// Get the absolute value; - /// - /// If *this is < 0 then return -(*this), otherwise *this; + /// Get the absolute value. If *this is < 0 then return -(*this), otherwise + /// *this. Note that the "most negative" signed number (e.g. -128 for 8 bit + /// wide APInt) is unchanged due to how negation works. APInt abs() const { if (isNegative()) return -(*this); @@ -1681,18 +1663,6 @@ /// \returns the multiplicative inverse for a given modulo. APInt multiplicativeInverse(const APInt &modulo) const; - /// @} - /// \name Support for division by constant - /// @{ - - /// Calculate the magic number for signed division by a constant. - struct ms; - ms magic() const; - - /// Calculate the magic number for unsigned division by a constant. - struct mu; - mu magicu(unsigned LeadingZeros = 0) const; - /// @} /// \name Building-block Operations for APInt and APFloat /// @{ @@ -1794,12 +1764,6 @@ /// restrictions on Count. static void tcShiftRight(WordType *, unsigned Words, unsigned Count); - /// The obvious AND, OR and XOR and complement operations. - static void tcAnd(WordType *, const WordType *, unsigned); - static void tcOr(WordType *, const WordType *, unsigned); - static void tcXor(WordType *, const WordType *, unsigned); - static void tcComplement(WordType *, unsigned); - /// Comparison (unsigned) of two bignums. static int tcCompare(const WordType *, const WordType *, unsigned); @@ -1813,9 +1777,6 @@ return tcSubtractPart(dst, 1, parts); } - /// Set the least significant BITS and clear the rest. - static void tcSetLeastSignificantBits(WordType *, unsigned, unsigned bits); - /// Used to insert APInt objects, or objects that contain APInt objects, into /// FoldingSets. void Profile(FoldingSetNodeID &id) const; @@ -1996,19 +1957,6 @@ /// @} }; -/// Magic data for optimising signed division by a constant. -struct APInt::ms { - APInt m; ///< magic number - unsigned s; ///< shift amount -}; - -/// Magic data for optimising unsigned division by a constant. -struct APInt::mu { - APInt m; ///< magic number - bool a; ///< add indicator - unsigned s; ///< shift amount -}; - inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; } inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; } diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -77,8 +77,8 @@ return isUsedByReturnOnly(Node, Chain); } -bool TargetLowering::parametersInCSRMatch(const MachineRegisterInfo &MRI, - const uint32_t *CallerPreservedMask, +bool TargetLowering::parametersInCSRMatch( + const MachineRegisterInfo &MRI, const uint32_t *CallerPreservedMask, const SmallVectorImpl &ArgLocs, const SmallVectorImpl &OutVals) const { for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { @@ -135,12 +135,9 @@ /// Generate a libcall taking the given operands as arguments and returning a /// result of type RetVT. -std::pair -TargetLowering::makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT, - ArrayRef Ops, - MakeLibCallOptions CallOptions, - const SDLoc &dl, - SDValue InChain) const { +std::pair TargetLowering::makeLibCall( + SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT, ArrayRef Ops, + MakeLibCallOptions CallOptions, const SDLoc &dl, SDValue InChain) const { if (!InChain) InChain = DAG.getEntryNode(); @@ -152,8 +149,8 @@ SDValue NewOp = Ops[i]; Entry.Node = NewOp; Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext()); - Entry.IsSExt = shouldSignExtendTypeInLibCall(NewOp.getValueType(), - CallOptions.IsSExt); + Entry.IsSExt = + shouldSignExtendTypeInLibCall(NewOp.getValueType(), CallOptions.IsSExt); Entry.IsZExt = !Entry.IsSExt; if (CallOptions.IsSoften && @@ -282,8 +279,8 @@ /// SELECT_CC, and SETCC handlers. void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT, SDValue &NewLHS, SDValue &NewRHS, - ISD::CondCode &CCCode, - const SDLoc &dl, const SDValue OldLHS, + ISD::CondCode &CCCode, const SDLoc &dl, + const SDValue OldLHS, const SDValue OldRHS) const { SDValue Chain; return softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, dl, OldLHS, @@ -292,17 +289,17 @@ void TargetLowering::softenSetCCOperands(SelectionDAG &DAG, EVT VT, SDValue &NewLHS, SDValue &NewRHS, - ISD::CondCode &CCCode, - const SDLoc &dl, const SDValue OldLHS, - const SDValue OldRHS, - SDValue &Chain, + ISD::CondCode &CCCode, const SDLoc &dl, + const SDValue OldLHS, + const SDValue OldRHS, SDValue &Chain, bool IsSignaling) const { // FIXME: Currently we cannot really respect all IEEE predicates due to libgcc // not supporting it. We can update this code when libgcc provides such // functions. - assert((VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128 || VT == MVT::ppcf128) - && "Unsupported setcc type!"); + assert((VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128 || + VT == MVT::ppcf128) && + "Unsupported setcc type!"); // Expand into one or more soft-fp libcall(s). RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL; @@ -310,85 +307,99 @@ switch (CCCode) { case ISD::SETEQ: case ISD::SETOEQ: - LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : - (VT == MVT::f64) ? RTLIB::OEQ_F64 : - (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 + : (VT == MVT::f64) ? RTLIB::OEQ_F64 + : (VT == MVT::f128) ? RTLIB::OEQ_F128 + : RTLIB::OEQ_PPCF128; break; case ISD::SETNE: case ISD::SETUNE: - LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : - (VT == MVT::f64) ? RTLIB::UNE_F64 : - (VT == MVT::f128) ? RTLIB::UNE_F128 : RTLIB::UNE_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 + : (VT == MVT::f64) ? RTLIB::UNE_F64 + : (VT == MVT::f128) ? RTLIB::UNE_F128 + : RTLIB::UNE_PPCF128; break; case ISD::SETGE: case ISD::SETOGE: - LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : - (VT == MVT::f64) ? RTLIB::OGE_F64 : - (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 + : (VT == MVT::f64) ? RTLIB::OGE_F64 + : (VT == MVT::f128) ? RTLIB::OGE_F128 + : RTLIB::OGE_PPCF128; break; case ISD::SETLT: case ISD::SETOLT: - LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : - (VT == MVT::f64) ? RTLIB::OLT_F64 : - (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 + : (VT == MVT::f64) ? RTLIB::OLT_F64 + : (VT == MVT::f128) ? RTLIB::OLT_F128 + : RTLIB::OLT_PPCF128; break; case ISD::SETLE: case ISD::SETOLE: - LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : - (VT == MVT::f64) ? RTLIB::OLE_F64 : - (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 + : (VT == MVT::f64) ? RTLIB::OLE_F64 + : (VT == MVT::f128) ? RTLIB::OLE_F128 + : RTLIB::OLE_PPCF128; break; case ISD::SETGT: case ISD::SETOGT: - LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : - (VT == MVT::f64) ? RTLIB::OGT_F64 : - (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 + : (VT == MVT::f64) ? RTLIB::OGT_F64 + : (VT == MVT::f128) ? RTLIB::OGT_F128 + : RTLIB::OGT_PPCF128; break; case ISD::SETO: ShouldInvertCC = true; LLVM_FALLTHROUGH; case ISD::SETUO: - LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : - (VT == MVT::f64) ? RTLIB::UO_F64 : - (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 + : (VT == MVT::f64) ? RTLIB::UO_F64 + : (VT == MVT::f128) ? RTLIB::UO_F128 + : RTLIB::UO_PPCF128; break; case ISD::SETONE: // SETONE = O && UNE ShouldInvertCC = true; LLVM_FALLTHROUGH; case ISD::SETUEQ: - LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : - (VT == MVT::f64) ? RTLIB::UO_F64 : - (VT == MVT::f128) ? RTLIB::UO_F128 : RTLIB::UO_PPCF128; - LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : - (VT == MVT::f64) ? RTLIB::OEQ_F64 : - (VT == MVT::f128) ? RTLIB::OEQ_F128 : RTLIB::OEQ_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 + : (VT == MVT::f64) ? RTLIB::UO_F64 + : (VT == MVT::f128) ? RTLIB::UO_F128 + : RTLIB::UO_PPCF128; + LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 + : (VT == MVT::f64) ? RTLIB::OEQ_F64 + : (VT == MVT::f128) ? RTLIB::OEQ_F128 + : RTLIB::OEQ_PPCF128; break; default: // Invert CC for unordered comparisons ShouldInvertCC = true; switch (CCCode) { case ISD::SETULT: - LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : - (VT == MVT::f64) ? RTLIB::OGE_F64 : - (VT == MVT::f128) ? RTLIB::OGE_F128 : RTLIB::OGE_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 + : (VT == MVT::f64) ? RTLIB::OGE_F64 + : (VT == MVT::f128) ? RTLIB::OGE_F128 + : RTLIB::OGE_PPCF128; break; case ISD::SETULE: - LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : - (VT == MVT::f64) ? RTLIB::OGT_F64 : - (VT == MVT::f128) ? RTLIB::OGT_F128 : RTLIB::OGT_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 + : (VT == MVT::f64) ? RTLIB::OGT_F64 + : (VT == MVT::f128) ? RTLIB::OGT_F128 + : RTLIB::OGT_PPCF128; break; case ISD::SETUGT: - LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : - (VT == MVT::f64) ? RTLIB::OLE_F64 : - (VT == MVT::f128) ? RTLIB::OLE_F128 : RTLIB::OLE_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 + : (VT == MVT::f64) ? RTLIB::OLE_F64 + : (VT == MVT::f128) ? RTLIB::OLE_F128 + : RTLIB::OLE_PPCF128; break; case ISD::SETUGE: - LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : - (VT == MVT::f64) ? RTLIB::OLT_F64 : - (VT == MVT::f128) ? RTLIB::OLT_F128 : RTLIB::OLT_PPCF128; + LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 + : (VT == MVT::f64) ? RTLIB::OLT_F64 + : (VT == MVT::f128) ? RTLIB::OLT_F128 + : RTLIB::OLT_PPCF128; break; - default: llvm_unreachable("Do not know how to soften this setcc!"); + default: + llvm_unreachable("Do not know how to soften this setcc!"); } } @@ -396,8 +407,7 @@ EVT RetVT = getCmpLibcallReturnType(); SDValue Ops[2] = {NewLHS, NewRHS}; TargetLowering::MakeLibCallOptions CallOptions; - EVT OpsVT[2] = { OldLHS.getValueType(), - OldRHS.getValueType() }; + EVT OpsVT[2] = {OldLHS.getValueType(), OldRHS.getValueType()}; CallOptions.setTypeListBeforeSoften(OpsVT, RetVT, true); auto Call = makeLibCall(DAG, LC1, RetVT, Ops, CallOptions, dl, Chain); NewLHS = Call.first; @@ -459,15 +469,13 @@ /// This returns the relocation base for the given PIC jumptable, the same as /// getPICJumpTableRelocBase, but as an MCExpr. -const MCExpr * -TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF, - unsigned JTI,MCContext &Ctx) const{ +const MCExpr *TargetLowering::getPICJumpTableRelocBaseExpr( + const MachineFunction *MF, unsigned JTI, MCContext &Ctx) const { // The normal PIC reloc base is the label at the start of the jump table. return MCSymbolRefExpr::create(MF->getJTISymbol(JTI, Ctx), Ctx); } -bool -TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { +bool TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { const TargetMachine &TM = getTargetMachine(); const GlobalValue *GV = GA->getGlobal(); @@ -1343,7 +1351,7 @@ if (DemandedBits.isSubsetOf(Known.Zero | Known2.Zero)) return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, VT, Op0, Op1)); - ConstantSDNode* C = isConstOrConstSplat(Op1, DemandedElts); + ConstantSDNode *C = isConstOrConstSplat(Op1, DemandedElts); if (C) { // If one side is a constant, and all of the set bits in the constant are // also known set on the other side, turn this into an AND, as we know @@ -1359,8 +1367,7 @@ // If the RHS is a constant, see if we can change it. Don't alter a -1 // constant because that's a 'not' op, and that is better for combining // and codegen. - if (!C->isAllOnesValue() && - DemandedBits.isSubsetOf(C->getAPIntValue())) { + if (!C->isAllOnesValue() && DemandedBits.isSubsetOf(C->getAPIntValue())) { // We're flipping all demanded bits. Flip the undemanded bits too. SDValue New = TLO.DAG.getNOT(dl, Op0, VT); return TLO.CombineTo(Op, New); @@ -1715,8 +1722,8 @@ // For pow-2 bitwidths we only demand the bottom modulo amt bits. if (isPowerOf2_32(BitWidth)) { APInt DemandedAmtBits(Op2.getScalarValueSizeInBits(), BitWidth - 1); - if (SimplifyDemandedBits(Op2, DemandedAmtBits, DemandedElts, - Known2, TLO, Depth + 1)) + if (SimplifyDemandedBits(Op2, DemandedAmtBits, DemandedElts, Known2, TLO, + Depth + 1)) return true; } break; @@ -1790,8 +1797,8 @@ // op legalization. // FIXME: Limit to scalars for now. if (DemandedBits.isOneValue() && !TLO.LegalOps && !VT.isVector()) - return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::PARITY, dl, VT, - Op.getOperand(0))); + return TLO.CombineTo( + Op, TLO.DAG.getNode(ISD::PARITY, dl, VT, Op.getOperand(0))); Known = TLO.DAG.computeKnownBits(Op, DemandedElts, Depth); break; @@ -2767,9 +2774,8 @@ // Update legal shuffle masks based on demanded elements if it won't reduce // to Identity which can cause premature removal of the shuffle mask. if (Updated && !IdentityLHS && !IdentityRHS && !TLO.LegalOps) { - SDValue LegalShuffle = - buildLegalVectorShuffle(VT, DL, Op.getOperand(0), Op.getOperand(1), - NewMask, TLO.DAG); + SDValue LegalShuffle = buildLegalVectorShuffle( + VT, DL, Op.getOperand(0), Op.getOperand(1), NewMask, TLO.DAG); if (LegalShuffle) return TLO.CombineTo(Op, LegalShuffle); } @@ -2973,14 +2979,14 @@ } void TargetLowering::computeKnownBitsForFrameIndex( - const int FrameIdx, KnownBits &Known, const MachineFunction &MF) const { + const int FrameIdx, KnownBits &Known, const MachineFunction &MF) const { // The low bits are known zero if the pointer is aligned. Known.Zero.setLowBits(Log2(MF.getFrameInfo().getObjectAlign(FrameIdx))); } Align TargetLowering::computeKnownAlignForTargetInstr( - GISelKnownBits &Analysis, Register R, const MachineRegisterInfo &MRI, - unsigned Depth) const { + GISelKnownBits &Analysis, Register R, const MachineRegisterInfo &MRI, + unsigned Depth) const { return Align(1); } @@ -3000,8 +3006,8 @@ } unsigned TargetLowering::computeNumSignBitsForTargetInstr( - GISelKnownBits &Analysis, Register R, const APInt &DemandedElts, - const MachineRegisterInfo &MRI, unsigned Depth) const { + GISelKnownBits &Analysis, Register R, const APInt &DemandedElts, + const MachineRegisterInfo &MRI, unsigned Depth) const { return 1; } @@ -3043,10 +3049,10 @@ return SDValue(); } -SDValue -TargetLowering::buildLegalVectorShuffle(EVT VT, const SDLoc &DL, SDValue N0, - SDValue N1, MutableArrayRef Mask, - SelectionDAG &DAG) const { +SDValue TargetLowering::buildLegalVectorShuffle(EVT VT, const SDLoc &DL, + SDValue N0, SDValue N1, + MutableArrayRef Mask, + SelectionDAG &DAG) const { bool LegalMask = isShuffleMaskLegal(Mask, VT); if (!LegalMask) { std::swap(N0, N1); @@ -3060,7 +3066,7 @@ return DAG.getVectorShuffle(VT, DL, N0, N1, Mask); } -const Constant *TargetLowering::getTargetConstantFromLoad(LoadSDNode*) const { +const Constant *TargetLowering::getTargetConstantFromLoad(LoadSDNode *) const { return nullptr; } @@ -3433,8 +3439,8 @@ return SDValue(); // (X - Y) == Y --> X == Y << 1 - EVT ShiftVT = getShiftAmountTy(OpVT, DAG.getDataLayout(), - !DCI.isBeforeLegalize()); + EVT ShiftVT = + getShiftAmountTy(OpVT, DAG.getDataLayout(), !DCI.isBeforeLegalize()); SDValue One = DAG.getConstant(1, DL, ShiftVT); SDValue YShl1 = DAG.getNode(ISD::SHL, DL, N1.getValueType(), Y, One); if (!DCI.isCalledByLegalizer()) @@ -3450,7 +3456,8 @@ // FIXME: Add vector support? Need to be careful with setcc result type below. SDValue CTPOP = N0; if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() && !VT.isVector() && - N0.getScalarValueSizeInBits() > Log2_32(N0.getOperand(0).getScalarValueSizeInBits())) + N0.getScalarValueSizeInBits() > + Log2_32(N0.getOperand(0).getScalarValueSizeInBits())) CTPOP = N0.getOperand(0); if (CTPOP.getOpcode() != ISD::CTPOP || !CTPOP.hasOneUse()) @@ -3585,8 +3592,8 @@ // (zext x) == C --> x == (trunc C) // (sext x) == C --> x == (trunc C) - if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && - DCI.isBeforeLegalize() && N0->hasOneUse()) { + if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && DCI.isBeforeLegalize() && + N0->hasOneUse()) { unsigned MinBits = N0.getValueSizeInBits(); SDValue PreExt; bool Signed = false; @@ -3597,7 +3604,7 @@ } else if (N0->getOpcode() == ISD::AND) { // DAGCombine turns costly ZExts into ANDs if (auto *C = dyn_cast(N0->getOperand(1))) - if ((C->getAPIntValue()+1).isPowerOf2()) { + if ((C->getAPIntValue() + 1).isPowerOf2()) { MinBits = C->getAPIntValue().countTrailingOnes(); PreExt = N0->getOperand(0); } @@ -3619,14 +3626,11 @@ } // Figure out how many bits we need to preserve this constant. - unsigned ReqdBits = Signed ? - C1.getBitWidth() - C1.getNumSignBits() + 1 : - C1.getActiveBits(); + unsigned ReqdBits = Signed ? C1.getBitWidth() - C1.getNumSignBits() + 1 + : C1.getActiveBits(); // Make sure we're not losing bits from the constant. - if (MinBits > 0 && - MinBits < C1.getBitWidth() && - MinBits >= ReqdBits) { + if (MinBits > 0 && MinBits < C1.getBitWidth() && MinBits >= ReqdBits) { EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits); if (isTypeDesirableForOp(ISD::SETCC, MinVT)) { // Will get folded away. @@ -3666,8 +3670,7 @@ cast(TopSetCC.getOperand(2))->get(), TopSetCC.getOperand(0).getValueType()); return DAG.getSetCC(dl, VT, TopSetCC.getOperand(0), - TopSetCC.getOperand(1), - InvCond); + TopSetCC.getOperand(1), InvCond); } } } @@ -3675,10 +3678,8 @@ // If the LHS is '(and load, const)', the RHS is 0, the test is for // equality or unsigned, and all 1 bits of the const are in the same // partial word, see if we can shorten the load. - if (DCI.isBeforeLegalize() && - !ISD::isSignedIntSetCC(Cond) && - N0.getOpcode() == ISD::AND && C1 == 0 && - N0.getNode()->hasOneUse() && + if (DCI.isBeforeLegalize() && !ISD::isSignedIntSetCC(Cond) && + N0.getOpcode() == ISD::AND && C1 == 0 && N0.getNode()->hasOneUse() && isa(N0.getOperand(0)) && N0.getOperand(0).getNode()->hasOneUse() && isa(N0.getOperand(1))) { @@ -3693,15 +3694,15 @@ if (Lod->getExtensionType() != ISD::NON_EXTLOAD) origWidth = Lod->getMemoryVT().getSizeInBits(); const APInt &Mask = N0.getConstantOperandAPInt(1); - for (unsigned width = origWidth / 2; width>=8; width /= 2) { + for (unsigned width = origWidth / 2; width >= 8; width /= 2) { APInt newMask = APInt::getLowBitsSet(maskWidth, width); - for (unsigned offset=0; offsetgetChain(), Ptr, Lod->getPointerInfo().getWithOffset(bestOffset), Lod->getOriginalAlign()); - return DAG.getSetCC(dl, VT, - DAG.getNode(ISD::AND, dl, newVT, NewLoad, - DAG.getConstant(bestMask.trunc(bestWidth), - dl, newVT)), - DAG.getConstant(0LL, dl, newVT), Cond); + return DAG.getSetCC( + dl, VT, + DAG.getNode( + ISD::AND, dl, newVT, NewLoad, + DAG.getConstant(bestMask.trunc(bestWidth), dl, newVT)), + DAG.getConstant(0LL, dl, newVT), Cond); } } } @@ -3775,8 +3777,8 @@ EVT NewSetCCVT = getSetCCResultType(Layout, *DAG.getContext(), newVT); SDValue NewConst = DAG.getConstant(C1.trunc(InSize), dl, newVT); - SDValue NewSetCC = DAG.getSetCC(dl, NewSetCCVT, N0.getOperand(0), - NewConst, Cond); + SDValue NewSetCC = + DAG.getSetCC(dl, NewSetCCVT, N0.getOperand(0), NewConst, Cond); return DAG.getBoolExtOrTrunc(NewSetCC, dl, VT, N0.getValueType()); } break; @@ -3809,13 +3811,13 @@ return DAG.getSetCC(dl, VT, ZextOp, DAG.getConstant(C1 & Imm, dl, ExtDstTy), Cond); } else if ((N1C->isNullValue() || N1C->isOne()) && - (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { + (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC - if (N0.getOpcode() == ISD::SETCC && - isTypeLegal(VT) && VT.bitsLE(N0.getValueType()) && + if (N0.getOpcode() == ISD::SETCC && isTypeLegal(VT) && + VT.bitsLE(N0.getValueType()) && (N0.getValueType() == MVT::i1 || getBooleanContents(N0.getOperand(0).getValueType()) == - ZeroOrOneBooleanContent)) { + ZeroOrOneBooleanContent)) { bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (!N1C->isOne()); if (TrueWhenTrue) return DAG.getNode(ISD::TRUNCATE, dl, VT, N0); @@ -3835,20 +3837,18 @@ // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We // can only do this if the top bits are known zero. unsigned BitWidth = N0.getValueSizeInBits(); - if (DAG.MaskedValueIsZero(N0, - APInt::getHighBitsSet(BitWidth, - BitWidth-1))) { + if (DAG.MaskedValueIsZero( + N0, APInt::getHighBitsSet(BitWidth, BitWidth - 1))) { // Okay, get the un-inverted input value. SDValue Val; if (N0.getOpcode() == ISD::XOR) { Val = N0.getOperand(0); } else { assert(N0.getOpcode() == ISD::AND && - N0.getOperand(0).getOpcode() == ISD::XOR); + N0.getOperand(0).getOpcode() == ISD::XOR); // ((X^1)&1)^1 -> X & 1 Val = DAG.getNode(ISD::AND, dl, N0.getValueType(), - N0.getOperand(0).getOperand(0), - N0.getOperand(1)); + N0.getOperand(0).getOperand(0), N0.getOperand(1)); } return DAG.getSetCC(dl, VT, Val, N1, @@ -3867,9 +3867,9 @@ // Ensure that the input setccs return an i1 type or 0/1 value. if (Op0.getValueType() == MVT::i1 || (getBooleanContents(XorLHS.getOperand(0).getValueType()) == - ZeroOrOneBooleanContent && + ZeroOrOneBooleanContent && getBooleanContents(XorRHS.getOperand(0).getValueType()) == - ZeroOrOneBooleanContent)) { + ZeroOrOneBooleanContent)) { // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc) Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ; return DAG.getSetCC(dl, VT, XorLHS, XorRHS, Cond); @@ -3878,13 +3878,15 @@ if (Op0.getOpcode() == ISD::AND && isOneConstant(Op0.getOperand(1))) { // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0. if (Op0.getValueType().bitsGT(VT)) - Op0 = DAG.getNode(ISD::AND, dl, VT, - DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)), - DAG.getConstant(1, dl, VT)); + Op0 = DAG.getNode( + ISD::AND, dl, VT, + DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)), + DAG.getConstant(1, dl, VT)); else if (Op0.getValueType().bitsLT(VT)) - Op0 = DAG.getNode(ISD::AND, dl, VT, - DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)), - DAG.getConstant(1, dl, VT)); + Op0 = DAG.getNode( + ISD::AND, dl, VT, + DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)), + DAG.getConstant(1, dl, VT)); return DAG.getSetCC(dl, VT, Op0, DAG.getConstant(0, dl, Op0.getValueType()), @@ -3956,8 +3958,7 @@ (!N1C->isOpaque() || (C.getBitWidth() <= 64 && isLegalICmpImmediate(C.getSExtValue())))) { return DAG.getSetCC(dl, VT, N0, - DAG.getConstant(C, dl, N1.getValueType()), - NewCC); + DAG.getConstant(C, dl, N1.getValueType()), NewCC); } } } @@ -3976,8 +3977,7 @@ (!N1C->isOpaque() || (C.getBitWidth() <= 64 && isLegalICmpImmediate(C.getSExtValue())))) { return DAG.getSetCC(dl, VT, N0, - DAG.getConstant(C, dl, N1.getValueType()), - NewCC); + DAG.getConstant(C, dl, N1.getValueType()), NewCC); } } } @@ -3993,7 +3993,7 @@ return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE); // If we have setult X, 1, turn it into seteq X, 0 - if (C1 == MinVal+1) + if (C1 == MinVal + 1) return DAG.getSetCC(dl, VT, N0, DAG.getConstant(MinVal, dl, N0.getValueType()), ISD::SETEQ); @@ -4011,7 +4011,7 @@ return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE); // If we have setugt X, Max-1, turn it into seteq X, Max - if (C1 == MaxVal-1) + if (C1 == MaxVal - 1) return DAG.getSetCC(dl, VT, N0, DAG.getConstant(MaxVal, dl, N0.getValueType()), ISD::SETEQ); @@ -4095,9 +4095,8 @@ // SETUGE X, SINTMIN -> SETLT X, 0 if ((Cond == ISD::SETUGT && C1.isMaxSignedValue()) || (Cond == ISD::SETUGE && C1.isMinSignedValue())) - return DAG.getSetCC(dl, VT, N0, - DAG.getConstant(0, dl, N1.getValueType()), - ISD::SETLT); + return DAG.getSetCC( + dl, VT, N0, DAG.getConstant(0, dl, N1.getValueType()), ISD::SETLT); // SETULT X, SINTMIN -> SETGT X, -1 // SETULE X, SINTMAX -> SETGT X, -1 @@ -4128,7 +4127,7 @@ if (auto *AndRHS = dyn_cast(N0.getOperand(1))) { EVT ShiftTy = getShiftAmountTy(ShValTy, Layout, !DCI.isBeforeLegalize()); - if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 + if (Cond == ISD::SETNE && C1 == 0) { // (X & 8) != 0 --> (X & 8) >> 3 // Perform the xform if the AND RHS is a single bit. unsigned ShCt = AndRHS->getAPIntValue().logBase2(); if (AndRHS->getAPIntValue().isPowerOf2() && @@ -4163,8 +4162,8 @@ unsigned ShiftBits = AndRHSC.countTrailingZeros(); if (!TLI.shouldAvoidTransformToShift(ShValTy, ShiftBits)) { SDValue Shift = - DAG.getNode(ISD::SRL, dl, ShValTy, N0.getOperand(0), - DAG.getConstant(ShiftBits, dl, ShiftTy)); + DAG.getNode(ISD::SRL, dl, ShValTy, N0.getOperand(0), + DAG.getConstant(ShiftBits, dl, ShiftTy)); SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), dl, ShValTy); return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond); } @@ -4231,11 +4230,20 @@ bool IsNegInf = CFP->getValueAPF().isNegative(); ISD::CondCode NewCond = ISD::SETCC_INVALID; switch (Cond) { - case ISD::SETOEQ: NewCond = IsNegInf ? ISD::SETOLE : ISD::SETOGE; break; - case ISD::SETUEQ: NewCond = IsNegInf ? ISD::SETULE : ISD::SETUGE; break; - case ISD::SETUNE: NewCond = IsNegInf ? ISD::SETUGT : ISD::SETULT; break; - case ISD::SETONE: NewCond = IsNegInf ? ISD::SETOGT : ISD::SETOLT; break; - default: break; + case ISD::SETOEQ: + NewCond = IsNegInf ? ISD::SETOLE : ISD::SETOGE; + break; + case ISD::SETUEQ: + NewCond = IsNegInf ? ISD::SETULE : ISD::SETUGE; + break; + case ISD::SETUNE: + NewCond = IsNegInf ? ISD::SETUGT : ISD::SETULT; + break; + case ISD::SETONE: + NewCond = IsNegInf ? ISD::SETOGT : ISD::SETOLT; + break; + default: + break; } if (NewCond != ISD::SETCC_INVALID && isCondCodeLegal(NewCond, N0.getSimpleValueType())) @@ -4259,8 +4267,7 @@ // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO // if it is not already. ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO; - if (NewCond != Cond && - (DCI.isBeforeLegalizeOps() || + if (NewCond != Cond && (DCI.isBeforeLegalizeOps() || isCondCodeLegal(NewCond, N0.getSimpleValueType()))) return DAG.getSetCC(dl, VT, N0, N1, NewCond); } @@ -4294,10 +4301,11 @@ if (auto *LHSR = dyn_cast(N0.getOperand(1))) { // Turn (X+C1) == C2 --> X == C2-C1 if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) { - return DAG.getSetCC(dl, VT, N0.getOperand(0), - DAG.getConstant(RHSC->getAPIntValue()- - LHSR->getAPIntValue(), - dl, N0.getValueType()), Cond); + return DAG.getSetCC( + dl, VT, N0.getOperand(0), + DAG.getConstant(RHSC->getAPIntValue() - LHSR->getAPIntValue(), + dl, N0.getValueType()), + Cond); } // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. @@ -4305,23 +4313,21 @@ // If we know that all of the inverted bits are zero, don't bother // performing the inversion. if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue())) - return - DAG.getSetCC(dl, VT, N0.getOperand(0), - DAG.getConstant(LHSR->getAPIntValue() ^ - RHSC->getAPIntValue(), - dl, N0.getValueType()), - Cond); + return DAG.getSetCC( + dl, VT, N0.getOperand(0), + DAG.getConstant(LHSR->getAPIntValue() ^ RHSC->getAPIntValue(), + dl, N0.getValueType()), + Cond); } // Turn (C1-X) == C2 --> X == C1-C2 if (auto *SUBC = dyn_cast(N0.getOperand(0))) { if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) { - return - DAG.getSetCC(dl, VT, N0.getOperand(1), - DAG.getConstant(SUBC->getAPIntValue() - - RHSC->getAPIntValue(), - dl, N0.getValueType()), - Cond); + return DAG.getSetCC( + dl, VT, N0.getOperand(1), + DAG.getConstant(SUBC->getAPIntValue() - RHSC->getAPIntValue(), + dl, N0.getValueType()), + Cond); } } @@ -4370,14 +4376,15 @@ if (N0.getValueType().getScalarType() == MVT::i1 && foldBooleans) { SDValue Temp; switch (Cond) { - default: llvm_unreachable("Unknown integer setcc!"); - case ISD::SETEQ: // X == Y -> ~(X^Y) + default: + llvm_unreachable("Unknown integer setcc!"); + case ISD::SETEQ: // X == Y -> ~(X^Y) Temp = DAG.getNode(ISD::XOR, dl, OpVT, N0, N1); N0 = DAG.getNOT(dl, Temp, OpVT); if (!DCI.isCalledByLegalizer()) DCI.AddToWorklist(Temp.getNode()); break; - case ISD::SETNE: // X != Y --> (X^Y) + case ISD::SETNE: // X != Y --> (X^Y) N0 = DAG.getNode(ISD::XOR, dl, OpVT, N0, N1); break; case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y @@ -4469,7 +4476,8 @@ if (S == 1) { switch (Constraint[0]) { - default: break; + default: + break; case 'r': return C_RegisterClass; case 'm': // memory @@ -4530,21 +4538,23 @@ std::vector &Ops, SelectionDAG &DAG) const { - if (Constraint.length() > 1) return; + if (Constraint.length() > 1) + return; char ConstraintLetter = Constraint[0]; switch (ConstraintLetter) { - default: break; - case 'X': // Allows any operand; labels (basic block) use this. + default: + break; + case 'X': // Allows any operand; labels (basic block) use this. if (Op.getOpcode() == ISD::BasicBlock || Op.getOpcode() == ISD::TargetBlockAddress) { Ops.push_back(Op); return; } LLVM_FALLTHROUGH; - case 'i': // Simple Integer or Relocatable Constant - case 'n': // Simple Integer - case 's': { // Relocatable Constant + case 'i': // Simple Integer or Relocatable Constant + case 'n': // Simple Integer + case 's': { // Relocatable Constant GlobalAddressSDNode *GA; ConstantSDNode *C; @@ -4738,7 +4748,8 @@ if (!OpTy->isSingleValueType() && OpTy->isSized()) { unsigned BitSize = DL.getTypeSizeInBits(OpTy); switch (BitSize) { - default: break; + default: + break; case 1: case 8: case 16: @@ -4871,8 +4882,8 @@ /// This object must already have been set up with the operand type /// and the current alternative constraint selected. TargetLowering::ConstraintWeight - TargetLowering::getMultipleConstraintMatchWeight( - AsmOperandInfo &info, int maIndex) const { +TargetLowering::getMultipleConstraintMatchWeight(AsmOperandInfo &info, + int maIndex) const { InlineAsm::ConstraintCodeVector *rCodes; if (maIndex >= (int)info.multipleAlternatives.size()) rCodes = &info.Codes; @@ -4883,7 +4894,7 @@ // Loop over the options, keeping track of the most general one. for (unsigned i = 0, e = rCodes->size(); i != e; ++i) { ConstraintWeight weight = - getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str()); + getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str()); if (weight > BestWeight) BestWeight = weight; } @@ -4895,44 +4906,44 @@ /// This object must already have been set up with the operand type /// and the current alternative constraint selected. TargetLowering::ConstraintWeight - TargetLowering::getSingleConstraintMatchWeight( - AsmOperandInfo &info, const char *constraint) const { +TargetLowering::getSingleConstraintMatchWeight(AsmOperandInfo &info, + const char *constraint) const { ConstraintWeight weight = CW_Invalid; Value *CallOperandVal = info.CallOperandVal; - // If we don't have a value, we can't do a match, - // but allow it at the lowest weight. + // If we don't have a value, we can't do a match, + // but allow it at the lowest weight. if (!CallOperandVal) return CW_Default; // Look at the constraint type. switch (*constraint) { - case 'i': // immediate integer. - case 'n': // immediate integer with a known value. - if (isa(CallOperandVal)) - weight = CW_Constant; - break; - case 's': // non-explicit intregal immediate. - if (isa(CallOperandVal)) - weight = CW_Constant; - break; - case 'E': // immediate float if host format. - case 'F': // immediate float. - if (isa(CallOperandVal)) - weight = CW_Constant; - break; - case '<': // memory operand with autodecrement. - case '>': // memory operand with autoincrement. - case 'm': // memory operand. - case 'o': // offsettable memory operand - case 'V': // non-offsettable memory operand - weight = CW_Memory; - break; - case 'r': // general register. - case 'g': // general register, memory operand or immediate integer. - // note: Clang converts "g" to "imr". - if (CallOperandVal->getType()->isIntegerTy()) - weight = CW_Register; - break; - case 'X': // any operand. + case 'i': // immediate integer. + case 'n': // immediate integer with a known value. + if (isa(CallOperandVal)) + weight = CW_Constant; + break; + case 's': // non-explicit intregal immediate. + if (isa(CallOperandVal)) + weight = CW_Constant; + break; + case 'E': // immediate float if host format. + case 'F': // immediate float. + if (isa(CallOperandVal)) + weight = CW_Constant; + break; + case '<': // memory operand with autodecrement. + case '>': // memory operand with autoincrement. + case 'm': // memory operand. + case 'o': // offsettable memory operand + case 'V': // non-offsettable memory operand + weight = CW_Memory; + break; + case 'r': // general register. + case 'g': // general register, memory operand or immediate integer. + // note: Clang converts "g" to "imr". + if (CallOperandVal->getType()->isIntegerTy()) + weight = CW_Register; + break; + case 'X': // any operand. default: weight = CW_Default; break; @@ -4961,8 +4972,8 @@ /// 'm' over 'r', for example. /// static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo, - const TargetLowering &TLI, - SDValue Op, SelectionDAG *DAG) { + const TargetLowering &TLI, SDValue Op, + SelectionDAG *DAG) { assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options"); unsigned BestIdx = 0; TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown; @@ -4971,7 +4982,7 @@ // Loop over the options, keeping track of the most general one. for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) { TargetLowering::ConstraintType CType = - TLI.getConstraintType(OpInfo.Codes[i]); + TLI.getConstraintType(OpInfo.Codes[i]); // Indirect 'other' or 'immediate' constraints are not allowed. if (OpInfo.isIndirect && !(CType == TargetLowering::C_Memory || @@ -4984,12 +4995,12 @@ // the operand is an integer in the range [0..31] we want to use I (saving a // load of a register), otherwise we must use 'r'. if ((CType == TargetLowering::C_Other || - CType == TargetLowering::C_Immediate) && Op.getNode()) { + CType == TargetLowering::C_Immediate) && + Op.getNode()) { assert(OpInfo.Codes[i].size() == 1 && "Unhandled multi-letter 'other' constraint"); std::vector ResultOps; - TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i], - ResultOps, *DAG); + TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i], ResultOps, *DAG); if (!ResultOps.empty()) { BestType = CType; BestIdx = i; @@ -5017,8 +5028,7 @@ /// Determines the constraint code and constraint type to use for the specific /// AsmOperandInfo, setting OpInfo.ConstraintCode and OpInfo.ConstraintType. -void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo, - SDValue Op, +void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo, SDValue Op, SelectionDAG *DAG) const { assert(!OpInfo.Codes.empty() && "Must have at least one constraint"); @@ -5121,7 +5131,8 @@ return DAG.getNode(ISD::MUL, dl, VT, Res, Factor); } -SDValue TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor, +SDValue +TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor, SelectionDAG &DAG, SmallVectorImpl &Created) const { AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes(); @@ -5131,6 +5142,112 @@ return SDValue(); } +namespace { +/// Magic data for optimising signed division by a constant. +struct ms { + APInt m; ///< magic number + unsigned s; ///< shift amount +}; + +/// Magic data for optimising unsigned division by a constant. +struct mu { + APInt m; ///< magic number + bool a; ///< add indicator + unsigned s; ///< shift amount +}; +} // namespace + +/// Calculate the magic numbers required to implement an unsigned integer +/// division by a constant as a sequence of multiplies, adds and shifts. +/// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry +/// S. Warren, Jr., chapter 10. +/// LeadingZeros can be used to simplify the calculation if the upper bits +/// of the divided value are known zero. +static mu magicu(const APInt &d, unsigned LeadingZeros = 0) { + unsigned p; + APInt nc, delta, q1, r1, q2, r2; + struct mu magu; + magu.a = 0; // initialize "add" indicator + APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()).lshr(LeadingZeros); + APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); + APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth()); + + nc = allOnes - (allOnes - d).urem(d); + p = d.getBitWidth() - 1; // initialize p + q1 = signedMin.udiv(nc); // initialize q1 = 2p/nc + r1 = signedMin - q1 * nc; // initialize r1 = rem(2p,nc) + q2 = signedMax.udiv(d); // initialize q2 = (2p-1)/d + r2 = signedMax - q2 * d; // initialize r2 = rem((2p-1),d) + do { + p = p + 1; + if (r1.uge(nc - r1)) { + q1 = q1 + q1 + 1; // update q1 + r1 = r1 + r1 - nc; // update r1 + } else { + q1 = q1 + q1; // update q1 + r1 = r1 + r1; // update r1 + } + if ((r2 + 1).uge(d - r2)) { + if (q2.uge(signedMax)) + magu.a = 1; + q2 = q2 + q2 + 1; // update q2 + r2 = r2 + r2 + 1 - d; // update r2 + } else { + if (q2.uge(signedMin)) + magu.a = 1; + q2 = q2 + q2; // update q2 + r2 = r2 + r2 + 1; // update r2 + } + delta = d - 1 - r2; + } while (p < d.getBitWidth() * 2 && + (q1.ult(delta) || (q1 == delta && r1 == 0))); + magu.m = q2 + 1; // resulting magic number + magu.s = p - d.getBitWidth(); // resulting shift + return magu; +} + +/// Calculate the magic numbers required to implement a signed integer division +/// by a constant as a sequence of multiplies, adds and shifts. Requires that +/// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S. +/// Warren, Jr., Chapter 10. +static ms magic(const APInt &d) { + unsigned p; + APInt ad, anc, delta, q1, r1, q2, r2, t; + APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); + struct ms mag; + + ad = d.abs(); + t = signedMin + (d.lshr(d.getBitWidth() - 1)); + anc = t - 1 - t.urem(ad); // absolute value of nc + p = d.getBitWidth() - 1; // initialize p + q1 = signedMin.udiv(anc); // initialize q1 = 2p/abs(nc) + r1 = signedMin - q1 * anc; // initialize r1 = rem(2p,abs(nc)) + q2 = signedMin.udiv(ad); // initialize q2 = 2p/abs(d) + r2 = signedMin - q2 * ad; // initialize r2 = rem(2p,abs(d)) + do { + p = p + 1; + q1 = q1 << 1; // update q1 = 2p/abs(nc) + r1 = r1 << 1; // update r1 = rem(2p/abs(nc)) + if (r1.uge(anc)) { // must be unsigned comparison + q1 = q1 + 1; + r1 = r1 - anc; + } + q2 = q2 << 1; // update q2 = 2p/abs(d) + r2 = r2 << 1; // update r2 = rem(2p/abs(d)) + if (r2.uge(ad)) { // must be unsigned comparison + q2 = q2 + 1; + r2 = r2 - ad; + } + delta = ad - r2; + } while (q1.ult(delta) || (q1 == delta && r1 == 0)); + + mag.m = q2 + 1; + if (d.isNegative()) + mag.m = -mag.m; // resulting magic number + mag.s = p - d.getBitWidth(); // resulting shift + return mag; +} + /// Given an ISD::SDIV node expressing a divide by constant, /// return a DAG expression to select that will generate the same value by /// multiplying by a magic number. @@ -5175,7 +5292,7 @@ return false; const APInt &Divisor = C->getAPIntValue(); - APInt::ms magics = Divisor.magic(); + ms magics = magic(Divisor); int NumeratorFactor = 0; int ShiftMask = -1; @@ -5320,8 +5437,8 @@ return false; // FIXME: We should use a narrower constant when the upper // bits are known to be zero. - const APInt& Divisor = C->getAPIntValue(); - APInt::mu magics = Divisor.magicu(); + const APInt &Divisor = C->getAPIntValue(); + mu magics = magicu(Divisor); unsigned PreShift = 0, PostShift = 0; // If the divisor is even, we can avoid using the expensive fixup by @@ -5329,7 +5446,7 @@ if (magics.a != 0 && !Divisor[0]) { PreShift = Divisor.countTrailingZeros(); // Get magic number for the shifted divisor. - magics = Divisor.lshr(PreShift).magicu(PreShift); + magics = magicu(Divisor.lshr(PreShift), PreShift); assert(magics.a == 0 && "Should use cheap fixup now"); } @@ -5992,8 +6109,8 @@ return Blended; } -bool TargetLowering:: -verifyReturnAddressArgumentIsConstant(SDValue Op, SelectionDAG &DAG) const { +bool TargetLowering::verifyReturnAddressArgumentIsConstant( + SDValue Op, SelectionDAG &DAG) const { if (!isa(Op.getOperand(0))) { DAG.getContext()->emitError("argument to '__builtin_return_address' must " "be a constant integer"); @@ -6772,8 +6889,7 @@ } bool TargetLowering::expandFP_TO_UINT(SDNode *Node, SDValue &Result, - SDValue &Chain, - SelectionDAG &DAG) const { + SDValue &Chain, SelectionDAG &DAG) const { SDLoc dl(SDValue(Node, 0)); unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0; SDValue Src = Node->getOperand(OpNo); @@ -6786,8 +6902,8 @@ getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), DstVT); // Only expand vector types if we have the appropriate vector bit operations. - unsigned SIntOpcode = Node->isStrictFPOpcode() ? ISD::STRICT_FP_TO_SINT : - ISD::FP_TO_SINT; + unsigned SIntOpcode = + Node->isStrictFPOpcode() ? ISD::STRICT_FP_TO_SINT : ISD::FP_TO_SINT; if (DstVT.isVector() && (!isOperationLegalOrCustom(SIntOpcode, DstVT) || !isOperationLegalOrCustomOrPromote(ISD::XOR, SrcVT))) return false; @@ -6801,8 +6917,8 @@ if (APFloat::opOverflow & APF.convertFromAPInt(SignMask, false, APFloat::rmNearestTiesToEven)) { if (Node->isStrictFPOpcode()) { - Result = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl, { DstVT, MVT::Other }, - { Node->getOperand(0), Src }); + Result = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl, {DstVT, MVT::Other}, + {Node->getOperand(0), Src}); Chain = Result.getValue(1); } else Result = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, Src); @@ -6818,8 +6934,8 @@ SDValue Sel; if (Node->isStrictFPOpcode()) { - Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT, - Node->getOperand(0), /*IsSignaling*/ true); + Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT, Node->getOperand(0), + /*IsSignaling*/ true); Chain = Sel.getValue(1); } else { Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT); @@ -6837,18 +6953,18 @@ // Result = fp_to_sint(Src - FltOfs) ^ IntOfs // TODO: Should any fast-math-flags be set for the FSUB? - SDValue FltOfs = DAG.getSelect(dl, SrcVT, Sel, - DAG.getConstantFP(0.0, dl, SrcVT), Cst); + SDValue FltOfs = + DAG.getSelect(dl, SrcVT, Sel, DAG.getConstantFP(0.0, dl, SrcVT), Cst); Sel = DAG.getBoolExtOrTrunc(Sel, dl, DstSetCCVT, DstVT); - SDValue IntOfs = DAG.getSelect(dl, DstVT, Sel, - DAG.getConstant(0, dl, DstVT), - DAG.getConstant(SignMask, dl, DstVT)); + SDValue IntOfs = + DAG.getSelect(dl, DstVT, Sel, DAG.getConstant(0, dl, DstVT), + DAG.getConstant(SignMask, dl, DstVT)); SDValue SInt; if (Node->isStrictFPOpcode()) { - SDValue Val = DAG.getNode(ISD::STRICT_FSUB, dl, { SrcVT, MVT::Other }, - { Chain, Src, FltOfs }); - SInt = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl, { DstVT, MVT::Other }, - { Val.getValue(1), Val }); + SDValue Val = DAG.getNode(ISD::STRICT_FSUB, dl, {SrcVT, MVT::Other}, + {Chain, Src, FltOfs}); + SInt = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl, {DstVT, MVT::Other}, + {Val.getValue(1), Val}); Chain = SInt.getValue(1); } else { SDValue Val = DAG.getNode(ISD::FSUB, dl, SrcVT, Src, FltOfs); @@ -6874,8 +6990,7 @@ } bool TargetLowering::expandUINT_TO_FP(SDNode *Node, SDValue &Result, - SDValue &Chain, - SelectionDAG &DAG) const { + SDValue &Chain, SelectionDAG &DAG) const { // This transform is not correct for converting 0 when rounding mode is set // to round toward negative infinity which will produce -0.0. So disable under // strictfp. @@ -6906,8 +7021,8 @@ // when rounding toward negative infinity. In that case the fsub will produce // -0.0. This will be added to +0.0 and produce -0.0 which is incorrect. SDValue TwoP52 = DAG.getConstant(UINT64_C(0x4330000000000000), dl, SrcVT); - SDValue TwoP84PlusTwoP52 = DAG.getConstantFP( - BitsToDouble(UINT64_C(0x4530000000100000)), dl, DstVT); + SDValue TwoP84PlusTwoP52 = + DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), dl, DstVT); SDValue TwoP84 = DAG.getConstant(UINT64_C(0x4530000000000000), dl, SrcVT); SDValue LoMask = DAG.getConstant(UINT64_C(0x00000000FFFFFFFF), dl, SrcVT); SDValue HiShift = DAG.getConstant(32, dl, ShiftVT); @@ -6918,8 +7033,7 @@ SDValue HiOr = DAG.getNode(ISD::OR, dl, SrcVT, Hi, TwoP84); SDValue LoFlt = DAG.getBitcast(DstVT, LoOr); SDValue HiFlt = DAG.getBitcast(DstVT, HiOr); - SDValue HiSub = - DAG.getNode(ISD::FSUB, dl, DstVT, HiFlt, TwoP84PlusTwoP52); + SDValue HiSub = DAG.getNode(ISD::FSUB, dl, DstVT, HiFlt, TwoP84PlusTwoP52); Result = DAG.getNode(ISD::FADD, dl, DstVT, LoFlt, HiSub); return true; } @@ -6927,8 +7041,8 @@ SDValue TargetLowering::expandFMINNUM_FMAXNUM(SDNode *Node, SelectionDAG &DAG) const { SDLoc dl(Node); - unsigned NewOp = Node->getOpcode() == ISD::FMINNUM ? - ISD::FMINNUM_IEEE : ISD::FMAXNUM_IEEE; + unsigned NewOp = + Node->getOpcode() == ISD::FMINNUM ? ISD::FMINNUM_IEEE : ISD::FMAXNUM_IEEE; EVT VT = Node->getValueType(0); if (VT.isScalableVector()) @@ -6943,12 +7057,12 @@ // Insert canonicalizes if it's possible we need to quiet to get correct // sNaN behavior. if (!DAG.isKnownNeverSNaN(Quiet0)) { - Quiet0 = DAG.getNode(ISD::FCANONICALIZE, dl, VT, Quiet0, - Node->getFlags()); + Quiet0 = + DAG.getNode(ISD::FCANONICALIZE, dl, VT, Quiet0, Node->getFlags()); } if (!DAG.isKnownNeverSNaN(Quiet1)) { - Quiet1 = DAG.getNode(ISD::FCANONICALIZE, dl, VT, Quiet1, - Node->getFlags()); + Quiet1 = + DAG.getNode(ISD::FCANONICALIZE, dl, VT, Quiet1, Node->getFlags()); } } @@ -7157,8 +7271,8 @@ return true; } -bool TargetLowering::expandABS(SDNode *N, SDValue &Result, - SelectionDAG &DAG, bool IsNegative) const { +bool TargetLowering::expandABS(SDNode *N, SDValue &Result, SelectionDAG &DAG, + bool IsNegative) const { SDLoc dl(N); EVT VT = N->getValueType(0); EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout()); @@ -7234,8 +7348,8 @@ Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, dl, SHVT)); Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, dl, SHVT)); - Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, - DAG.getConstant(0xFF0000, dl, VT)); + Tmp3 = + DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, dl, VT)); Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, dl, VT)); Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1); @@ -7250,17 +7364,17 @@ Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, dl, SHVT)); Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, dl, SHVT)); Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, - DAG.getConstant(255ULL<<48, dl, VT)); + DAG.getConstant(255ULL << 48, dl, VT)); Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, - DAG.getConstant(255ULL<<40, dl, VT)); + DAG.getConstant(255ULL << 40, dl, VT)); Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, - DAG.getConstant(255ULL<<32, dl, VT)); + DAG.getConstant(255ULL << 32, dl, VT)); Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, - DAG.getConstant(255ULL<<24, dl, VT)); + DAG.getConstant(255ULL << 24, dl, VT)); Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, - DAG.getConstant(255ULL<<16, dl, VT)); + DAG.getConstant(255ULL << 16, dl, VT)); Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, - DAG.getConstant(255ULL<<8 , dl, VT)); + DAG.getConstant(255ULL << 8, dl, VT)); Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7); Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5); Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); @@ -7316,7 +7430,7 @@ } Tmp = DAG.getConstant(0, dl, VT); - for (unsigned I = 0, J = Sz-1; I < Sz; ++I, --J) { + for (unsigned I = 0, J = Sz - 1; I < Sz; ++I, --J) { if (I < J) Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(J - I, dl, SHVT)); @@ -7334,8 +7448,7 @@ } std::pair -TargetLowering::scalarizeVectorLoad(LoadSDNode *LD, - SelectionDAG &DAG) const { +TargetLowering::scalarizeVectorLoad(LoadSDNode *LD, SelectionDAG &DAG) const { SDLoc SL(LD); SDValue Chain = LD->getChain(); SDValue BasePTR = LD->getBasePtr(); @@ -7515,20 +7628,19 @@ if (VT.isFloatingPoint() || VT.isVector()) { EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits()); if (isTypeLegal(intVT) && isTypeLegal(LoadedVT)) { - if (!isOperationLegalOrCustom(ISD::LOAD, intVT) && - LoadedVT.isVector()) { + if (!isOperationLegalOrCustom(ISD::LOAD, intVT) && LoadedVT.isVector()) { // Scalarize the load and let the individual components be handled. return scalarizeVectorLoad(LD, DAG); } // Expand to a (misaligned) integer load of the same size, // then bitconvert to floating point or vector. - SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, - LD->getMemOperand()); + SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getMemOperand()); SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad); if (LoadedVT != VT) - Result = DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND : - ISD::ANY_EXTEND, dl, VT, Result); + Result = + DAG.getNode(VT.isFloatingPoint() ? ISD::FP_EXTEND : ISD::ANY_EXTEND, + dl, VT, Result); return std::make_pair(Result, newLoad.getValue(1)); } @@ -7572,8 +7684,8 @@ } // The last copy may be partial. Do an extending load. - EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), - 8 * (LoadedBytes - Offset)); + EVT MemVT = + EVT::getIntegerVT(*DAG.getContext(), 8 * (LoadedBytes - Offset)); SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr, LD->getPointerInfo().getWithOffset(Offset), MemVT, @@ -7605,7 +7717,7 @@ // integer MVT. unsigned NumBits = LoadedVT.getSizeInBits(); EVT NewLoadedVT; - NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2); + NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits / 2); NumBits >>= 1; Align Alignment = LD->getOriginalAlign(); @@ -7641,14 +7753,13 @@ } // aggregate the two parts - SDValue ShiftAmount = - DAG.getConstant(NumBits, dl, getShiftAmountTy(Hi.getValueType(), - DAG.getDataLayout())); + SDValue ShiftAmount = DAG.getConstant( + NumBits, dl, getShiftAmountTy(Hi.getValueType(), DAG.getDataLayout())); SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount); Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo); SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), - Hi.getValue(1)); + Hi.getValue(1)); return std::make_pair(Result, TF); } @@ -7778,11 +7889,10 @@ return Result; } -SDValue -TargetLowering::IncrementMemoryAddress(SDValue Addr, SDValue Mask, - const SDLoc &DL, EVT DataVT, - SelectionDAG &DAG, - bool IsCompressedMemory) const { +SDValue TargetLowering::IncrementMemoryAddress(SDValue Addr, SDValue Mask, + const SDLoc &DL, EVT DataVT, + SelectionDAG &DAG, + bool IsCompressedMemory) const { SDValue Increment; EVT AddrVT = Addr.getValueType(); EVT MaskVT = Mask.getValueType(); @@ -7793,7 +7903,8 @@ report_fatal_error( "Cannot currently handle compressed memory with scalable vectors"); // Incrementing the pointer according to number of '1's in the mask. - EVT MaskIntVT = EVT::getIntegerVT(*DAG.getContext(), MaskVT.getSizeInBits()); + EVT MaskIntVT = + EVT::getIntegerVT(*DAG.getContext(), MaskVT.getSizeInBits()); SDValue MaskInIntReg = DAG.getBitcast(MaskIntVT, Mask); if (MaskIntVT.getSizeInBits() < 32) { MaskInIntReg = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, MaskInIntReg); @@ -7804,8 +7915,8 @@ Increment = DAG.getNode(ISD::CTPOP, DL, MaskIntVT, MaskInIntReg); Increment = DAG.getZExtOrTrunc(Increment, DL, AddrVT); // Scale is an element size in bytes. - SDValue Scale = DAG.getConstant(DataVT.getScalarSizeInBits() / 8, DL, - AddrVT); + SDValue Scale = + DAG.getConstant(DataVT.getScalarSizeInBits() / 8, DL, AddrVT); Increment = DAG.getNode(ISD::MUL, DL, AddrVT, Increment, Scale); } else if (DataVT.isScalableVector()) { Increment = DAG.getVScale(DL, AddrVT, @@ -7869,7 +7980,8 @@ EVT EltVT = VecVT.getVectorElementType(); // Calculate the element offset and add it to the pointer. - unsigned EltSize = EltVT.getFixedSizeInBits() / 8; // FIXME: should be ABI size. + unsigned EltSize = + EltVT.getFixedSizeInBits() / 8; // FIXME: should be ABI size. assert(EltSize * 8 == EltVT.getFixedSizeInBits() && "Converting bits to bytes lost precision"); @@ -7903,7 +8015,7 @@ ArgListTy Args; ArgListEntry Entry; std::string NameString = ("__emutls_v." + GA->getGlobal()->getName()).str(); - Module *VariableModule = const_cast(GA->getGlobal()->getParent()); + Module *VariableModule = const_cast(GA->getGlobal()->getParent()); StringRef EmuTlsVarName(NameString); GlobalVariable *EmuTlsVar = VariableModule->getNamedGlobal(EmuTlsVarName); assert(EmuTlsVar && "Cannot find EmuTlsVar "); @@ -7997,11 +8109,20 @@ // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B ISD::CondCode CC; switch (Opcode) { - default: llvm_unreachable("How did we get here?"); - case ISD::SMAX: CC = ISD::SETGT; break; - case ISD::SMIN: CC = ISD::SETLT; break; - case ISD::UMAX: CC = ISD::SETUGT; break; - case ISD::UMIN: CC = ISD::SETULT; break; + default: + llvm_unreachable("How did we get here?"); + case ISD::SMAX: + CC = ISD::SETGT; + break; + case ISD::SMIN: + CC = ISD::SETLT; + break; + case ISD::UMAX: + CC = ISD::SETUGT; + break; + case ISD::UMIN: + CC = ISD::SETULT; + break; } // FIXME: Should really try to split the vector in case it's legal on a @@ -8062,7 +8183,8 @@ unsigned BitWidth = LHS.getScalarValueSizeInBits(); EVT BoolVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); - SDValue Result = DAG.getNode(OverflowOp, dl, DAG.getVTList(VT, BoolVT), LHS, RHS); + SDValue Result = + DAG.getNode(OverflowOp, dl, DAG.getVTList(VT, BoolVT), LHS, RHS); SDValue SumDiff = Result.getValue(0); SDValue Overflow = Result.getValue(1); SDValue Zero = DAG.getConstant(0, dl, VT); @@ -8108,7 +8230,7 @@ assert((Node->getOpcode() == ISD::SSHLSAT || Node->getOpcode() == ISD::USHLSAT) && - "Expected a SHLSAT opcode"); + "Expected a SHLSAT opcode"); assert(VT == RHS.getValueType() && "Expected operands to be the same type"); assert(VT.isInteger() && "Expected operands to be integers"); @@ -8123,8 +8245,8 @@ if (IsSigned) { SDValue SatMin = DAG.getConstant(APInt::getSignedMinValue(BW), dl, VT); SDValue SatMax = DAG.getConstant(APInt::getSignedMaxValue(BW), dl, VT); - SatVal = DAG.getSelectCC(dl, LHS, DAG.getConstant(0, dl, VT), - SatMin, SatMax, ISD::SETLT); + SatVal = DAG.getSelectCC(dl, LHS, DAG.getConstant(0, dl, VT), SatMin, + SatMax, ISD::SETLT); } else { SatVal = DAG.getConstant(APInt::getMaxValue(BW), dl, VT); } @@ -8133,8 +8255,8 @@ return Result; } -SDValue -TargetLowering::expandFixedPointMul(SDNode *Node, SelectionDAG &DAG) const { +SDValue TargetLowering::expandFixedPointMul(SDNode *Node, + SelectionDAG &DAG) const { assert((Node->getOpcode() == ISD::SMULFIX || Node->getOpcode() == ISD::UMULFIX || Node->getOpcode() == ISD::SMULFIXSAT || @@ -8232,11 +8354,10 @@ // Saturate to max if ((Hi >> Scale) != 0), // which is the same as if (Hi > ((1 << Scale) - 1)) APInt MaxVal = APInt::getMaxValue(VTSize); - SDValue LowMask = DAG.getConstant(APInt::getLowBitsSet(VTSize, Scale), - dl, VT); - Result = DAG.getSelectCC(dl, Hi, LowMask, - DAG.getConstant(MaxVal, dl, VT), Result, - ISD::SETUGT); + SDValue LowMask = + DAG.getConstant(APInt::getLowBitsSet(VTSize, Scale), dl, VT); + Result = DAG.getSelectCC(dl, Hi, LowMask, DAG.getConstant(MaxVal, dl, VT), + Result, ISD::SETUGT); return Result; } @@ -8254,8 +8375,8 @@ // Saturated to SatMin if wide product is negative, and SatMax if wide // product is positive ... SDValue Zero = DAG.getConstant(0, dl, VT); - SDValue ResultIfOverflow = DAG.getSelectCC(dl, Hi, Zero, SatMin, SatMax, - ISD::SETLT); + SDValue ResultIfOverflow = + DAG.getSelectCC(dl, Hi, Zero, SatMin, SatMax, ISD::SETLT); // ... but only if we overflowed. return DAG.getSelect(dl, VT, Overflow, ResultIfOverflow, Result); } @@ -8264,22 +8385,21 @@ // Saturate to max if ((Hi >> (Scale - 1)) > 0), // which is the same as if (Hi > (1 << (Scale - 1)) - 1) - SDValue LowMask = DAG.getConstant(APInt::getLowBitsSet(VTSize, Scale - 1), - dl, VT); + SDValue LowMask = + DAG.getConstant(APInt::getLowBitsSet(VTSize, Scale - 1), dl, VT); Result = DAG.getSelectCC(dl, Hi, LowMask, SatMax, Result, ISD::SETGT); // Saturate to min if (Hi >> (Scale - 1)) < -1), // which is the same as if (HI < (-1 << (Scale - 1)) - SDValue HighMask = - DAG.getConstant(APInt::getHighBitsSet(VTSize, VTSize - Scale + 1), - dl, VT); + SDValue HighMask = DAG.getConstant( + APInt::getHighBitsSet(VTSize, VTSize - Scale + 1), dl, VT); Result = DAG.getSelectCC(dl, Hi, HighMask, SatMin, Result, ISD::SETLT); return Result; } -SDValue -TargetLowering::expandFixedPointDiv(unsigned Opcode, const SDLoc &dl, - SDValue LHS, SDValue RHS, - unsigned Scale, SelectionDAG &DAG) const { +SDValue TargetLowering::expandFixedPointDiv(unsigned Opcode, const SDLoc &dl, + SDValue LHS, SDValue RHS, + unsigned Scale, + SelectionDAG &DAG) const { assert((Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIX || Opcode == ISD::UDIVFIXSAT) && "Expected a fixed point division opcode"); @@ -8333,38 +8453,33 @@ // FIXME: Ideally we would always produce an SDIVREM here, but if the // type isn't legal, SDIVREM cannot be expanded. There is no reason why // we couldn't just form a libcall, but the type legalizer doesn't do it. - if (isTypeLegal(VT) && - isOperationLegalOrCustom(ISD::SDIVREM, VT)) { - Quot = DAG.getNode(ISD::SDIVREM, dl, - DAG.getVTList(VT, VT), - LHS, RHS); + if (isTypeLegal(VT) && isOperationLegalOrCustom(ISD::SDIVREM, VT)) { + Quot = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), LHS, RHS); Rem = Quot.getValue(1); Quot = Quot.getValue(0); } else { - Quot = DAG.getNode(ISD::SDIV, dl, VT, - LHS, RHS); - Rem = DAG.getNode(ISD::SREM, dl, VT, - LHS, RHS); + Quot = DAG.getNode(ISD::SDIV, dl, VT, LHS, RHS); + Rem = DAG.getNode(ISD::SREM, dl, VT, LHS, RHS); } SDValue Zero = DAG.getConstant(0, dl, VT); SDValue RemNonZero = DAG.getSetCC(dl, BoolVT, Rem, Zero, ISD::SETNE); SDValue LHSNeg = DAG.getSetCC(dl, BoolVT, LHS, Zero, ISD::SETLT); SDValue RHSNeg = DAG.getSetCC(dl, BoolVT, RHS, Zero, ISD::SETLT); SDValue QuotNeg = DAG.getNode(ISD::XOR, dl, BoolVT, LHSNeg, RHSNeg); - SDValue Sub1 = DAG.getNode(ISD::SUB, dl, VT, Quot, - DAG.getConstant(1, dl, VT)); + SDValue Sub1 = + DAG.getNode(ISD::SUB, dl, VT, Quot, DAG.getConstant(1, dl, VT)); Quot = DAG.getSelect(dl, VT, DAG.getNode(ISD::AND, dl, BoolVT, RemNonZero, QuotNeg), Sub1, Quot); } else - Quot = DAG.getNode(ISD::UDIV, dl, VT, - LHS, RHS); + Quot = DAG.getNode(ISD::UDIV, dl, VT, LHS, RHS); return Quot; } -void TargetLowering::expandUADDSUBO( - SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const { +void TargetLowering::expandUADDSUBO(SDNode *Node, SDValue &Result, + SDValue &Overflow, + SelectionDAG &DAG) const { SDLoc dl(Node); SDValue LHS = Node->getOperand(0); SDValue RHS = Node->getOperand(1); @@ -8374,37 +8489,38 @@ unsigned OpcCarry = IsAdd ? ISD::ADDCARRY : ISD::SUBCARRY; if (isOperationLegalOrCustom(OpcCarry, Node->getValueType(0))) { SDValue CarryIn = DAG.getConstant(0, dl, Node->getValueType(1)); - SDValue NodeCarry = DAG.getNode(OpcCarry, dl, Node->getVTList(), - { LHS, RHS, CarryIn }); + SDValue NodeCarry = + DAG.getNode(OpcCarry, dl, Node->getVTList(), {LHS, RHS, CarryIn}); Result = SDValue(NodeCarry.getNode(), 0); Overflow = SDValue(NodeCarry.getNode(), 1); return; } - Result = DAG.getNode(IsAdd ? ISD::ADD : ISD::SUB, dl, - LHS.getValueType(), LHS, RHS); + Result = DAG.getNode(IsAdd ? ISD::ADD : ISD::SUB, dl, LHS.getValueType(), LHS, + RHS); EVT ResultType = Node->getValueType(1); - EVT SetCCType = getSetCCResultType( - DAG.getDataLayout(), *DAG.getContext(), Node->getValueType(0)); + EVT SetCCType = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), + Node->getValueType(0)); ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT; SDValue SetCC = DAG.getSetCC(dl, SetCCType, Result, LHS, CC); Overflow = DAG.getBoolExtOrTrunc(SetCC, dl, ResultType, ResultType); } -void TargetLowering::expandSADDSUBO( - SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const { +void TargetLowering::expandSADDSUBO(SDNode *Node, SDValue &Result, + SDValue &Overflow, + SelectionDAG &DAG) const { SDLoc dl(Node); SDValue LHS = Node->getOperand(0); SDValue RHS = Node->getOperand(1); bool IsAdd = Node->getOpcode() == ISD::SADDO; - Result = DAG.getNode(IsAdd ? ISD::ADD : ISD::SUB, dl, - LHS.getValueType(), LHS, RHS); + Result = DAG.getNode(IsAdd ? ISD::ADD : ISD::SUB, dl, LHS.getValueType(), LHS, + RHS); EVT ResultType = Node->getValueType(1); - EVT OType = getSetCCResultType( - DAG.getDataLayout(), *DAG.getContext(), Node->getValueType(0)); + EVT OType = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), + Node->getValueType(0)); // If SADDSAT/SSUBSAT is legal, compare results to detect overflow. unsigned OpcSat = IsAdd ? ISD::SADDSAT : ISD::SSUBSAT; @@ -8452,37 +8568,39 @@ SDValue ShiftAmt = DAG.getConstant(C.logBase2(), dl, ShiftAmtTy); Result = DAG.getNode(ISD::SHL, dl, VT, LHS, ShiftAmt); Overflow = DAG.getSetCC(dl, SetCCVT, - DAG.getNode(UseArithShift ? ISD::SRA : ISD::SRL, - dl, VT, Result, ShiftAmt), - LHS, ISD::SETNE); + DAG.getNode(UseArithShift ? ISD::SRA : ISD::SRL, + dl, VT, Result, ShiftAmt), + LHS, ISD::SETNE); return true; } } - EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getScalarSizeInBits() * 2); + EVT WideVT = + EVT::getIntegerVT(*DAG.getContext(), VT.getScalarSizeInBits() * 2); if (VT.isVector()) - WideVT = EVT::getVectorVT(*DAG.getContext(), WideVT, - VT.getVectorNumElements()); + WideVT = + EVT::getVectorVT(*DAG.getContext(), WideVT, VT.getVectorNumElements()); SDValue BottomHalf; SDValue TopHalf; - static const unsigned Ops[2][3] = - { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND }, - { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }}; + static const unsigned Ops[2][3] = { + {ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND}, + {ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND}}; if (isOperationLegalOrCustom(Ops[isSigned][0], VT)) { BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS); TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS); } else if (isOperationLegalOrCustom(Ops[isSigned][1], VT)) { - BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS, - RHS); + BottomHalf = + DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS, RHS); TopHalf = BottomHalf.getValue(1); } else if (isTypeLegal(WideVT)) { LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS); RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS); SDValue Mul = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS); BottomHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, Mul); - SDValue ShiftAmt = DAG.getConstant(VT.getScalarSizeInBits(), dl, - getShiftAmountTy(WideVT, DAG.getDataLayout())); + SDValue ShiftAmt = + DAG.getConstant(VT.getScalarSizeInBits(), dl, + getShiftAmountTy(WideVT, DAG.getDataLayout())); TopHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, DAG.getNode(ISD::SRL, dl, WideVT, Mul, ShiftAmt)); } else { @@ -8510,17 +8628,15 @@ // The high part is obtained by SRA'ing all but one of the bits of low // part. unsigned LoSize = VT.getFixedSizeInBits(); - HiLHS = - DAG.getNode(ISD::SRA, dl, VT, LHS, - DAG.getConstant(LoSize - 1, dl, - getPointerTy(DAG.getDataLayout()))); - HiRHS = - DAG.getNode(ISD::SRA, dl, VT, RHS, - DAG.getConstant(LoSize - 1, dl, - getPointerTy(DAG.getDataLayout()))); + HiLHS = DAG.getNode( + ISD::SRA, dl, VT, LHS, + DAG.getConstant(LoSize - 1, dl, getPointerTy(DAG.getDataLayout()))); + HiRHS = DAG.getNode( + ISD::SRA, dl, VT, RHS, + DAG.getConstant(LoSize - 1, dl, getPointerTy(DAG.getDataLayout()))); } else { - HiLHS = DAG.getConstant(0, dl, VT); - HiRHS = DAG.getConstant(0, dl, VT); + HiLHS = DAG.getConstant(0, dl, VT); + HiRHS = DAG.getConstant(0, dl, VT); } // Here we're passing the 2 arguments explicitly as 4 arguments that are @@ -8536,10 +8652,10 @@ // depending on platform endianness. This is usually handled by // the C calling convention, but we can't defer to it in // the legalizer. - SDValue Args[] = { LHS, HiLHS, RHS, HiRHS }; + SDValue Args[] = {LHS, HiLHS, RHS, HiRHS}; Ret = makeLibCall(DAG, LC, WideVT, Args, CallOptions, dl).first; } else { - SDValue Args[] = { HiLHS, LHS, HiRHS, RHS }; + SDValue Args[] = {HiLHS, LHS, HiRHS, RHS}; Ret = makeLibCall(DAG, LC, WideVT, Args, CallOptions, dl).first; } assert(Ret.getOpcode() == ISD::MERGE_VALUES && @@ -8562,8 +8678,8 @@ SDValue Sign = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt); Overflow = DAG.getSetCC(dl, SetCCVT, TopHalf, Sign, ISD::SETNE); } else { - Overflow = DAG.getSetCC(dl, SetCCVT, TopHalf, - DAG.getConstant(0, dl, VT), ISD::SETNE); + Overflow = DAG.getSetCC(dl, SetCCVT, TopHalf, DAG.getConstant(0, dl, VT), + ISD::SETNE); } // Truncate the result if SetCC returns a larger type than needed. @@ -8616,7 +8732,8 @@ return Res; } -SDValue TargetLowering::expandVecReduceSeq(SDNode *Node, SelectionDAG &DAG) const { +SDValue TargetLowering::expandVecReduceSeq(SDNode *Node, + SelectionDAG &DAG) const { SDLoc dl(Node); SDValue AccOp = Node->getOperand(0); SDValue VecOp = Node->getOperand(1); diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp --- a/llvm/lib/Support/APFloat.cpp +++ b/llvm/lib/Support/APFloat.cpp @@ -43,188 +43,179 @@ /// /// TODO: If clang source code is ever allowed to use constexpr in its own /// codebase, change this into a static inline function. -#define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs)) +#define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs)*4 + (_rhs)) /* Assumed in hexadecimal significand parsing, and conversion to hexadecimal strings. */ -static_assert(APFloatBase::integerPartWidth % 4 == 0, "Part width must be divisible by 4!"); +static_assert(APFloatBase::integerPartWidth % 4 == 0, + "Part width must be divisible by 4!"); namespace llvm { - /* Represents floating point arithmetic semantics. */ - struct fltSemantics { - /* The largest E such that 2^E is representable; this matches the - definition of IEEE 754. */ - APFloatBase::ExponentType maxExponent; - - /* The smallest E such that 2^E is a normalized number; this - matches the definition of IEEE 754. */ - APFloatBase::ExponentType minExponent; - - /* Number of bits in the significand. This includes the integer - bit. */ - unsigned int precision; - - /* Number of bits actually used in the semantics. */ - unsigned int sizeInBits; - - // Returns true if any number described by this semantics can be precisely - // represented by the specified semantics. - bool isRepresentableBy(const fltSemantics &S) const { - return maxExponent <= S.maxExponent && minExponent >= S.minExponent && - precision <= S.precision; - } - }; +/* Represents floating point arithmetic semantics. */ +struct fltSemantics { + /* The largest E such that 2^E is representable; this matches the + definition of IEEE 754. */ + APFloatBase::ExponentType maxExponent; - static const fltSemantics semIEEEhalf = {15, -14, 11, 16}; - static const fltSemantics semBFloat = {127, -126, 8, 16}; - static const fltSemantics semIEEEsingle = {127, -126, 24, 32}; - static const fltSemantics semIEEEdouble = {1023, -1022, 53, 64}; - static const fltSemantics semIEEEquad = {16383, -16382, 113, 128}; - static const fltSemantics semX87DoubleExtended = {16383, -16382, 64, 80}; - static const fltSemantics semBogus = {0, 0, 0, 0}; - - /* The IBM double-double semantics. Such a number consists of a pair of IEEE - 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal, - (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo. - Therefore it has two 53-bit mantissa parts that aren't necessarily adjacent - to each other, and two 11-bit exponents. - - Note: we need to make the value different from semBogus as otherwise - an unsafe optimization may collapse both values to a single address, - and we heavily rely on them having distinct addresses. */ - static const fltSemantics semPPCDoubleDouble = {-1, 0, 0, 0}; - - /* These are legacy semantics for the fallback, inaccrurate implementation of - IBM double-double, if the accurate semPPCDoubleDouble doesn't handle the - operation. It's equivalent to having an IEEE number with consecutive 106 - bits of mantissa and 11 bits of exponent. - - It's not equivalent to IBM double-double. For example, a legit IBM - double-double, 1 + epsilon: - - 1 + epsilon = 1 + (1 >> 1076) - - is not representable by a consecutive 106 bits of mantissa. - - Currently, these semantics are used in the following way: - - semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) -> - (64-bit APInt, 64-bit APInt) -> (128-bit APInt) -> - semPPCDoubleDoubleLegacy -> IEEE operations - - We use bitcastToAPInt() to get the bit representation (in APInt) of the - underlying IEEEdouble, then use the APInt constructor to construct the - legacy IEEE float. - - TODO: Implement all operations in semPPCDoubleDouble, and delete these - semantics. */ - static const fltSemantics semPPCDoubleDoubleLegacy = {1023, -1022 + 53, - 53 + 53, 128}; - - const llvm::fltSemantics &APFloatBase::EnumToSemantics(Semantics S) { - switch (S) { - case S_IEEEhalf: - return IEEEhalf(); - case S_BFloat: - return BFloat(); - case S_IEEEsingle: - return IEEEsingle(); - case S_IEEEdouble: - return IEEEdouble(); - case S_x87DoubleExtended: - return x87DoubleExtended(); - case S_IEEEquad: - return IEEEquad(); - case S_PPCDoubleDouble: - return PPCDoubleDouble(); - } - llvm_unreachable("Unrecognised floating semantics"); - } - - APFloatBase::Semantics - APFloatBase::SemanticsToEnum(const llvm::fltSemantics &Sem) { - if (&Sem == &llvm::APFloat::IEEEhalf()) - return S_IEEEhalf; - else if (&Sem == &llvm::APFloat::BFloat()) - return S_BFloat; - else if (&Sem == &llvm::APFloat::IEEEsingle()) - return S_IEEEsingle; - else if (&Sem == &llvm::APFloat::IEEEdouble()) - return S_IEEEdouble; - else if (&Sem == &llvm::APFloat::x87DoubleExtended()) - return S_x87DoubleExtended; - else if (&Sem == &llvm::APFloat::IEEEquad()) - return S_IEEEquad; - else if (&Sem == &llvm::APFloat::PPCDoubleDouble()) - return S_PPCDoubleDouble; - else - llvm_unreachable("Unknown floating semantics"); - } + /* The smallest E such that 2^E is a normalized number; this + matches the definition of IEEE 754. */ + APFloatBase::ExponentType minExponent; - const fltSemantics &APFloatBase::IEEEhalf() { - return semIEEEhalf; - } - const fltSemantics &APFloatBase::BFloat() { - return semBFloat; - } - const fltSemantics &APFloatBase::IEEEsingle() { - return semIEEEsingle; - } - const fltSemantics &APFloatBase::IEEEdouble() { - return semIEEEdouble; - } - const fltSemantics &APFloatBase::IEEEquad() { - return semIEEEquad; - } - const fltSemantics &APFloatBase::x87DoubleExtended() { - return semX87DoubleExtended; - } - const fltSemantics &APFloatBase::Bogus() { - return semBogus; - } - const fltSemantics &APFloatBase::PPCDoubleDouble() { - return semPPCDoubleDouble; + /* Number of bits in the significand. This includes the integer + bit. */ + unsigned int precision; + + /* Number of bits actually used in the semantics. */ + unsigned int sizeInBits; + + // Returns true if any number described by this semantics can be precisely + // represented by the specified semantics. + bool isRepresentableBy(const fltSemantics &S) const { + return maxExponent <= S.maxExponent && minExponent >= S.minExponent && + precision <= S.precision; } +}; - constexpr RoundingMode APFloatBase::rmNearestTiesToEven; - constexpr RoundingMode APFloatBase::rmTowardPositive; - constexpr RoundingMode APFloatBase::rmTowardNegative; - constexpr RoundingMode APFloatBase::rmTowardZero; - constexpr RoundingMode APFloatBase::rmNearestTiesToAway; +static const fltSemantics semIEEEhalf = {15, -14, 11, 16}; +static const fltSemantics semBFloat = {127, -126, 8, 16}; +static const fltSemantics semIEEEsingle = {127, -126, 24, 32}; +static const fltSemantics semIEEEdouble = {1023, -1022, 53, 64}; +static const fltSemantics semIEEEquad = {16383, -16382, 113, 128}; +static const fltSemantics semX87DoubleExtended = {16383, -16382, 64, 80}; +static const fltSemantics semBogus = {0, 0, 0, 0}; + +/* The IBM double-double semantics. Such a number consists of a pair of IEEE + 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal, + (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo. + Therefore it has two 53-bit mantissa parts that aren't necessarily adjacent + to each other, and two 11-bit exponents. + + Note: we need to make the value different from semBogus as otherwise + an unsafe optimization may collapse both values to a single address, + and we heavily rely on them having distinct addresses. */ +static const fltSemantics semPPCDoubleDouble = {-1, 0, 0, 0}; + +/* These are legacy semantics for the fallback, inaccrurate implementation of + IBM double-double, if the accurate semPPCDoubleDouble doesn't handle the + operation. It's equivalent to having an IEEE number with consecutive 106 + bits of mantissa and 11 bits of exponent. + + It's not equivalent to IBM double-double. For example, a legit IBM + double-double, 1 + epsilon: + + 1 + epsilon = 1 + (1 >> 1076) + + is not representable by a consecutive 106 bits of mantissa. + + Currently, these semantics are used in the following way: + + semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) -> + (64-bit APInt, 64-bit APInt) -> (128-bit APInt) -> + semPPCDoubleDoubleLegacy -> IEEE operations + + We use bitcastToAPInt() to get the bit representation (in APInt) of the + underlying IEEEdouble, then use the APInt constructor to construct the + legacy IEEE float. + + TODO: Implement all operations in semPPCDoubleDouble, and delete these + semantics. */ +static const fltSemantics semPPCDoubleDoubleLegacy = {1023, -1022 + 53, 53 + 53, + 128}; + +const llvm::fltSemantics &APFloatBase::EnumToSemantics(Semantics S) { + switch (S) { + case S_IEEEhalf: + return IEEEhalf(); + case S_BFloat: + return BFloat(); + case S_IEEEsingle: + return IEEEsingle(); + case S_IEEEdouble: + return IEEEdouble(); + case S_x87DoubleExtended: + return x87DoubleExtended(); + case S_IEEEquad: + return IEEEquad(); + case S_PPCDoubleDouble: + return PPCDoubleDouble(); + } + llvm_unreachable("Unrecognised floating semantics"); +} + +APFloatBase::Semantics +APFloatBase::SemanticsToEnum(const llvm::fltSemantics &Sem) { + if (&Sem == &llvm::APFloat::IEEEhalf()) + return S_IEEEhalf; + else if (&Sem == &llvm::APFloat::BFloat()) + return S_BFloat; + else if (&Sem == &llvm::APFloat::IEEEsingle()) + return S_IEEEsingle; + else if (&Sem == &llvm::APFloat::IEEEdouble()) + return S_IEEEdouble; + else if (&Sem == &llvm::APFloat::x87DoubleExtended()) + return S_x87DoubleExtended; + else if (&Sem == &llvm::APFloat::IEEEquad()) + return S_IEEEquad; + else if (&Sem == &llvm::APFloat::PPCDoubleDouble()) + return S_PPCDoubleDouble; + else + llvm_unreachable("Unknown floating semantics"); +} - /* A tight upper bound on number of parts required to hold the value - pow(5, power) is +const fltSemantics &APFloatBase::IEEEhalf() { return semIEEEhalf; } +const fltSemantics &APFloatBase::BFloat() { return semBFloat; } +const fltSemantics &APFloatBase::IEEEsingle() { return semIEEEsingle; } +const fltSemantics &APFloatBase::IEEEdouble() { return semIEEEdouble; } +const fltSemantics &APFloatBase::IEEEquad() { return semIEEEquad; } +const fltSemantics &APFloatBase::x87DoubleExtended() { + return semX87DoubleExtended; +} +const fltSemantics &APFloatBase::Bogus() { return semBogus; } +const fltSemantics &APFloatBase::PPCDoubleDouble() { + return semPPCDoubleDouble; +} - power * 815 / (351 * integerPartWidth) + 1 +constexpr RoundingMode APFloatBase::rmNearestTiesToEven; +constexpr RoundingMode APFloatBase::rmTowardPositive; +constexpr RoundingMode APFloatBase::rmTowardNegative; +constexpr RoundingMode APFloatBase::rmTowardZero; +constexpr RoundingMode APFloatBase::rmNearestTiesToAway; - However, whilst the result may require only this many parts, - because we are multiplying two values to get it, the - multiplication may require an extra part with the excess part - being zero (consider the trivial case of 1 * 1, tcFullMultiply - requires two parts to hold the single-part result). So we add an - extra one to guarantee enough space whilst multiplying. */ - const unsigned int maxExponent = 16383; - const unsigned int maxPrecision = 113; - const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1; - const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815) / (351 * APFloatBase::integerPartWidth)); +/* A tight upper bound on number of parts required to hold the value + pow(5, power) is - unsigned int APFloatBase::semanticsPrecision(const fltSemantics &semantics) { - return semantics.precision; - } - APFloatBase::ExponentType - APFloatBase::semanticsMaxExponent(const fltSemantics &semantics) { - return semantics.maxExponent; - } - APFloatBase::ExponentType - APFloatBase::semanticsMinExponent(const fltSemantics &semantics) { - return semantics.minExponent; - } - unsigned int APFloatBase::semanticsSizeInBits(const fltSemantics &semantics) { - return semantics.sizeInBits; - } + power * 815 / (351 * integerPartWidth) + 1 + + However, whilst the result may require only this many parts, + because we are multiplying two values to get it, the + multiplication may require an extra part with the excess part + being zero (consider the trivial case of 1 * 1, tcFullMultiply + requires two parts to hold the single-part result). So we add an + extra one to guarantee enough space whilst multiplying. */ +const unsigned int maxExponent = 16383; +const unsigned int maxPrecision = 113; +const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1; +const unsigned int maxPowerOfFiveParts = + 2 + + ((maxPowerOfFiveExponent * 815) / (351 * APFloatBase::integerPartWidth)); + +unsigned int APFloatBase::semanticsPrecision(const fltSemantics &semantics) { + return semantics.precision; +} +APFloatBase::ExponentType +APFloatBase::semanticsMaxExponent(const fltSemantics &semantics) { + return semantics.maxExponent; +} +APFloatBase::ExponentType +APFloatBase::semanticsMinExponent(const fltSemantics &semantics) { + return semantics.minExponent; +} +unsigned int APFloatBase::semanticsSizeInBits(const fltSemantics &semantics) { + return semantics.sizeInBits; +} - unsigned APFloatBase::getSizeInBits(const fltSemantics &Sem) { - return Sem.sizeInBits; +unsigned APFloatBase::getSizeInBits(const fltSemantics &Sem) { + return Sem.sizeInBits; } /* A bunch of private, handy routines. */ @@ -233,18 +224,13 @@ return make_error(Err, inconvertibleErrorCode()); } -static inline unsigned int -partCountForBits(unsigned int bits) -{ - return ((bits) + APFloatBase::integerPartWidth - 1) / APFloatBase::integerPartWidth; +static inline unsigned int partCountForBits(unsigned int bits) { + return ((bits) + APFloatBase::integerPartWidth - 1) / + APFloatBase::integerPartWidth; } /* Returns 0U-9U. Return values >= 10U are not digits. */ -static inline unsigned int -decDigitValue(unsigned int c) -{ - return c - '0'; -} +static inline unsigned int decDigitValue(unsigned int c) { return c - '0'; } /* Return the value of a decimal exponent of the form [+-]ddddddd. @@ -255,7 +241,7 @@ StringRef::iterator end) { bool isNegative; unsigned int absExponent; - const unsigned int overlargeExponent = 24000; /* FIXME. */ + const unsigned int overlargeExponent = 24000; /* FIXME. */ StringRef::iterator p = begin; // Treat no exponent as 0 to match binutils @@ -289,9 +275,9 @@ } if (isNegative) - return -(int) absExponent; + return -(int)absExponent; else - return (int) absExponent; + return (int)absExponent; } /* This is ugly and needs cleaning up, but I don't immediately see @@ -342,7 +328,7 @@ } if (overflow) - exponent = negative ? -32768: 32767; + exponent = negative ? -32768 : 32767; return exponent; } @@ -445,9 +431,10 @@ /* Adjust the exponents for any decimal point. */ D->exponent += static_cast((dot - p) - (dot > p)); - D->normalizedExponent = (D->exponent + - static_cast((p - D->firstSigDigit) - - (dot > D->firstSigDigit && dot < p))); + D->normalizedExponent = + (D->exponent + + static_cast( + (p - D->firstSigDigit) - (dot > D->firstSigDigit && dot < p))); } D->lastSigDigit = p; @@ -481,18 +468,16 @@ /* If we ran off the end it is exactly zero or one-half, otherwise a little more. */ if (hexDigit == -1U) - return digitValue == 0 ? lfExactlyZero: lfExactlyHalf; + return digitValue == 0 ? lfExactlyZero : lfExactlyHalf; else - return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf; + return digitValue == 0 ? lfLessThanHalf : lfMoreThanHalf; } /* Return the fraction lost were a bignum truncated losing the least significant BITS bits. */ static lostFraction lostFractionThroughTruncation(const APFloatBase::integerPart *parts, - unsigned int partCount, - unsigned int bits) -{ + unsigned int partCount, unsigned int bits) { unsigned int lsb; lsb = APInt::tcLSB(parts, partCount); @@ -510,9 +495,8 @@ } /* Shift DST right BITS bits noting lost fraction. */ -static lostFraction -shiftRight(APFloatBase::integerPart *dst, unsigned int parts, unsigned int bits) -{ +static lostFraction shiftRight(APFloatBase::integerPart *dst, + unsigned int parts, unsigned int bits) { lostFraction lost_fraction; lost_fraction = lostFractionThroughTruncation(dst, parts, bits); @@ -523,10 +507,8 @@ } /* Combine the effect of two lost fractions. */ -static lostFraction -combineLostFractions(lostFraction moreSignificant, - lostFraction lessSignificant) -{ +static lostFraction combineLostFractions(lostFraction moreSignificant, + lostFraction lessSignificant) { if (lessSignificant != lfExactlyZero) { if (moreSignificant == lfExactlyZero) moreSignificant = lfLessThanHalf; @@ -544,13 +526,12 @@ See "How to Read Floating Point Numbers Accurately" by William D Clinger. */ -static unsigned int -HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2) -{ +static unsigned int HUerrBound(bool inexactMultiply, unsigned int HUerr1, + unsigned int HUerr2) { assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8)); if (HUerr1 + HUerr2 == 0) - return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */ + return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */ else return inexactMultiply + 2 * (HUerr1 + HUerr2); } @@ -570,10 +551,11 @@ count = bits / APFloatBase::integerPartWidth; partBits = bits % APFloatBase::integerPartWidth + 1; - part = parts[count] & (~(APFloatBase::integerPart) 0 >> (APFloatBase::integerPartWidth - partBits)); + part = parts[count] & (~(APFloatBase::integerPart)0 >> + (APFloatBase::integerPartWidth - partBits)); if (isNearest) - boundary = (APFloatBase::integerPart) 1 << (partBits - 1); + boundary = (APFloatBase::integerPart)1 << (partBits - 1); else boundary = 0; @@ -587,29 +569,30 @@ if (part == boundary) { while (--count) if (parts[count]) - return ~(APFloatBase::integerPart) 0; /* A lot. */ + return ~(APFloatBase::integerPart)0; /* A lot. */ return parts[0]; } else if (part == boundary - 1) { while (--count) if (~parts[count]) - return ~(APFloatBase::integerPart) 0; /* A lot. */ + return ~(APFloatBase::integerPart)0; /* A lot. */ return -parts[0]; } - return ~(APFloatBase::integerPart) 0; /* A lot. */ + return ~(APFloatBase::integerPart)0; /* A lot. */ } /* Place pow(5, power) in DST, and return the number of parts used. DST must be at least one part larger than size of the answer. */ -static unsigned int -powerOf5(APFloatBase::integerPart *dst, unsigned int power) { - static const APFloatBase::integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125, 15625, 78125 }; +static unsigned int powerOf5(APFloatBase::integerPart *dst, + unsigned int power) { + static const APFloatBase::integerPart firstEightPowers[] = { + 1, 5, 25, 125, 625, 3125, 15625, 78125}; APFloatBase::integerPart pow5s[maxPowerOfFiveParts * 2 + 5]; pow5s[0] = 78125 * 5; - unsigned int partsCount[16] = { 1 }; + unsigned int partsCount[16] = {1}; APFloatBase::integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5; unsigned int result; assert(power <= maxExponent); @@ -674,10 +657,8 @@ /* Write out an integerPart in hexadecimal, starting with the most significant nibble. Write out exactly COUNT hexdigits, return COUNT. */ -static unsigned int -partAsHex (char *dst, APFloatBase::integerPart part, unsigned int count, - const char *hexDigitChars) -{ +static unsigned int partAsHex(char *dst, APFloatBase::integerPart part, + unsigned int count, const char *hexDigitChars) { unsigned int result = count; assert(count != 0 && count <= APFloatBase::integerPartWidth / 4); @@ -692,9 +673,7 @@ } /* Write out an unsigned decimal integer. */ -static char * -writeUnsignedDecimal (char *dst, unsigned int n) -{ +static char *writeUnsignedDecimal(char *dst, unsigned int n) { char buff[40], *p; p = buff; @@ -710,12 +689,10 @@ } /* Write out a signed decimal integer. */ -static char * -writeSignedDecimal (char *dst, int value) -{ +static char *writeSignedDecimal(char *dst, int value) { if (value < 0) { *dst++ = '-'; - dst = writeUnsignedDecimal(dst, -(unsigned) value); + dst = writeUnsignedDecimal(dst, -(unsigned)value); } else dst = writeUnsignedDecimal(dst, value); @@ -735,7 +712,7 @@ void IEEEFloat::freeSignificand() { if (needsCleanup()) - delete [] significand.parts; + delete[] significand.parts; } void IEEEFloat::assign(const IEEEFloat &rhs) { @@ -752,8 +729,7 @@ assert(isFiniteNonZero() || category == fcNaN); assert(rhs.partCount() >= partCount()); - APInt::tcAssign(significandParts(), rhs.significandParts(), - partCount()); + APInt::tcAssign(significandParts(), rhs.significandParts(), partCount()); } /* Make this number a NaN, with an arbitrary but deterministic value @@ -833,8 +809,8 @@ bool IEEEFloat::isDenormal() const { return isFiniteNonZero() && (exponent == semantics->minExponent) && - (APInt::tcExtractBit(significandParts(), - semantics->precision - 1) == 0); + (APInt::tcExtractBit(significandParts(), semantics->precision - 1) == + 0); } bool IEEEFloat::isSmallest() const { @@ -842,7 +818,7 @@ // denormal, i.e. the floating point number with exponent being minimum // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0). return isFiniteNonZero() && exponent == semantics->minExponent && - significandMSB() == 0; + significandMSB() == 0; } bool IEEEFloat::isSignificandAllOnes() const { @@ -856,11 +832,11 @@ // Set the unused high bits to all ones when we compare. const unsigned NumHighBits = - PartCount*integerPartWidth - semantics->precision + 1; + PartCount * integerPartWidth - semantics->precision + 1; assert(NumHighBits <= integerPartWidth && NumHighBits > 0 && "Can not have more high bits to fill than integerPartWidth"); - const integerPart HighBitFill = - ~integerPart(0) << (integerPartWidth - NumHighBits); + const integerPart HighBitFill = ~integerPart(0) + << (integerPartWidth - NumHighBits); if (~(Parts[PartCount - 1] | HighBitFill)) return false; @@ -879,9 +855,9 @@ // Compute how many bits are used in the final word. const unsigned NumHighBits = - PartCount*integerPartWidth - semantics->precision + 1; + PartCount * integerPartWidth - semantics->precision + 1; assert(NumHighBits < integerPartWidth && "Can not have more high bits to " - "clear than integerPartWidth"); + "clear than integerPartWidth"); const integerPart HighBitMask = ~integerPart(0) >> NumHighBits; if (Parts[PartCount - 1] & HighBitMask) @@ -893,13 +869,14 @@ bool IEEEFloat::isLargest() const { // The largest number by magnitude in our format will be the floating point // number with maximum exponent and with significand that is all ones. - return isFiniteNonZero() && exponent == semantics->maxExponent - && isSignificandAllOnes(); + return isFiniteNonZero() && exponent == semantics->maxExponent && + isSignificandAllOnes(); } bool IEEEFloat::isInteger() const { // This could be made more efficient; I'm going for obviously correct. - if (!isFinite()) return false; + if (!isFinite()) + return false; IEEEFloat truncated = *this; truncated.roundToIntegral(rmTowardZero); return compare(truncated) == cmpEqual; @@ -908,11 +885,10 @@ bool IEEEFloat::bitwiseIsEqual(const IEEEFloat &rhs) const { if (this == &rhs) return true; - if (semantics != rhs.semantics || - category != rhs.category || + if (semantics != rhs.semantics || category != rhs.category || sign != rhs.sign) return false; - if (category==fcZero || category==fcInfinity) + if (category == fcZero || category == fcInfinity) return true; if (isFiniteNonZero() && exponent != rhs.exponent) @@ -1006,8 +982,7 @@ assert(semantics == rhs.semantics); assert(exponent == rhs.exponent); - return APInt::tcSubtract(parts, rhs.significandParts(), borrow, - partCount()); + return APInt::tcSubtract(parts, rhs.significandParts(), borrow, partCount()); } /* Multiply the significand of the RHS. If ADDEND is non-NULL, add it @@ -1015,7 +990,7 @@ lost fraction. */ lostFraction IEEEFloat::multiplySignificand(const IEEEFloat &rhs, IEEEFloat addend) { - unsigned int omsb; // One, not zero, based MSB. + unsigned int omsb; // One, not zero, based MSB. unsigned int partsCount, newPartsCount, precision; integerPart *lhsSignificand; integerPart scratch[4]; @@ -1039,8 +1014,8 @@ lhsSignificand = significandParts(); partsCount = partCount(); - APInt::tcFullMultiply(fullSignificand, lhsSignificand, - rhs.significandParts(), partsCount, partsCount); + APInt::tcFullMultiply(fullSignificand, lhsSignificand, rhs.significandParts(), + partsCount, partsCount); lost_fraction = lfExactlyZero; omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1; @@ -1097,7 +1072,8 @@ // Shift the significand of the addend right by one bit. This guarantees // that the high bit of the significand is zero (same as fullSignificand), - // so the addition will overflow (if it does overflow at all) into the top bit. + // so the addition will overflow (if it does overflow at all) into the top + // bit. lost_fraction = extendedAddend.shiftSignificandRight(1); assert(lost_fraction == lfExactlyZero && "Lost precision while shifting addend for fused-multiply-add."); @@ -1140,7 +1116,7 @@ APInt::tcAssign(lhsSignificand, fullSignificand, partsCount); if (newPartsCount > 4) - delete [] fullSignificand; + delete[] fullSignificand; return lost_fraction; } @@ -1227,7 +1203,7 @@ lost_fraction = lfLessThanHalf; if (partsCount > 2) - delete [] dividend; + delete[] dividend; return lost_fraction; } @@ -1243,7 +1219,7 @@ /* Note that a zero result is NOT normalized to fcZero. */ lostFraction IEEEFloat::shiftSignificandRight(unsigned int bits) { /* Our exponent should not overflow. */ - assert((ExponentType) (exponent + bits) >= exponent); + assert((ExponentType)(exponent + bits) >= exponent); exponent += bits; @@ -1288,6 +1264,23 @@ return cmpEqual; } +/* Set the least significant BITS bits of a bignum, clear the + rest. */ +static void tcSetLeastSignificantBits(APInt::WordType *dst, unsigned parts, + unsigned bits) { + unsigned i = 0; + while (bits > APInt::APINT_BITS_PER_WORD) { + dst[i++] = ~(APInt::WordType)0; + bits -= APInt::APINT_BITS_PER_WORD; + } + + if (bits) + dst[i++] = ~(APInt::WordType)0 >> (APInt::APINT_BITS_PER_WORD - bits); + + while (i < parts) + dst[i++] = 0; +} + /* Handle overflow. Sign is preserved. We either become infinity or the largest finite number. */ IEEEFloat::opStatus IEEEFloat::handleOverflow(roundingMode rounding_mode) { @@ -1297,14 +1290,14 @@ (rounding_mode == rmTowardPositive && !sign) || (rounding_mode == rmTowardNegative && sign)) { category = fcInfinity; - return (opStatus) (opOverflow | opInexact); + return (opStatus)(opOverflow | opInexact); } /* Otherwise we become the largest finite number. */ category = fcNormal; exponent = semantics->maxExponent; - APInt::tcSetLeastSignificantBits(significandParts(), partCount(), - semantics->precision); + tcSetLeastSignificantBits(significandParts(), partCount(), + semantics->precision); return opInexact; } @@ -1354,7 +1347,7 @@ IEEEFloat::opStatus IEEEFloat::normalize(roundingMode rounding_mode, lostFraction lost_fraction) { - unsigned int omsb; /* One, not zero, based MSB. */ + unsigned int omsb; /* One, not zero, based MSB. */ int exponentChange; if (!isFiniteNonZero()) @@ -1397,7 +1390,7 @@ lost_fraction = combineLostFractions(lf, lost_fraction); /* Keep OMSB up-to-date. */ - if (omsb > (unsigned) exponentChange) + if (omsb > (unsigned)exponentChange) omsb -= exponentChange; else omsb = 0; @@ -1426,14 +1419,14 @@ omsb = significandMSB() + 1; /* Did the significand increment overflow? */ - if (omsb == (unsigned) semantics->precision + 1) { + if (omsb == (unsigned)semantics->precision + 1) { /* Renormalize by incrementing the exponent and shifting our significand right one. However if we already have the maximum exponent we overflow to infinity. */ if (exponent == semantics->maxExponent) { category = fcInfinity; - return (opStatus) (opOverflow | opInexact); + return (opStatus)(opOverflow | opInexact); } shiftSignificandRight(1); @@ -1455,7 +1448,7 @@ category = fcZero; /* The fcZero case is a denormal that underflowed to zero. */ - return (opStatus) (opUnderflow | opInexact); + return (opStatus)(opUnderflow | opInexact); } IEEEFloat::opStatus IEEEFloat::addOrSubtractSpecials(const IEEEFloat &rhs, @@ -1502,7 +1495,7 @@ case PackCategoriesIntoKey(fcInfinity, fcInfinity): /* Differently signed infinities can only be validly subtracted. */ - if (((sign ^ rhs.sign)!=0) != subtract) { + if (((sign ^ rhs.sign) != 0) != subtract) { makeNaN(); return opInvalidOp; } @@ -1544,13 +1537,12 @@ // Should we reverse the subtraction. if (compareAbsoluteValue(temp_rhs) == cmpLessThan) { - carry = temp_rhs.subtractSignificand - (*this, lost_fraction != lfExactlyZero); + carry = + temp_rhs.subtractSignificand(*this, lost_fraction != lfExactlyZero); copySignificand(temp_rhs); sign = !sign; } else { - carry = subtractSignificand - (temp_rhs, lost_fraction != lfExactlyZero); + carry = subtractSignificand(temp_rhs, lost_fraction != lfExactlyZero); } /* Invert the lost fraction - it was on the RHS and @@ -1809,7 +1801,7 @@ lostFraction lost_fraction = multiplySignificand(rhs); fs = normalize(rounding_mode, lost_fraction); if (lost_fraction != lfExactlyZero) - fs = (opStatus) (fs | opInexact); + fs = (opStatus)(fs | opInexact); } return fs; @@ -1827,7 +1819,7 @@ lostFraction lost_fraction = divideSignificand(rhs); fs = normalize(rounding_mode, lost_fraction); if (lost_fraction != lfExactlyZero) - fs = (opStatus) (fs | opInexact); + fs = (opStatus)(fs | opInexact); } return fs; @@ -1932,7 +1924,7 @@ } if (isZero()) - sign = origSign; // IEEE754 requires this + sign = origSign; // IEEE754 requires this else sign ^= origSign; return fs; @@ -1952,7 +1944,7 @@ V.sign = sign; fs = subtract(V, rmNearestTiesToEven); - assert(fs==opOK); + assert(fs == opOK); } if (isZero()) sign = origSign; // fmod requires this @@ -1970,15 +1962,14 @@ /* If and only if all arguments are normal do we need to do an extended-precision calculation. */ - if (isFiniteNonZero() && - multiplicand.isFiniteNonZero() && + if (isFiniteNonZero() && multiplicand.isFiniteNonZero() && addend.isFinite()) { lostFraction lost_fraction; lost_fraction = multiplySignificand(multiplicand, addend); fs = normalize(rounding_mode, lost_fraction); if (lost_fraction != lfExactlyZero) - fs = (opStatus) (fs | opInexact); + fs = (opStatus)(fs | opInexact); /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a positive zero unless rounding to minus infinity, except that @@ -2054,7 +2045,7 @@ // If the exponent is large enough, we know that this value is already // integral, and the arithmetic below would potentially cause it to saturate // to +/-Inf. Bail out early instead. - if (exponent+1 >= (int)semanticsPrecision(*semantics)) + if (exponent + 1 >= (int)semanticsPrecision(*semantics)) return opOK; // The algorithm here is quite simple: we add 2^(p-1), where p is the @@ -2064,7 +2055,7 @@ // NOTE: When the input value is negative, we do subtraction followed by // addition instead. APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1); - IntegerConstant <<= semanticsPrecision(*semantics)-1; + IntegerConstant <<= semanticsPrecision(*semantics) - 1; IEEEFloat MagicConstant(*semantics); fs = MagicConstant.convertFromAPInt(IntegerConstant, false, rmNearestTiesToEven); @@ -2088,7 +2079,6 @@ return fs; } - /* Comparison requires normalized numbers. */ IEEEFloat::cmpResult IEEEFloat::compare(const IEEEFloat &rhs) const { cmpResult result; @@ -2209,7 +2199,7 @@ } // If this is a truncation, perform the shift before we narrow the storage. - if (shift < 0 && (isFiniteNonZero() || category==fcNaN)) + if (shift < 0 && (isFiniteNonZero() || category == fcNaN)) lostFraction = shiftRight(significandParts(), oldPartCount, -shift); // Fix the storage so it can hold to new value. @@ -2218,14 +2208,14 @@ integerPart *newParts; newParts = new integerPart[newPartCount]; APInt::tcSet(newParts, 0, newPartCount); - if (isFiniteNonZero() || category==fcNaN) + if (isFiniteNonZero() || category == fcNaN) APInt::tcAssign(newParts, significandParts(), oldPartCount); freeSignificand(); significand.parts = newParts; } else if (newPartCount == 1 && oldPartCount != 1) { // Switch to built-in storage for a single part. integerPart newPart = 0; - if (isFiniteNonZero() || category==fcNaN) + if (isFiniteNonZero() || category == fcNaN) newPart = significandParts()[0]; freeSignificand(); significand.part = newPart; @@ -2236,7 +2226,7 @@ // If this is an extension, perform the shift now that the storage is // available. - if (shift > 0 && (isFiniteNonZero() || category==fcNaN)) + if (shift > 0 && (isFiniteNonZero() || category == fcNaN)) APInt::tcShiftLeft(significandParts(), newPartCount, shift); if (isFiniteNonZero()) { @@ -2309,7 +2299,7 @@ APInt::tcSet(parts.data(), 0, dstPartsCount); /* For exponent -1 the integer bit represents .5, look at that. For smaller exponents leftmost truncated bit is 0. */ - truncatedBits = semantics->precision -1U - exponent; + truncatedBits = semantics->precision - 1U - exponent; } else { /* We want the most significant (exponent + 1) bits; the rest are truncated. */ @@ -2336,12 +2326,12 @@ /* Step 2: work out any lost fraction, and increment the absolute value if we would round away from zero. */ if (truncatedBits) { - lost_fraction = lostFractionThroughTruncation(src, partCount(), - truncatedBits); + lost_fraction = + lostFractionThroughTruncation(src, partCount(), truncatedBits); if (lost_fraction != lfExactlyZero && roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) { if (APInt::tcIncrement(parts.data(), dstPartsCount)) - return opInvalidOp; /* Overflow. */ + return opInvalidOp; /* Overflow. */ } } else { lost_fraction = lfExactlyZero; @@ -2368,7 +2358,7 @@ return opInvalidOp; } - APInt::tcNegate (parts.data(), dstPartsCount); + APInt::tcNegate(parts.data(), dstPartsCount); } else { if (omsb >= width + !isSigned) return opInvalidOp; @@ -2412,7 +2402,7 @@ else bits = width - isSigned; - APInt::tcSetLeastSignificantBits(parts.data(), dstPartsCount, bits); + tcSetLeastSignificantBits(parts.data(), dstPartsCount, bits); if (sign && isSigned) APInt::tcShiftLeft(parts.data(), dstPartsCount, width - 1); } @@ -2439,8 +2429,8 @@ be that many; extract what we can. */ if (precision <= omsb) { exponent = omsb - 1; - lost_fraction = lostFractionThroughTruncation(src, srcCount, - omsb - precision); + lost_fraction = + lostFractionThroughTruncation(src, srcCount, omsb - precision); APInt::tcExtract(dst, dstCount, src, precision, omsb - precision); } else { exponent = precision - 1; @@ -2474,8 +2464,7 @@ roundingMode rounding_mode) { opStatus status; - if (isSigned && - APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) { + if (isSigned && APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) { integerPart *copy; /* If we're signed and negative negate a copy. */ @@ -2484,7 +2473,7 @@ APInt::tcAssign(copy, src, srcCount); APInt::tcNegate(copy, srcCount); status = convertFromUnsignedParts(copy, srcCount, rounding_mode); - delete [] copy; + delete[] copy; } else { sign = false; status = convertFromUnsignedParts(src, srcCount, rounding_mode); @@ -2609,7 +2598,7 @@ unsigned sigPartCount, int exp, roundingMode rounding_mode) { unsigned int parts, pow5PartCount; - fltSemantics calcSemantics = { 32767, -32767, 0, 0 }; + fltSemantics calcSemantics = {32767, -32767, 0, 0}; integerPart pow5Parts[maxPowerOfFiveParts]; bool isNearest; @@ -2619,7 +2608,7 @@ parts = partCountForBits(semantics->precision + 11); /* Calculate pow(5, abs(exp)). */ - pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp); + pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp : -exp); for (;; parts *= 2) { opStatus sigStatus, powStatus; @@ -2658,13 +2647,14 @@ excessPrecision = calcSemantics.precision; } /* Extra half-ulp lost in reciprocal of exponent. */ - powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2; + powHUerr = + (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0 : 2; } /* Both multiplySignificand and divideSignificand return the result with the integer bit set. */ - assert(APInt::tcExtractBit - (decSig.significandParts(), calcSemantics.precision - 1) == 1); + assert(APInt::tcExtractBit(decSig.significandParts(), + calcSemantics.precision - 1) == 1); HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK, powHUerr); @@ -2673,17 +2663,16 @@ /* Are we guaranteed to round correctly if we truncate? */ if (HUdistance >= HUerr) { - APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(), - calcSemantics.precision - excessPrecision, - excessPrecision); + APInt::tcExtract( + significandParts(), partCount(), decSig.significandParts(), + calcSemantics.precision - excessPrecision, excessPrecision); /* Take the exponent of decSig. If we tcExtract-ed less bits above we must adjust our exponent to compensate for the implicit right shift. */ - exponent = (decSig.exponent + semantics->precision - - (calcSemantics.precision - excessPrecision)); - calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(), - decSig.partCount(), - truncatedBits); + exponent = (decSig.exponent + semantics->precision - + (calcSemantics.precision - excessPrecision)); + calcLostFraction = lostFractionThroughTruncation( + decSig.significandParts(), decSig.partCount(), truncatedBits); return normalize(rounding_mode, calcLostFraction); } } @@ -2727,26 +2716,26 @@ category = fcZero; fs = opOK; - /* Check whether the normalized exponent is high enough to overflow - max during the log-rebasing in the max-exponent check below. */ + /* Check whether the normalized exponent is high enough to overflow + max during the log-rebasing in the max-exponent check below. */ } else if (D.normalizedExponent - 1 > INT_MAX / 42039) { fs = handleOverflow(rounding_mode); - /* If it wasn't, then it also wasn't high enough to overflow max - during the log-rebasing in the min-exponent check. Check that it - won't overflow min in either check, then perform the min-exponent - check. */ + /* If it wasn't, then it also wasn't high enough to overflow max + during the log-rebasing in the min-exponent check. Check that it + won't overflow min in either check, then perform the min-exponent + check. */ } else if (D.normalizedExponent - 1 < INT_MIN / 42039 || (D.normalizedExponent + 1) * 28738 <= - 8651 * (semantics->minExponent - (int) semantics->precision)) { + 8651 * (semantics->minExponent - (int)semantics->precision)) { /* Underflow to zero and round. */ category = fcNormal; zeroSignificand(); fs = normalize(rounding_mode, lfLessThanHalf); - /* We can finally safely perform the max-exponent check. */ - } else if ((D.normalizedExponent - 1) * 42039 - >= 12655 * semantics->maxExponent) { + /* We can finally safely perform the max-exponent check. */ + } else if ((D.normalizedExponent - 1) * 42039 >= + 12655 * semantics->maxExponent) { /* Overflow and round. */ fs = handleOverflow(rounding_mode); } else { @@ -2788,7 +2777,7 @@ val = val * 10 + decValue; /* The maximum number that can be multiplied by ten with any digit added without overflowing an integerPart. */ - } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10); + } while (p <= D.lastSigDigit && multiplier <= (~(integerPart)0 - 9) / 10); /* Multiply out the current part. */ APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val, @@ -2801,10 +2790,10 @@ } while (p <= D.lastSigDigit); category = fcNormal; - fs = roundSignificandWithExponent(decSignificand, partCount, - D.exponent, rounding_mode); + fs = roundSignificandWithExponent(decSignificand, partCount, D.exponent, + rounding_mode); - delete [] decSignificand; + delete[] decSignificand; } return fs; @@ -2945,30 +2934,30 @@ switch (category) { case fcInfinity: - memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1); + memcpy(dst, upperCase ? infinityU : infinityL, sizeof infinityU - 1); dst += sizeof infinityL - 1; break; case fcNaN: - memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1); + memcpy(dst, upperCase ? NaNU : NaNL, sizeof NaNU - 1); dst += sizeof NaNU - 1; break; case fcZero: *dst++ = '0'; - *dst++ = upperCase ? 'X': 'x'; + *dst++ = upperCase ? 'X' : 'x'; *dst++ = '0'; if (hexDigits > 1) { *dst++ = '.'; - memset (dst, '0', hexDigits - 1); + memset(dst, '0', hexDigits - 1); dst += hexDigits - 1; } - *dst++ = upperCase ? 'P': 'p'; + *dst++ = upperCase ? 'P' : 'p'; *dst++ = '0'; break; case fcNormal: - dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode); + dst = convertNormalToHexString(dst, hexDigits, upperCase, rounding_mode); break; } @@ -2991,10 +2980,10 @@ bool roundUp; *dst++ = '0'; - *dst++ = upperCase ? 'X': 'x'; + *dst++ = upperCase ? 'X' : 'x'; roundUp = false; - hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower; + hexDigitChars = upperCase ? hexDigitsUpper : hexDigitsLower; significand = significandParts(); partsCount = partCount(); @@ -3006,7 +2995,7 @@ /* The natural number of digits required ignoring trailing insignificant zeroes. */ - outputDigits = (valueBits - significandLSB () + 3) / 4; + outputDigits = (valueBits - significandLSB() + 3) / 4; /* hexDigits of zero means use the required number for the precision. Otherwise, see if we are truncating. If we are, @@ -3019,7 +3008,7 @@ lostFraction fraction; bits = valueBits - hexDigits * 4; - fraction = lostFractionThroughTruncation (significand, partsCount, bits); + fraction = lostFractionThroughTruncation(significand, partsCount, bits); roundUp = roundAwayFromZero(rounding_mode, fraction, bits); } outputDigits = hexDigits; @@ -3037,7 +3026,7 @@ /* Put the most significant integerPartWidth bits in "part". */ if (--count == partsCount) - part = 0; /* An imaginary higher zero part. */ + part = 0; /* An imaginary higher zero part. */ else part = significand[count] << shift; @@ -3049,7 +3038,7 @@ if (curDigits > outputDigits) curDigits = outputDigits; - dst += partAsHex (dst, part, curDigits, hexDigitChars); + dst += partAsHex(dst, part, curDigits, hexDigitChars); outputDigits -= curDigits; } @@ -3059,12 +3048,12 @@ /* Note that hexDigitChars has a trailing '0'. */ do { q--; - *q = hexDigitChars[hexDigitValue (*q) + 1]; + *q = hexDigitChars[hexDigitValue(*q) + 1]; } while (*q == '0'); assert(q >= p); } else { /* Add trailing zeroes. */ - memset (dst, '0', outputDigits); + memset(dst, '0', outputDigits); dst += outputDigits; } @@ -3072,15 +3061,15 @@ is something after the decimal point add it. This must come after rounding above. */ p[-1] = p[0]; - if (dst -1 == p) + if (dst - 1 == p) dst--; else p[0] = '.'; /* Finally output the exponent. */ - *dst++ = upperCase ? 'P': 'p'; + *dst++ = upperCase ? 'P' : 'p'; - return writeSignedDecimal (dst, exponent); + return writeSignedDecimal(dst, exponent); } hash_code hash_value(const IEEEFloat &Arg) { @@ -3091,11 +3080,11 @@ Arg.semantics->precision); // Normal floats need their exponent and significand hashed. - return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign, - Arg.semantics->precision, Arg.exponent, - hash_combine_range( - Arg.significandParts(), - Arg.significandParts() + Arg.partCount())); + return hash_combine( + (uint8_t)Arg.category, (uint8_t)Arg.sign, Arg.semantics->precision, + Arg.exponent, + hash_combine_range(Arg.significandParts(), + Arg.significandParts() + Arg.partCount())); } // Conversion from APFloat to/from host float/double. It may eventually be @@ -3108,20 +3097,20 @@ // the actual IEEE respresentations. We compensate for that here. APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt() const { - assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended); - assert(partCount()==2); + assert(semantics == (const llvm::fltSemantics *)&semX87DoubleExtended); + assert(partCount() == 2); uint64_t myexponent, mysignificand; if (isFiniteNonZero()) { - myexponent = exponent+16383; //bias + myexponent = exponent + 16383; // bias mysignificand = significandParts()[0]; - if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL)) - myexponent = 0; // denormal - } else if (category==fcZero) { + if (myexponent == 1 && !(mysignificand & 0x8000000000000000ULL)) + myexponent = 0; // denormal + } else if (category == fcZero) { myexponent = 0; mysignificand = 0; - } else if (category==fcInfinity) { + } else if (category == fcInfinity) { myexponent = 0x7fff; mysignificand = 0x8000000000000000ULL; } else { @@ -3132,14 +3121,13 @@ uint64_t words[2]; words[0] = mysignificand; - words[1] = ((uint64_t)(sign & 1) << 15) | - (myexponent & 0x7fffLL); + words[1] = ((uint64_t)(sign & 1) << 15) | (myexponent & 0x7fffLL); return APInt(80, words); } APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt() const { assert(semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy); - assert(partCount()==2); + assert(partCount() == 2); uint64_t words[2]; opStatus fs; @@ -3187,21 +3175,21 @@ } APInt IEEEFloat::convertQuadrupleAPFloatToAPInt() const { - assert(semantics == (const llvm::fltSemantics*)&semIEEEquad); - assert(partCount()==2); + assert(semantics == (const llvm::fltSemantics *)&semIEEEquad); + assert(partCount() == 2); uint64_t myexponent, mysignificand, mysignificand2; if (isFiniteNonZero()) { - myexponent = exponent+16383; //bias + myexponent = exponent + 16383; // bias mysignificand = significandParts()[0]; mysignificand2 = significandParts()[1]; - if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL)) - myexponent = 0; // denormal - } else if (category==fcZero) { + if (myexponent == 1 && !(mysignificand2 & 0x1000000000000LL)) + myexponent = 0; // denormal + } else if (category == fcZero) { myexponent = 0; mysignificand = mysignificand2 = 0; - } else if (category==fcInfinity) { + } else if (category == fcInfinity) { myexponent = 0x7fff; mysignificand = mysignificand2 = 0; } else { @@ -3213,28 +3201,27 @@ uint64_t words[2]; words[0] = mysignificand; - words[1] = ((uint64_t)(sign & 1) << 63) | - ((myexponent & 0x7fff) << 48) | + words[1] = ((uint64_t)(sign & 1) << 63) | ((myexponent & 0x7fff) << 48) | (mysignificand2 & 0xffffffffffffLL); return APInt(128, words); } APInt IEEEFloat::convertDoubleAPFloatToAPInt() const { - assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble); - assert(partCount()==1); + assert(semantics == (const llvm::fltSemantics *)&semIEEEdouble); + assert(partCount() == 1); uint64_t myexponent, mysignificand; if (isFiniteNonZero()) { - myexponent = exponent+1023; //bias + myexponent = exponent + 1023; // bias mysignificand = *significandParts(); - if (myexponent==1 && !(mysignificand & 0x10000000000000LL)) - myexponent = 0; // denormal - } else if (category==fcZero) { + if (myexponent == 1 && !(mysignificand & 0x10000000000000LL)) + myexponent = 0; // denormal + } else if (category == fcZero) { myexponent = 0; mysignificand = 0; - } else if (category==fcInfinity) { + } else if (category == fcInfinity) { myexponent = 0x7ff; mysignificand = 0; } else { @@ -3243,26 +3230,26 @@ mysignificand = *significandParts(); } - return APInt(64, ((((uint64_t)(sign & 1) << 63) | - ((myexponent & 0x7ff) << 52) | - (mysignificand & 0xfffffffffffffLL)))); + return APInt(64, + ((((uint64_t)(sign & 1) << 63) | ((myexponent & 0x7ff) << 52) | + (mysignificand & 0xfffffffffffffLL)))); } APInt IEEEFloat::convertFloatAPFloatToAPInt() const { - assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle); - assert(partCount()==1); + assert(semantics == (const llvm::fltSemantics *)&semIEEEsingle); + assert(partCount() == 1); uint32_t myexponent, mysignificand; if (isFiniteNonZero()) { - myexponent = exponent+127; //bias + myexponent = exponent + 127; // bias mysignificand = (uint32_t)*significandParts(); if (myexponent == 1 && !(mysignificand & 0x800000)) - myexponent = 0; // denormal - } else if (category==fcZero) { + myexponent = 0; // denormal + } else if (category == fcZero) { myexponent = 0; mysignificand = 0; - } else if (category==fcInfinity) { + } else if (category == fcInfinity) { myexponent = 0xff; mysignificand = 0; } else { @@ -3271,7 +3258,7 @@ mysignificand = (uint32_t)*significandParts(); } - return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) | + return APInt(32, (((sign & 1) << 31) | ((myexponent & 0xff) << 23) | (mysignificand & 0x7fffff))); } @@ -3303,20 +3290,20 @@ } APInt IEEEFloat::convertHalfAPFloatToAPInt() const { - assert(semantics == (const llvm::fltSemantics*)&semIEEEhalf); - assert(partCount()==1); + assert(semantics == (const llvm::fltSemantics *)&semIEEEhalf); + assert(partCount() == 1); uint32_t myexponent, mysignificand; if (isFiniteNonZero()) { - myexponent = exponent+15; //bias + myexponent = exponent + 15; // bias mysignificand = (uint32_t)*significandParts(); if (myexponent == 1 && !(mysignificand & 0x400)) - myexponent = 0; // denormal - } else if (category==fcZero) { + myexponent = 0; // denormal + } else if (category == fcZero) { myexponent = 0; mysignificand = 0; - } else if (category==fcInfinity) { + } else if (category == fcInfinity) { myexponent = 0x1f; mysignificand = 0; } else { @@ -3325,7 +3312,7 @@ mysignificand = (uint32_t)*significandParts(); } - return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) | + return APInt(16, (((sign & 1) << 15) | ((myexponent & 0x1f) << 10) | (mysignificand & 0x3ff))); } @@ -3334,38 +3321,38 @@ // and treating the result as a normal integer is unlikely to be useful. APInt IEEEFloat::bitcastToAPInt() const { - if (semantics == (const llvm::fltSemantics*)&semIEEEhalf) + if (semantics == (const llvm::fltSemantics *)&semIEEEhalf) return convertHalfAPFloatToAPInt(); if (semantics == (const llvm::fltSemantics *)&semBFloat) return convertBFloatAPFloatToAPInt(); - if (semantics == (const llvm::fltSemantics*)&semIEEEsingle) + if (semantics == (const llvm::fltSemantics *)&semIEEEsingle) return convertFloatAPFloatToAPInt(); - if (semantics == (const llvm::fltSemantics*)&semIEEEdouble) + if (semantics == (const llvm::fltSemantics *)&semIEEEdouble) return convertDoubleAPFloatToAPInt(); - if (semantics == (const llvm::fltSemantics*)&semIEEEquad) + if (semantics == (const llvm::fltSemantics *)&semIEEEquad) return convertQuadrupleAPFloatToAPInt(); if (semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy) return convertPPCDoubleDoubleAPFloatToAPInt(); - assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended && + assert(semantics == (const llvm::fltSemantics *)&semX87DoubleExtended && "unknown format!"); return convertF80LongDoubleAPFloatToAPInt(); } float IEEEFloat::convertToFloat() const { - assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle && + assert(semantics == (const llvm::fltSemantics *)&semIEEEsingle && "Float semantics are not IEEEsingle"); APInt api = bitcastToAPInt(); return api.bitsToFloat(); } double IEEEFloat::convertToDouble() const { - assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble && + assert(semantics == (const llvm::fltSemantics *)&semIEEEdouble && "Float semantics are not IEEEdouble"); APInt api = bitcastToAPInt(); return api.bitsToDouble(); @@ -3379,7 +3366,7 @@ /// exponent = 0, integer bit 1 ("pseudodenormal") /// At the moment, the first three are treated as NaNs, the last one as Normal. void IEEEFloat::initFromF80LongDoubleAPInt(const APInt &api) { - assert(api.getBitWidth()==80); + assert(api.getBitWidth() == 80); uint64_t i1 = api.getRawData()[0]; uint64_t i2 = api.getRawData()[1]; uint64_t myexponent = (i2 & 0x7fff); @@ -3387,12 +3374,12 @@ uint8_t myintegerbit = mysignificand >> 63; initialize(&semX87DoubleExtended); - assert(partCount()==2); + assert(partCount() == 2); - sign = static_cast(i2>>15); + sign = static_cast(i2 >> 15); if (myexponent == 0 && mysignificand == 0) { makeZero(sign); - } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) { + } else if (myexponent == 0x7fff && mysignificand == 0x8000000000000000ULL) { makeInf(sign); } else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) || (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) { @@ -3405,13 +3392,13 @@ exponent = myexponent - 16383; significandParts()[0] = mysignificand; significandParts()[1] = 0; - if (myexponent==0) // denormal + if (myexponent == 0) // denormal exponent = -16382; } } void IEEEFloat::initFromPPCDoubleDoubleAPInt(const APInt &api) { - assert(api.getBitWidth()==128); + assert(api.getBitWidth() == 128); uint64_t i1 = api.getRawData()[0]; uint64_t i2 = api.getRawData()[1]; opStatus fs; @@ -3435,25 +3422,24 @@ } void IEEEFloat::initFromQuadrupleAPInt(const APInt &api) { - assert(api.getBitWidth()==128); + assert(api.getBitWidth() == 128); uint64_t i1 = api.getRawData()[0]; uint64_t i2 = api.getRawData()[1]; uint64_t myexponent = (i2 >> 48) & 0x7fff; - uint64_t mysignificand = i1; + uint64_t mysignificand = i1; uint64_t mysignificand2 = i2 & 0xffffffffffffLL; initialize(&semIEEEquad); - assert(partCount()==2); + assert(partCount() == 2); - sign = static_cast(i2>>63); - if (myexponent==0 && - (mysignificand==0 && mysignificand2==0)) { + sign = static_cast(i2 >> 63); + if (myexponent == 0 && (mysignificand == 0 && mysignificand2 == 0)) { makeZero(sign); - } else if (myexponent==0x7fff && - (mysignificand==0 && mysignificand2==0)) { + } else if (myexponent == 0x7fff && + (mysignificand == 0 && mysignificand2 == 0)) { makeInf(sign); - } else if (myexponent==0x7fff && - (mysignificand!=0 || mysignificand2 !=0)) { + } else if (myexponent == 0x7fff && + (mysignificand != 0 || mysignificand2 != 0)) { category = fcNaN; exponent = exponentNaN(); significandParts()[0] = mysignificand; @@ -3463,28 +3449,28 @@ exponent = myexponent - 16383; significandParts()[0] = mysignificand; significandParts()[1] = mysignificand2; - if (myexponent==0) // denormal + if (myexponent == 0) // denormal exponent = -16382; else - significandParts()[1] |= 0x1000000000000LL; // integer bit + significandParts()[1] |= 0x1000000000000LL; // integer bit } } void IEEEFloat::initFromDoubleAPInt(const APInt &api) { - assert(api.getBitWidth()==64); + assert(api.getBitWidth() == 64); uint64_t i = *api.getRawData(); uint64_t myexponent = (i >> 52) & 0x7ff; uint64_t mysignificand = i & 0xfffffffffffffLL; initialize(&semIEEEdouble); - assert(partCount()==1); + assert(partCount() == 1); - sign = static_cast(i>>63); - if (myexponent==0 && mysignificand==0) { + sign = static_cast(i >> 63); + if (myexponent == 0 && mysignificand == 0) { makeZero(sign); - } else if (myexponent==0x7ff && mysignificand==0) { + } else if (myexponent == 0x7ff && mysignificand == 0) { makeInf(sign); - } else if (myexponent==0x7ff && mysignificand!=0) { + } else if (myexponent == 0x7ff && mysignificand != 0) { category = fcNaN; exponent = exponentNaN(); *significandParts() = mysignificand; @@ -3492,36 +3478,36 @@ category = fcNormal; exponent = myexponent - 1023; *significandParts() = mysignificand; - if (myexponent==0) // denormal + if (myexponent == 0) // denormal exponent = -1022; else - *significandParts() |= 0x10000000000000LL; // integer bit + *significandParts() |= 0x10000000000000LL; // integer bit } } void IEEEFloat::initFromFloatAPInt(const APInt &api) { - assert(api.getBitWidth()==32); + assert(api.getBitWidth() == 32); uint32_t i = (uint32_t)*api.getRawData(); uint32_t myexponent = (i >> 23) & 0xff; uint32_t mysignificand = i & 0x7fffff; initialize(&semIEEEsingle); - assert(partCount()==1); + assert(partCount() == 1); sign = i >> 31; - if (myexponent==0 && mysignificand==0) { + if (myexponent == 0 && mysignificand == 0) { makeZero(sign); - } else if (myexponent==0xff && mysignificand==0) { + } else if (myexponent == 0xff && mysignificand == 0) { makeInf(sign); - } else if (myexponent==0xff && mysignificand!=0) { + } else if (myexponent == 0xff && mysignificand != 0) { category = fcNaN; exponent = exponentNaN(); *significandParts() = mysignificand; } else { category = fcNormal; - exponent = myexponent - 127; //bias + exponent = myexponent - 127; // bias *significandParts() = mysignificand; - if (myexponent==0) // denormal + if (myexponent == 0) // denormal exponent = -126; else *significandParts() |= 0x800000; // integer bit @@ -3558,28 +3544,28 @@ } void IEEEFloat::initFromHalfAPInt(const APInt &api) { - assert(api.getBitWidth()==16); + assert(api.getBitWidth() == 16); uint32_t i = (uint32_t)*api.getRawData(); uint32_t myexponent = (i >> 10) & 0x1f; uint32_t mysignificand = i & 0x3ff; initialize(&semIEEEhalf); - assert(partCount()==1); + assert(partCount() == 1); sign = i >> 15; - if (myexponent==0 && mysignificand==0) { + if (myexponent == 0 && mysignificand == 0) { makeZero(sign); - } else if (myexponent==0x1f && mysignificand==0) { + } else if (myexponent == 0x1f && mysignificand == 0) { makeInf(sign); - } else if (myexponent==0x1f && mysignificand!=0) { + } else if (myexponent == 0x1f && mysignificand != 0) { category = fcNaN; exponent = exponentNaN(); *significandParts() = mysignificand; } else { category = fcNormal; - exponent = myexponent - 15; //bias + exponent = myexponent - 15; // bias *significandParts() = mysignificand; - if (myexponent==0) // denormal + if (myexponent == 0) // denormal exponent = -14; else *significandParts() |= 0x400; // integer bit @@ -3623,12 +3609,12 @@ // Use memset to set all but the highest integerPart to all ones. integerPart *significand = significandParts(); unsigned PartCount = partCount(); - memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1)); + memset(significand, 0xFF, sizeof(integerPart) * (PartCount - 1)); // Set the high integerPart especially setting all unused top bits for // internal consistency. const unsigned NumUnusedHighBits = - PartCount*integerPartWidth - semantics->precision; + PartCount * integerPartWidth - semantics->precision; significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth) ? (~integerPart(0) >> NumUnusedHighBits) : 0; @@ -3674,87 +3660,89 @@ } namespace { - void append(SmallVectorImpl &Buffer, StringRef Str) { - Buffer.append(Str.begin(), Str.end()); - } - - /// Removes data from the given significand until it is no more - /// precise than is required for the desired precision. - void AdjustToPrecision(APInt &significand, - int &exp, unsigned FormatPrecision) { - unsigned bits = significand.getActiveBits(); - - // 196/59 is a very slight overestimate of lg_2(10). - unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59; +void append(SmallVectorImpl &Buffer, StringRef Str) { + Buffer.append(Str.begin(), Str.end()); +} - if (bits <= bitsRequired) return; +/// Removes data from the given significand until it is no more +/// precise than is required for the desired precision. +void AdjustToPrecision(APInt &significand, int &exp, unsigned FormatPrecision) { + unsigned bits = significand.getActiveBits(); - unsigned tensRemovable = (bits - bitsRequired) * 59 / 196; - if (!tensRemovable) return; + // 196/59 is a very slight overestimate of lg_2(10). + unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59; - exp += tensRemovable; + if (bits <= bitsRequired) + return; - APInt divisor(significand.getBitWidth(), 1); - APInt powten(significand.getBitWidth(), 10); - while (true) { - if (tensRemovable & 1) - divisor *= powten; - tensRemovable >>= 1; - if (!tensRemovable) break; - powten *= powten; - } + unsigned tensRemovable = (bits - bitsRequired) * 59 / 196; + if (!tensRemovable) + return; - significand = significand.udiv(divisor); + exp += tensRemovable; - // Truncate the significand down to its active bit count. - significand = significand.trunc(significand.getActiveBits()); + APInt divisor(significand.getBitWidth(), 1); + APInt powten(significand.getBitWidth(), 10); + while (true) { + if (tensRemovable & 1) + divisor *= powten; + tensRemovable >>= 1; + if (!tensRemovable) + break; + powten *= powten; } + significand = significand.udiv(divisor); - void AdjustToPrecision(SmallVectorImpl &buffer, - int &exp, unsigned FormatPrecision) { - unsigned N = buffer.size(); - if (N <= FormatPrecision) return; + // Truncate the significand down to its active bit count. + significand = significand.trunc(significand.getActiveBits()); +} - // The most significant figures are the last ones in the buffer. - unsigned FirstSignificant = N - FormatPrecision; +void AdjustToPrecision(SmallVectorImpl &buffer, int &exp, + unsigned FormatPrecision) { + unsigned N = buffer.size(); + if (N <= FormatPrecision) + return; - // Round. - // FIXME: this probably shouldn't use 'round half up'. + // The most significant figures are the last ones in the buffer. + unsigned FirstSignificant = N - FormatPrecision; - // Rounding down is just a truncation, except we also want to drop - // trailing zeros from the new result. - if (buffer[FirstSignificant - 1] < '5') { - while (FirstSignificant < N && buffer[FirstSignificant] == '0') - FirstSignificant++; + // Round. + // FIXME: this probably shouldn't use 'round half up'. - exp += FirstSignificant; - buffer.erase(&buffer[0], &buffer[FirstSignificant]); - return; - } + // Rounding down is just a truncation, except we also want to drop + // trailing zeros from the new result. + if (buffer[FirstSignificant - 1] < '5') { + while (FirstSignificant < N && buffer[FirstSignificant] == '0') + FirstSignificant++; - // Rounding up requires a decimal add-with-carry. If we continue - // the carry, the newly-introduced zeros will just be truncated. - for (unsigned I = FirstSignificant; I != N; ++I) { - if (buffer[I] == '9') { - FirstSignificant++; - } else { - buffer[I]++; - break; - } - } + exp += FirstSignificant; + buffer.erase(&buffer[0], &buffer[FirstSignificant]); + return; + } - // If we carried through, we have exactly one digit of precision. - if (FirstSignificant == N) { - exp += FirstSignificant; - buffer.clear(); - buffer.push_back('1'); - return; + // Rounding up requires a decimal add-with-carry. If we continue + // the carry, the newly-introduced zeros will just be truncated. + for (unsigned I = FirstSignificant; I != N; ++I) { + if (buffer[I] == '9') { + FirstSignificant++; + } else { + buffer[I]++; + break; } + } + // If we carried through, we have exactly one digit of precision. + if (FirstSignificant == N) { exp += FirstSignificant; - buffer.erase(&buffer[0], &buffer[FirstSignificant]); + buffer.clear(); + buffer.push_back('1'); + return; } + + exp += FirstSignificant; + buffer.erase(&buffer[0], &buffer[FirstSignificant]); +} } // namespace void IEEEFloat::toString(SmallVectorImpl &Str, unsigned FormatPrecision, @@ -3766,7 +3754,8 @@ else return append(Str, "+Inf"); - case fcNaN: return append(Str, "NaN"); + case fcNaN: + return append(Str, "NaN"); case fcZero: if (isNegative()) @@ -3793,10 +3782,10 @@ Str.push_back('-'); // Decompose the number into an APInt and an exponent. - int exp = exponent - ((int) semantics->precision - 1); - APInt significand(semantics->precision, - makeArrayRef(significandParts(), - partCountForBits(semantics->precision))); + int exp = exponent - ((int)semantics->precision - 1); + APInt significand( + semantics->precision, + makeArrayRef(significandParts(), partCountForBits(semantics->precision))); // Set FormatPrecision if zero. We want to do this before we // truncate trailing zeros, as those are part of the precision. @@ -3843,10 +3832,12 @@ significand = significand.zext(precision); APInt five_to_the_i(precision, 5); while (true) { - if (texp & 1) significand *= five_to_the_i; + if (texp & 1) + significand *= five_to_the_i; texp >>= 1; - if (!texp) break; + if (!texp) + break; five_to_the_i *= five_to_the_i; } } @@ -3869,9 +3860,10 @@ unsigned d = digit.getZExtValue(); // Drop trailing zeros. - if (inTrail && !d) exp++; + if (inTrail && !d) + exp++; else { - buffer.push_back((char) ('0' + d)); + buffer.push_back((char)('0' + d)); inTrail = false; } } @@ -3893,18 +3885,18 @@ // 765e3 --> 765000 // ^^^ // But we shouldn't make the number look more precise than it is. - FormatScientific = ((unsigned) exp > FormatMaxPadding || - NDigits + (unsigned) exp > FormatPrecision); + FormatScientific = ((unsigned)exp > FormatMaxPadding || + NDigits + (unsigned)exp > FormatPrecision); } else { // Power of the most significant digit. - int MSD = exp + (int) (NDigits - 1); + int MSD = exp + (int)(NDigits - 1); if (MSD >= 0) { // 765e-2 == 7.65 FormatScientific = false; } else { // 765e-5 == 0.00765 // ^ ^^ - FormatScientific = ((unsigned) -MSD) > FormatMaxPadding; + FormatScientific = ((unsigned)-MSD) > FormatMaxPadding; } } } @@ -3913,13 +3905,13 @@ if (FormatScientific) { exp += (NDigits - 1); - Str.push_back(buffer[NDigits-1]); + Str.push_back(buffer[NDigits - 1]); Str.push_back('.'); if (NDigits == 1 && TruncateZero) Str.push_back('0'); else for (unsigned I = 1; I != NDigits; ++I) - Str.push_back(buffer[NDigits-1-I]); + Str.push_back(buffer[NDigits - 1 - I]); // Fill with zeros up to FormatPrecision. if (!TruncateZero && FormatPrecision > NDigits - 1) Str.append(FormatPrecision - NDigits + 1, '0'); @@ -3927,25 +3919,26 @@ Str.push_back(TruncateZero ? 'E' : 'e'); Str.push_back(exp >= 0 ? '+' : '-'); - if (exp < 0) exp = -exp; + if (exp < 0) + exp = -exp; SmallVector expbuf; do { - expbuf.push_back((char) ('0' + (exp % 10))); + expbuf.push_back((char)('0' + (exp % 10))); exp /= 10; } while (exp); // Exponent always at least two digits if we do not truncate zeros. if (!TruncateZero && expbuf.size() < 2) expbuf.push_back('0'); for (unsigned I = 0, E = expbuf.size(); I != E; ++I) - Str.push_back(expbuf[E-1-I]); + Str.push_back(expbuf[E - 1 - I]); return; } // Non-scientific, positive exponents. if (exp >= 0) { for (unsigned I = 0; I != NDigits; ++I) - Str.push_back(buffer[NDigits-1-I]); - for (unsigned I = 0; I != (unsigned) exp; ++I) + Str.push_back(buffer[NDigits - 1 - I]); + for (unsigned I = 0; I != (unsigned)exp; ++I) Str.push_back('0'); return; } @@ -3953,15 +3946,15 @@ // Non-scientific, negative exponents. // The number of digits to the left of the decimal point. - int NWholeDigits = exp + (int) NDigits; + int NWholeDigits = exp + (int)NDigits; unsigned I = 0; if (NWholeDigits > 0) { - for (; I != (unsigned) NWholeDigits; ++I) - Str.push_back(buffer[NDigits-I-1]); + for (; I != (unsigned)NWholeDigits; ++I) + Str.push_back(buffer[NDigits - I - 1]); Str.push_back('.'); } else { - unsigned NZeros = 1 + (unsigned) -NWholeDigits; + unsigned NZeros = 1 + (unsigned)-NWholeDigits; Str.push_back('0'); Str.push_back('.'); @@ -3970,7 +3963,7 @@ } for (; I != NDigits; ++I) - Str.push_back(buffer[NDigits-I-1]); + Str.push_back(buffer[NDigits - I - 1]); } bool IEEEFloat::getExactInverse(APFloat *inv) const { @@ -4073,7 +4066,7 @@ // smallest binade or are dealing with denormals. // 2. Our significand excluding the integral bit is all zeros. bool WillCrossBinadeBoundary = - exponent != semantics->minExponent && isSignificandAllZeros(); + exponent != semantics->minExponent && isSignificandAllZeros(); // Decrement the significand. // @@ -4793,10 +4786,9 @@ return; } if (usesLayout(Semantics)) { - const fltSemantics& S = F.getSemantics(); - new (&Double) - DoubleAPFloat(Semantics, APFloat(std::move(F), S), - APFloat(semIEEEdouble)); + const fltSemantics &S = F.getSemantics(); + new (&Double) DoubleAPFloat(Semantics, APFloat(std::move(F), S), + APFloat(semIEEEdouble)); return; } llvm_unreachable("Unexpected semantics"); diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -34,7 +34,7 @@ /// A utility function for allocating memory, checking for allocation failures, /// and ensuring the contents are zeroed. -inline static uint64_t* getClearedMemory(unsigned numWords) { +inline static uint64_t *getClearedMemory(unsigned numWords) { uint64_t *result = new uint64_t[numWords]; memset(result, 0, numWords * sizeof(uint64_t)); return result; @@ -42,7 +42,7 @@ /// A utility function for allocating memory and checking for allocation /// failure. The content is not zeroed. -inline static uint64_t* getMemory(unsigned numWords) { +inline static uint64_t *getMemory(unsigned numWords) { return new uint64_t[numWords]; } @@ -73,7 +73,6 @@ return -1U; } - void APInt::initSlowCase(uint64_t val, bool isSigned) { U.pVal = getClearedMemory(getNumWords()); U.pVal[0] = val; @@ -83,7 +82,7 @@ clearUnusedBits(); } -void APInt::initSlowCase(const APInt& that) { +void APInt::initSlowCase(const APInt &that) { U.pVal = getMemory(getNumWords()); memcpy(U.pVal, that.U.pVal, getNumWords() * APINT_WORD_SIZE); } @@ -105,18 +104,17 @@ clearUnusedBits(); } -APInt::APInt(unsigned numBits, ArrayRef bigVal) - : BitWidth(numBits) { +APInt::APInt(unsigned numBits, ArrayRef bigVal) : BitWidth(numBits) { initFromArray(bigVal); } APInt::APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]) - : BitWidth(numBits) { + : BitWidth(numBits) { initFromArray(makeArrayRef(bigVal, numWords)); } APInt::APInt(unsigned numbits, StringRef Str, uint8_t radix) - : BitWidth(numbits) { + : BitWidth(numbits) { assert(BitWidth && "Bitwidth too small"); fromString(numbits, Str, radix); } @@ -130,7 +128,7 @@ // If we have an allocation, delete it. if (!isSingleWord()) - delete [] U.pVal; + delete[] U.pVal; // Update BitWidth. BitWidth = NewBitWidth; @@ -140,7 +138,7 @@ U.pVal = getMemory(getNumWords()); } -void APInt::AssignSlowCase(const APInt& RHS) { +void APInt::AssignSlowCase(const APInt &RHS) { // Don't do anything for X = X if (this == &RHS) return; @@ -156,7 +154,7 @@ } /// This method 'profiles' an APInt for use with FoldingSet. -void APInt::Profile(FoldingSetNodeID& ID) const { +void APInt::Profile(FoldingSetNodeID &ID) const { ID.AddInteger(BitWidth); if (isSingleWord()) { @@ -170,7 +168,7 @@ } /// Prefix increment operator. Increments the APInt by one. -APInt& APInt::operator++() { +APInt &APInt::operator++() { if (isSingleWord()) ++U.VAL; else @@ -179,7 +177,7 @@ } /// Prefix decrement operator. Decrements the APInt by one. -APInt& APInt::operator--() { +APInt &APInt::operator--() { if (isSingleWord()) --U.VAL; else @@ -190,7 +188,7 @@ /// Adds the RHS APInt to this APInt. /// @returns this, after addition of RHS. /// Addition assignment operator. -APInt& APInt::operator+=(const APInt& RHS) { +APInt &APInt::operator+=(const APInt &RHS) { assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); if (isSingleWord()) U.VAL += RHS.U.VAL; @@ -199,7 +197,7 @@ return clearUnusedBits(); } -APInt& APInt::operator+=(uint64_t RHS) { +APInt &APInt::operator+=(uint64_t RHS) { if (isSingleWord()) U.VAL += RHS; else @@ -210,7 +208,7 @@ /// Subtracts the RHS APInt from this APInt /// @returns this, after subtraction /// Subtraction assignment operator. -APInt& APInt::operator-=(const APInt& RHS) { +APInt &APInt::operator-=(const APInt &RHS) { assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); if (isSingleWord()) U.VAL -= RHS.U.VAL; @@ -219,7 +217,7 @@ return clearUnusedBits(); } -APInt& APInt::operator-=(uint64_t RHS) { +APInt &APInt::operator-=(uint64_t RHS) { if (isSingleWord()) U.VAL -= RHS; else @@ -227,7 +225,7 @@ return clearUnusedBits(); } -APInt APInt::operator*(const APInt& RHS) const { +APInt APInt::operator*(const APInt &RHS) const { assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); if (isSingleWord()) return APInt(BitWidth, U.VAL * RHS.U.VAL); @@ -240,25 +238,31 @@ return Result; } -void APInt::AndAssignSlowCase(const APInt& RHS) { - tcAnd(U.pVal, RHS.U.pVal, getNumWords()); +void APInt::AndAssignSlowCase(const APInt &RHS) { + WordType *dst = U.pVal, *rhs = RHS.U.pVal; + for (size_t i = 0, e = getNumWords(); i != e; ++i) + dst[i] &= rhs[i]; } -void APInt::OrAssignSlowCase(const APInt& RHS) { - tcOr(U.pVal, RHS.U.pVal, getNumWords()); +void APInt::OrAssignSlowCase(const APInt &RHS) { + WordType *dst = U.pVal, *rhs = RHS.U.pVal; + for (size_t i = 0, e = getNumWords(); i != e; ++i) + dst[i] |= rhs[i]; } -void APInt::XorAssignSlowCase(const APInt& RHS) { - tcXor(U.pVal, RHS.U.pVal, getNumWords()); +void APInt::XorAssignSlowCase(const APInt &RHS) { + WordType *dst = U.pVal, *rhs = RHS.U.pVal; + for (size_t i = 0, e = getNumWords(); i != e; ++i) + dst[i] ^= rhs[i]; } -APInt& APInt::operator*=(const APInt& RHS) { +APInt &APInt::operator*=(const APInt &RHS) { assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); *this = *this * RHS; return *this; } -APInt& APInt::operator*=(uint64_t RHS) { +APInt &APInt::operator*=(uint64_t RHS) { if (isSingleWord()) { U.VAL *= RHS; } else { @@ -268,11 +272,11 @@ return clearUnusedBits(); } -bool APInt::EqualSlowCase(const APInt& RHS) const { +bool APInt::EqualSlowCase(const APInt &RHS) const { return std::equal(U.pVal, U.pVal + getNumWords(), RHS.U.pVal); } -int APInt::compare(const APInt& RHS) const { +int APInt::compare(const APInt &RHS) const { assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); if (isSingleWord()) return U.VAL < RHS.U.VAL ? -1 : U.VAL > RHS.U.VAL; @@ -280,7 +284,7 @@ return tcCompare(U.pVal, RHS.U.pVal, getNumWords()); } -int APInt::compareSigned(const APInt& RHS) const { +int APInt::compareSigned(const APInt &RHS) const { assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"); if (isSingleWord()) { int64_t lhsSext = SignExtend64(U.VAL, BitWidth); @@ -327,6 +331,12 @@ U.pVal[word] = WORDTYPE_MAX; } +/* Complement a bignum in-place. */ +static void tcComplement(APInt::WordType *dst, unsigned parts) { + for (unsigned i = 0; i < parts; i++) + dst[i] = ~dst[i]; +} + /// Toggle every bit to its opposite value. void APInt::flipAllBitsSlowCase() { tcComplement(U.pVal, getNumWords()); @@ -396,7 +406,8 @@ setBitVal(bitPosition + i, subBits[i]); } -void APInt::insertBits(uint64_t subBits, unsigned bitPosition, unsigned numBits) { +void APInt::insertBits(uint64_t subBits, unsigned bitPosition, + unsigned numBits) { uint64_t maskBits = maskTrailingOnes(numBits); subBits &= maskBits; if (isSingleWord()) { @@ -414,7 +425,8 @@ return; } - static_assert(8 * sizeof(WordType) <= 64, "This code assumes only two words affected"); + static_assert(8 * sizeof(WordType) <= 64, + "This code assumes only two words affected"); unsigned wordBits = 8 * sizeof(WordType); U.pVal[loWord] &= ~(maskBits << loBit); U.pVal[loWord] |= subBits << loBit; @@ -477,7 +489,8 @@ if (loWord == hiWord) return (U.pVal[loWord] >> loBit) & maskBits; - static_assert(8 * sizeof(WordType) <= 64, "This code assumes only two words affected"); + static_assert(8 * sizeof(WordType) <= 64, + "This code assumes only two words affected"); unsigned wordBits = 8 * sizeof(WordType); uint64_t retBits = U.pVal[loWord] >> loBit; retBits |= U.pVal[hiWord] << (wordBits - loBit); @@ -487,9 +500,9 @@ unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) { assert(!str.empty() && "Invalid string length"); - assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || - radix == 36) && - "Radix should be 2, 8, 10, 16, or 36!"); + assert( + (radix == 10 || radix == 8 || radix == 16 || radix == 2 || radix == 36) && + "Radix should be 2, 8, 10, 16, or 36!"); size_t slen = str.size(); @@ -521,9 +534,8 @@ // be too large. This avoids the assertion in the constructor. This // calculation doesn't work appropriately for the numbers 0-9, so just use 4 // bits in that case. - unsigned sufficient - = radix == 10? (slen == 1 ? 4 : slen * 64/18) - : (slen == 1 ? 7 : slen * 16/3); + unsigned sufficient = radix == 10 ? (slen == 1 ? 4 : slen * 64 / 18) + : (slen == 1 ? 7 : slen * 16 / 3); // Convert to the actual binary value. APInt tmp(sufficient, StringRef(p, slen), radix); @@ -587,7 +599,7 @@ unsigned APInt::countLeadingZerosSlowCase() const { unsigned Count = 0; - for (int i = getNumWords()-1; i >= 0; --i) { + for (int i = getNumWords() - 1; i >= 0; --i) { uint64_t V = U.pVal[i]; if (V == 0) Count += APINT_BITS_PER_WORD; @@ -722,11 +734,14 @@ APInt llvm::APIntOps::GreatestCommonDivisor(APInt A, APInt B) { // Fast-path a common case. - if (A == B) return A; + if (A == B) + return A; // Corner cases: if either operand is zero, the other is the gcd. - if (!A) return B; - if (!B) return A; + if (!A) + return B; + if (!B) + return A; // Count common powers of 2 and remove all other powers of 2. unsigned Pow2; @@ -781,8 +796,8 @@ // If the exponent doesn't shift all bits out of the mantissa if (exp < 52) - return isNeg ? -APInt(width, mantissa >> (52 - exp)) : - APInt(width, mantissa >> (52 - exp)); + return isNeg ? -APInt(width, mantissa >> (52 - exp)) + : APInt(width, mantissa >> (52 - exp)); // If the client didn't provide enough bits for us to shift the mantissa into // then the result is undefined, just return 0 @@ -805,7 +820,8 @@ double APInt::roundToDouble(bool isSigned) const { // Handle the simple case where the value is contained in one uint64_t. - // It is wrong to optimize getWord(0) to VAL; there might be more than one word. + // It is wrong to optimize getWord(0) to VAL; there might be more than one + // word. if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) { if (isSigned) { int64_t sext = SignExtend64(getWord(0), BitWidth); @@ -815,7 +831,7 @@ } // Determine if the value is negative. - bool isNeg = isSigned ? (*this)[BitWidth-1] : false; + bool isNeg = isSigned ? (*this)[BitWidth - 1] : false; // Construct the absolute value if we're negative. APInt Tmp(isNeg ? -(*this) : (*this)); @@ -840,7 +856,7 @@ // Number of bits in mantissa is 52. To obtain the mantissa value, we must // extract the high 52 bits from the correct words in pVal. uint64_t mantissa; - unsigned hiWord = whichWord(n-1); + unsigned hiWord = whichWord(n - 1); if (hiWord == 0) { mantissa = Tmp.U.pVal[0]; if (n > 52) @@ -848,7 +864,7 @@ } else { assert(hiWord > 0 && "huh?"); uint64_t hibits = Tmp.U.pVal[hiWord] << (52 - n % APINT_BITS_PER_WORD); - uint64_t lobits = Tmp.U.pVal[hiWord-1] >> (11 + n % APINT_BITS_PER_WORD); + uint64_t lobits = Tmp.U.pVal[hiWord - 1] >> (11 + n % APINT_BITS_PER_WORD); mantissa = hibits | lobits; } @@ -1015,8 +1031,9 @@ } else { // Move the words containing significant bits. for (unsigned i = 0; i != WordsToMove - 1; ++i) - U.pVal[i] = (U.pVal[i + WordShift] >> BitShift) | - (U.pVal[i + WordShift + 1] << (APINT_BITS_PER_WORD - BitShift)); + U.pVal[i] = + (U.pVal[i + WordShift] >> BitShift) | + (U.pVal[i + WordShift + 1] << (APINT_BITS_PER_WORD - BitShift)); // Handle the last word which has no high bits to copy. U.pVal[WordsToMove - 1] = U.pVal[WordShift + WordsToMove - 1] >> BitShift; @@ -1092,6 +1109,35 @@ return lshr(rotateAmt) | shl(BitWidth - rotateAmt); } +/// \returns the nearest log base 2 of this APInt. Ties round up. +/// +/// NOTE: When we have a BitWidth of 1, we define: +/// +/// log2(0) = UINT32_MAX +/// log2(1) = 0 +/// +/// to get around any mathematical concerns resulting from +/// referencing 2 in a space where 2 does no exist. +unsigned APInt::nearestLogBase2() const { + // Special case when we have a bitwidth of 1. If VAL is 1, then we + // get 0. If VAL is 0, we get WORDTYPE_MAX which gets truncated to + // UINT32_MAX. + if (BitWidth == 1) + return U.VAL - 1; + + // Handle the zero case. + if (isNullValue()) + return UINT32_MAX; + + // The non-zero case is handled by computing: + // + // nearestLogBase2(x) = logBase2(x) + x[logBase2(x)-1]. + // + // where x[i] is referring to the value of the ith bit of x. + unsigned lg = logBase2(); + return lg + unsigned((*this)[lg - 1]); +} + // Square Root - this method computes and returns the square root of "this". // Three mechanisms are used for computation. For small values (<= 5 bits), // a table lookup is done. This gets some performance for common cases. For @@ -1108,15 +1154,14 @@ // rounding errors in libc sqrt for small values. if (magnitude <= 5) { static const uint8_t results[32] = { - /* 0 */ 0, - /* 1- 2 */ 1, 1, - /* 3- 6 */ 2, 2, 2, 2, - /* 7-12 */ 3, 3, 3, 3, 3, 3, - /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4, - /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - /* 31 */ 6 - }; - return APInt(BitWidth, results[ (isSingleWord() ? U.VAL : U.pVal[0]) ]); + /* 0 */ 0, + /* 1- 2 */ 1, 1, + /* 3- 6 */ 2, 2, 2, 2, + /* 7-12 */ 3, 3, 3, 3, 3, 3, + /* 13-20 */ 4, 4, 4, 4, 4, 4, 4, 4, + /* 21-30 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + /* 31 */ 6}; + return APInt(BitWidth, results[(isSingleWord() ? U.VAL : U.pVal[0])]); } // If the magnitude of the value fits in less than 52 bits (the precision of @@ -1124,9 +1169,9 @@ // libc sqrt function which will probably use a hardware sqrt computation. // This should be faster than the algorithm below. if (magnitude < 52) { - return APInt(BitWidth, - uint64_t(::round(::sqrt(double(isSingleWord() ? U.VAL - : U.pVal[0]))))); + return APInt( + BitWidth, + uint64_t(::round(::sqrt(double(isSingleWord() ? U.VAL : U.pVal[0]))))); } // Okay, all the short cuts are exhausted. We must compute it. The following @@ -1162,7 +1207,7 @@ // floating point representation after 192 bits. There are no discrepancies // between this algorithm and pari/gp for bit widths < 192 bits. APInt square(x_old * x_old); - APInt nextSquare((x_old + 1) * (x_old +1)); + APInt nextSquare((x_old + 1) * (x_old + 1)); if (this->ult(square)) return x_old; assert(this->ule(nextSquare) && "Error in APInt::sqrt computation"); @@ -1180,7 +1225,7 @@ /// (potentially large) APInts around. /// WARNING: a value of '0' may be returned, /// signifying that no multiplicative inverse exists! -APInt APInt::multiplicativeInverse(const APInt& modulo) const { +APInt APInt::multiplicativeInverse(const APInt &modulo) const { assert(ult(modulo) && "This APInt must be smaller than the modulo"); // Using the properties listed at the following web page (accessed 06/21/08): @@ -1191,18 +1236,18 @@ // inverse exists, but may not suffice for the general extended Euclidean // algorithm. - APInt r[2] = { modulo, *this }; - APInt t[2] = { APInt(BitWidth, 0), APInt(BitWidth, 1) }; + APInt r[2] = {modulo, *this}; + APInt t[2] = {APInt(BitWidth, 0), APInt(BitWidth, 1)}; APInt q(BitWidth, 0); unsigned i; - for (i = 0; r[i^1] != 0; i ^= 1) { + for (i = 0; r[i ^ 1] != 0; i ^= 1) { // An overview of the math without the confusing bit-flipping: // q = r[i-2] / r[i-1] // r[i] = r[i-2] % r[i-1] // t[i] = t[i-2] - t[i-1] * q - udivrem(r[i], r[i^1], q, r[i]); - t[i] -= t[i^1] * q; + udivrem(r[i], r[i ^ 1], q, r[i]); + t[i] -= t[i ^ 1] * q; } // If this APInt and the modulo are not coprime, there is no multiplicative @@ -1222,109 +1267,17 @@ return std::move(t[i]); } -/// Calculate the magic numbers required to implement a signed integer division -/// by a constant as a sequence of multiplies, adds and shifts. Requires that -/// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S. -/// Warren, Jr., chapter 10. -APInt::ms APInt::magic() const { - const APInt& d = *this; - unsigned p; - APInt ad, anc, delta, q1, r1, q2, r2, t; - APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); - struct ms mag; - - ad = d.abs(); - t = signedMin + (d.lshr(d.getBitWidth() - 1)); - anc = t - 1 - t.urem(ad); // absolute value of nc - p = d.getBitWidth() - 1; // initialize p - q1 = signedMin.udiv(anc); // initialize q1 = 2p/abs(nc) - r1 = signedMin - q1*anc; // initialize r1 = rem(2p,abs(nc)) - q2 = signedMin.udiv(ad); // initialize q2 = 2p/abs(d) - r2 = signedMin - q2*ad; // initialize r2 = rem(2p,abs(d)) - do { - p = p + 1; - q1 = q1<<1; // update q1 = 2p/abs(nc) - r1 = r1<<1; // update r1 = rem(2p/abs(nc)) - if (r1.uge(anc)) { // must be unsigned comparison - q1 = q1 + 1; - r1 = r1 - anc; - } - q2 = q2<<1; // update q2 = 2p/abs(d) - r2 = r2<<1; // update r2 = rem(2p/abs(d)) - if (r2.uge(ad)) { // must be unsigned comparison - q2 = q2 + 1; - r2 = r2 - ad; - } - delta = ad - r2; - } while (q1.ult(delta) || (q1 == delta && r1 == 0)); - - mag.m = q2 + 1; - if (d.isNegative()) mag.m = -mag.m; // resulting magic number - mag.s = p - d.getBitWidth(); // resulting shift - return mag; -} - -/// Calculate the magic numbers required to implement an unsigned integer -/// division by a constant as a sequence of multiplies, adds and shifts. -/// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry -/// S. Warren, Jr., chapter 10. -/// LeadingZeros can be used to simplify the calculation if the upper bits -/// of the divided value are known zero. -APInt::mu APInt::magicu(unsigned LeadingZeros) const { - const APInt& d = *this; - unsigned p; - APInt nc, delta, q1, r1, q2, r2; - struct mu magu; - magu.a = 0; // initialize "add" indicator - APInt allOnes = APInt::getAllOnesValue(d.getBitWidth()).lshr(LeadingZeros); - APInt signedMin = APInt::getSignedMinValue(d.getBitWidth()); - APInt signedMax = APInt::getSignedMaxValue(d.getBitWidth()); - - nc = allOnes - (allOnes - d).urem(d); - p = d.getBitWidth() - 1; // initialize p - q1 = signedMin.udiv(nc); // initialize q1 = 2p/nc - r1 = signedMin - q1*nc; // initialize r1 = rem(2p,nc) - q2 = signedMax.udiv(d); // initialize q2 = (2p-1)/d - r2 = signedMax - q2*d; // initialize r2 = rem((2p-1),d) - do { - p = p + 1; - if (r1.uge(nc - r1)) { - q1 = q1 + q1 + 1; // update q1 - r1 = r1 + r1 - nc; // update r1 - } - else { - q1 = q1+q1; // update q1 - r1 = r1+r1; // update r1 - } - if ((r2 + 1).uge(d - r2)) { - if (q2.uge(signedMax)) magu.a = 1; - q2 = q2+q2 + 1; // update q2 - r2 = r2+r2 + 1 - d; // update r2 - } - else { - if (q2.uge(signedMin)) magu.a = 1; - q2 = q2+q2; // update q2 - r2 = r2+r2 + 1; // update r2 - } - delta = d - 1 - r2; - } while (p < d.getBitWidth()*2 && - (q1.ult(delta) || (q1 == delta && r1 == 0))); - magu.m = q2 + 1; // resulting magic number - magu.s = p - d.getBitWidth(); // resulting shift - return magu; -} - /// Implementation of Knuth's Algorithm D (Division of nonnegative integers) /// from "Art of Computer Programming, Volume 2", section 4.3.1, p. 272. The /// variables here have the same names as in the algorithm. Comments explain /// the algorithm and any deviation from it. -static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t* r, +static void KnuthDiv(uint32_t *u, uint32_t *v, uint32_t *q, uint32_t *r, unsigned m, unsigned n) { assert(u && "Must provide dividend"); assert(v && "Must provide divisor"); assert(q && "Must provide quotient"); assert(u != v && u != q && v != q && "Must use different memory"); - assert(n>1 && "n must be > 1"); + assert(n > 1 && "n must be > 1"); // b denotes the base of the number system. In our case b is 2^32. const uint64_t b = uint64_t(1) << 32; @@ -1334,7 +1287,9 @@ #ifdef KNUTH_DEBUG #define DEBUG_KNUTH(X) LLVM_DEBUG(X) #else -#define DEBUG_KNUTH(X) do {} while(false) +#define DEBUG_KNUTH(X) \ + do { \ + } while (false) #endif DEBUG_KNUTH(dbgs() << "KnuthDiv: m=" << m << " n=" << n << '\n'); @@ -1351,11 +1306,11 @@ // and v so that its high bits are shifted to the top of v's range without // overflow. Note that this can require an extra word in u so that u must // be of length m+n+1. - unsigned shift = countLeadingZeros(v[n-1]); + unsigned shift = countLeadingZeros(v[n - 1]); uint32_t v_carry = 0; uint32_t u_carry = 0; if (shift) { - for (unsigned i = 0; i < m+n; ++i) { + for (unsigned i = 0; i < m + n; ++i) { uint32_t u_tmp = u[i] >> (32 - shift); u[i] = (u[i] << shift) | u_carry; u_carry = u_tmp; @@ -1366,7 +1321,7 @@ v_carry = v_tmp; } } - u[m+n] = u_carry; + u[m + n] = u_carry; DEBUG_KNUTH(dbgs() << "KnuthDiv: normal:"); DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]); @@ -1386,14 +1341,14 @@ // on v[n-2] determines at high speed most of the cases in which the trial // value qp is one too large, and it eliminates all cases where qp is two // too large. - uint64_t dividend = Make_64(u[j+n], u[j+n-1]); + uint64_t dividend = Make_64(u[j + n], u[j + n - 1]); DEBUG_KNUTH(dbgs() << "KnuthDiv: dividend == " << dividend << '\n'); - uint64_t qp = dividend / v[n-1]; - uint64_t rp = dividend % v[n-1]; - if (qp == b || qp*v[n-2] > b*rp + u[j+n-2]) { + uint64_t qp = dividend / v[n - 1]; + uint64_t rp = dividend % v[n - 1]; + if (qp == b || qp * v[n - 2] > b * rp + u[j + n - 2]) { qp--; - rp += v[n-1]; - if (rp < b && (qp == b || qp*v[n-2] > b*rp + u[j+n-2])) + rp += v[n - 1]; + if (rp < b && (qp == b || qp * v[n - 2] > b * rp + u[j + n - 2])) qp--; } DEBUG_KNUTH(dbgs() << "KnuthDiv: qp == " << qp << ", rp == " << rp << '\n'); @@ -1409,14 +1364,14 @@ int64_t borrow = 0; for (unsigned i = 0; i < n; ++i) { uint64_t p = uint64_t(qp) * uint64_t(v[i]); - int64_t subres = int64_t(u[j+i]) - borrow - Lo_32(p); - u[j+i] = Lo_32(subres); + int64_t subres = int64_t(u[j + i]) - borrow - Lo_32(p); + u[j + i] = Lo_32(subres); borrow = Hi_32(p) - Hi_32(subres); DEBUG_KNUTH(dbgs() << "KnuthDiv: u[j+i] = " << u[j + i] - << ", borrow = " << borrow << '\n'); + << ", borrow = " << borrow << '\n'); } - bool isNeg = u[j+n] < borrow; - u[j+n] -= Lo_32(borrow); + bool isNeg = u[j + n] < borrow; + u[j + n] -= Lo_32(borrow); DEBUG_KNUTH(dbgs() << "KnuthDiv: after subtraction:"); DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]); @@ -1435,11 +1390,11 @@ // since it cancels with the borrow that occurred in D4. bool carry = false; for (unsigned i = 0; i < n; i++) { - uint32_t limit = std::min(u[j+i],v[i]); - u[j+i] += v[i] + carry; - carry = u[j+i] < limit || (carry && u[j+i] == limit); + uint32_t limit = std::min(u[j + i], v[i]); + u[j + i] += v[i] + carry; + carry = u[j + i] < limit || (carry && u[j + i] == limit); } - u[j+n] += carry; + u[j + n] += carry; } DEBUG_KNUTH(dbgs() << "KnuthDiv: after correction:"); DEBUG_KNUTH(for (int i = m + n; i >= 0; i--) dbgs() << " " << u[i]); @@ -1462,13 +1417,13 @@ if (shift) { uint32_t carry = 0; DEBUG_KNUTH(dbgs() << "KnuthDiv: remainder:"); - for (int i = n-1; i >= 0; i--) { + for (int i = n - 1; i >= 0; i--) { r[i] = (u[i] >> shift) | carry; carry = u[i] << (32 - shift); DEBUG_KNUTH(dbgs() << " " << r[i]); } } else { - for (int i = n-1; i >= 0; i--) { + for (int i = n - 1; i >= 0; i--) { r[i] = u[i]; DEBUG_KNUTH(dbgs() << " " << r[i]); } @@ -1499,31 +1454,31 @@ uint32_t *V = nullptr; uint32_t *Q = nullptr; uint32_t *R = nullptr; - if ((Remainder?4:3)*n+2*m+1 <= 128) { + if ((Remainder ? 4 : 3) * n + 2 * m + 1 <= 128) { U = &SPACE[0]; - V = &SPACE[m+n+1]; - Q = &SPACE[(m+n+1) + n]; + V = &SPACE[m + n + 1]; + Q = &SPACE[(m + n + 1) + n]; if (Remainder) - R = &SPACE[(m+n+1) + n + (m+n)]; + R = &SPACE[(m + n + 1) + n + (m + n)]; } else { U = new uint32_t[m + n + 1]; V = new uint32_t[n]; - Q = new uint32_t[m+n]; + Q = new uint32_t[m + n]; if (Remainder) R = new uint32_t[n]; } // Initialize the dividend - memset(U, 0, (m+n+1)*sizeof(uint32_t)); + memset(U, 0, (m + n + 1) * sizeof(uint32_t)); for (unsigned i = 0; i < lhsWords; ++i) { uint64_t tmp = LHS[i]; U[i * 2] = Lo_32(tmp); U[i * 2 + 1] = Hi_32(tmp); } - U[m+n] = 0; // this extra word is for "spill" in the Knuth algorithm. + U[m + n] = 0; // this extra word is for "spill" in the Knuth algorithm. // Initialize the divisor - memset(V, 0, (n)*sizeof(uint32_t)); + memset(V, 0, (n) * sizeof(uint32_t)); for (unsigned i = 0; i < rhsWords; ++i) { uint64_t tmp = RHS[i]; V[i * 2] = Lo_32(tmp); @@ -1531,7 +1486,7 @@ } // initialize the quotient and remainder - memset(Q, 0, (m+n) * sizeof(uint32_t)); + memset(Q, 0, (m + n) * sizeof(uint32_t)); if (Remainder) memset(R, 0, n * sizeof(uint32_t)); @@ -1539,11 +1494,11 @@ // the divisor. m is the number of words by which the dividend exceeds the // divisor (i.e. m+n is the length of the dividend). These sizes must not // contain any zero words or the Knuth algorithm fails. - for (unsigned i = n; i > 0 && V[i-1] == 0; i--) { + for (unsigned i = n; i > 0 && V[i - 1] == 0; i--) { n--; m++; } - for (unsigned i = m+n; i > 0 && U[i-1] == 0; i--) + for (unsigned i = m + n; i > 0 && U[i - 1] == 0; i--) m--; // If we're left with only a single word for the divisor, Knuth doesn't work @@ -1583,21 +1538,21 @@ // If the caller wants the quotient if (Quotient) { for (unsigned i = 0; i < lhsWords; ++i) - Quotient[i] = Make_64(Q[i*2+1], Q[i*2]); + Quotient[i] = Make_64(Q[i * 2 + 1], Q[i * 2]); } // If the caller wants the remainder if (Remainder) { for (unsigned i = 0; i < rhsWords; ++i) - Remainder[i] = Make_64(R[i*2+1], R[i*2]); + Remainder[i] = Make_64(R[i * 2 + 1], R[i * 2]); } // Clean up the memory we allocated. if (U != &SPACE[0]) { - delete [] U; - delete [] V; - delete [] Q; - delete [] R; + delete[] U; + delete[] V; + delete[] Q; + delete[] R; } } @@ -1612,7 +1567,7 @@ // Get some facts about the LHS and RHS number of bits and words unsigned lhsWords = getNumWords(getActiveBits()); - unsigned rhsBits = RHS.getActiveBits(); + unsigned rhsBits = RHS.getActiveBits(); unsigned rhsWords = getNumWords(rhsBits); assert(rhsWords && "Divided by zero???"); @@ -1786,8 +1741,8 @@ return this->urem(RHS); } -void APInt::udivrem(const APInt &LHS, const APInt &RHS, - APInt &Quotient, APInt &Remainder) { +void APInt::udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, + APInt &Remainder) { assert(LHS.BitWidth == RHS.BitWidth && "Bit widths must be the same"); unsigned BitWidth = LHS.BitWidth; @@ -1803,31 +1758,31 @@ // Get some size facts about the dividend and divisor unsigned lhsWords = getNumWords(LHS.getActiveBits()); - unsigned rhsBits = RHS.getActiveBits(); + unsigned rhsBits = RHS.getActiveBits(); unsigned rhsWords = getNumWords(rhsBits); assert(rhsWords && "Performing divrem operation by zero ???"); // Check the degenerate cases if (lhsWords == 0) { - Quotient = APInt(BitWidth, 0); // 0 / Y ===> 0 - Remainder = APInt(BitWidth, 0); // 0 % Y ===> 0 + Quotient = APInt(BitWidth, 0); // 0 / Y ===> 0 + Remainder = APInt(BitWidth, 0); // 0 % Y ===> 0 return; } if (rhsBits == 1) { - Quotient = LHS; // X / 1 ===> X - Remainder = APInt(BitWidth, 0); // X % 1 ===> 0 + Quotient = LHS; // X / 1 ===> X + Remainder = APInt(BitWidth, 0); // X % 1 ===> 0 } if (lhsWords < rhsWords || LHS.ult(RHS)) { - Remainder = LHS; // X % Y ===> X, iff X < Y - Quotient = APInt(BitWidth, 0); // X / Y ===> 0, iff X < Y + Remainder = LHS; // X % Y ===> X, iff X < Y + Quotient = APInt(BitWidth, 0); // X / Y ===> 0, iff X < Y return; } if (LHS == RHS) { - Quotient = APInt(BitWidth, 1); // X / X ===> 1 - Remainder = APInt(BitWidth, 0); // X % X ===> 0; + Quotient = APInt(BitWidth, 1); // X / X ===> 1 + Remainder = APInt(BitWidth, 0); // X % X ===> 0; return; } @@ -1875,26 +1830,26 @@ // Check the degenerate cases if (lhsWords == 0) { - Quotient = APInt(BitWidth, 0); // 0 / Y ===> 0 - Remainder = 0; // 0 % Y ===> 0 + Quotient = APInt(BitWidth, 0); // 0 / Y ===> 0 + Remainder = 0; // 0 % Y ===> 0 return; } if (RHS == 1) { - Quotient = LHS; // X / 1 ===> X - Remainder = 0; // X % 1 ===> 0 + Quotient = LHS; // X / 1 ===> X + Remainder = 0; // X % 1 ===> 0 return; } if (LHS.ult(RHS)) { - Remainder = LHS.getZExtValue(); // X % Y ===> X, iff X < Y - Quotient = APInt(BitWidth, 0); // X / Y ===> 0, iff X < Y + Remainder = LHS.getZExtValue(); // X % Y ===> X, iff X < Y + Quotient = APInt(BitWidth, 0); // X / Y ===> 0, iff X < Y return; } if (LHS == RHS) { - Quotient = APInt(BitWidth, 1); // X / X ===> 1 - Remainder = 0; // X % X ===> 0; + Quotient = APInt(BitWidth, 1); // X / X ===> 1 + Remainder = 0; // X % X ===> 0; return; } @@ -1918,8 +1873,8 @@ (getNumWords(BitWidth) - lhsWords) * APINT_WORD_SIZE); } -void APInt::sdivrem(const APInt &LHS, const APInt &RHS, - APInt &Quotient, APInt &Remainder) { +void APInt::sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, + APInt &Remainder) { if (LHS.isNegative()) { if (RHS.isNegative()) APInt::udivrem(-LHS, -RHS, Quotient, Remainder); @@ -1936,8 +1891,8 @@ } } -void APInt::sdivrem(const APInt &LHS, int64_t RHS, - APInt &Quotient, int64_t &Remainder) { +void APInt::sdivrem(const APInt &LHS, int64_t RHS, APInt &Quotient, + int64_t &Remainder) { uint64_t R = Remainder; if (LHS.isNegative()) { if (RHS < 0) @@ -1957,14 +1912,14 @@ } APInt APInt::sadd_ov(const APInt &RHS, bool &Overflow) const { - APInt Res = *this+RHS; + APInt Res = *this + RHS; Overflow = isNonNegative() == RHS.isNonNegative() && Res.isNonNegative() != isNonNegative(); return Res; } APInt APInt::uadd_ov(const APInt &RHS, bool &Overflow) const { - APInt Res = *this+RHS; + APInt Res = *this + RHS; Overflow = Res.ult(RHS); return Res; } @@ -1977,7 +1932,7 @@ } APInt APInt::usub_ov(const APInt &RHS, bool &Overflow) const { - APInt Res = *this-RHS; + APInt Res = *this - RHS; Overflow = Res.ugt(*this); return Res; } @@ -2120,9 +2075,9 @@ void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) { // Check our assumptions here assert(!str.empty() && "Invalid string length"); - assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || - radix == 36) && - "Radix should be 2, 8, 10, 16, or 36!"); + assert( + (radix == 10 || radix == 8 || radix == 16 || radix == 2 || radix == 36) && + "Radix should be 2, 8, 10, 16, or 36!"); StringRef::iterator p = str.begin(); size_t slen = str.size(); @@ -2133,9 +2088,10 @@ assert(slen && "String is only a sign, needs a value."); } assert((slen <= numbits || radix != 2) && "Insufficient bit width"); - assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width"); - assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width"); - assert((((slen-1)*64)/22 <= numbits || radix != 10) && + assert(((slen - 1) * 3 <= numbits || radix != 8) && "Insufficient bit width"); + assert(((slen - 1) * 4 <= numbits || radix != 16) && + "Insufficient bit width"); + assert((((slen - 1) * 64) / 22 <= numbits || radix != 10) && "Insufficient bit width"); // Allocate memory if needed @@ -2168,30 +2124,30 @@ this->negate(); } -void APInt::toString(SmallVectorImpl &Str, unsigned Radix, - bool Signed, bool formatAsCLiteral) const { - assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 || - Radix == 36) && - "Radix should be 2, 8, 10, 16, or 36!"); +void APInt::toString(SmallVectorImpl &Str, unsigned Radix, bool Signed, + bool formatAsCLiteral) const { + assert( + (Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 || Radix == 36) && + "Radix should be 2, 8, 10, 16, or 36!"); const char *Prefix = ""; if (formatAsCLiteral) { switch (Radix) { - case 2: - // Binary literals are a non-standard extension added in gcc 4.3: - // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Binary-constants.html - Prefix = "0b"; - break; - case 8: - Prefix = "0"; - break; - case 10: - break; // No prefix - case 16: - Prefix = "0x"; - break; - default: - llvm_unreachable("Invalid radix!"); + case 2: + // Binary literals are a non-standard extension added in gcc 4.3: + // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Binary-constants.html + Prefix = "0b"; + break; + case 8: + Prefix = "0"; + break; + case 10: + break; // No prefix + case 16: + Prefix = "0x"; + break; + default: + llvm_unreachable("Invalid radix!"); } } @@ -2278,7 +2234,7 @@ } // Reverse the digits before returning. - std::reverse(Str.begin()+StartDig, Str.end()); + std::reverse(Str.begin() + StartDig, Str.end()); } #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) @@ -2286,14 +2242,13 @@ SmallString<40> S, U; this->toStringUnsigned(U); this->toStringSigned(S); - dbgs() << "APInt(" << BitWidth << "b, " - << U << "u " << S << "s)\n"; + dbgs() << "APInt(" << BitWidth << "b, " << U << "u " << S << "s)\n"; } #endif void APInt::print(raw_ostream &OS, bool isSigned) const { SmallString<40> S; - this->toString(S, 10, isSigned, /* formatAsCLiteral = */false); + this->toString(S, 10, isSigned, /* formatAsCLiteral = */ false); OS << S; } @@ -2312,7 +2267,7 @@ static inline APInt::WordType lowBitMask(unsigned bits) { assert(bits != 0 && bits <= APInt::APINT_BITS_PER_WORD); - return ~(APInt::WordType) 0 >> (APInt::APINT_BITS_PER_WORD - bits); + return ~(APInt::WordType)0 >> (APInt::APINT_BITS_PER_WORD - bits); } /* Returns the value of the lower half of PART. */ @@ -2411,29 +2366,28 @@ srcLSB, to DST, of dstCOUNT parts, such that the bit srcLSB becomes the least significant bit of DST. All high bits above srcBITS in DST are zero-filled. */ -void -APInt::tcExtract(WordType *dst, unsigned dstCount, const WordType *src, - unsigned srcBits, unsigned srcLSB) { +void APInt::tcExtract(WordType *dst, unsigned dstCount, const WordType *src, + unsigned srcBits, unsigned srcLSB) { unsigned dstParts = (srcBits + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD; assert(dstParts <= dstCount); unsigned firstSrcPart = srcLSB / APINT_BITS_PER_WORD; - tcAssign (dst, src + firstSrcPart, dstParts); + tcAssign(dst, src + firstSrcPart, dstParts); unsigned shift = srcLSB % APINT_BITS_PER_WORD; - tcShiftRight (dst, dstParts, shift); + tcShiftRight(dst, dstParts, shift); /* We now have (dstParts * APINT_BITS_PER_WORD - shift) bits from SRC in DST. If this is less that srcBits, append the rest, else clear the high bits. */ unsigned n = dstParts * APINT_BITS_PER_WORD - shift; if (n < srcBits) { - WordType mask = lowBitMask (srcBits - n); - dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask) - << n % APINT_BITS_PER_WORD); + WordType mask = lowBitMask(srcBits - n); + dst[dstParts - 1] |= + ((src[firstSrcPart + dstParts] & mask) << n % APINT_BITS_PER_WORD); } else if (n > srcBits) { if (srcBits % APINT_BITS_PER_WORD) - dst[dstParts - 1] &= lowBitMask (srcBits % APINT_BITS_PER_WORD); + dst[dstParts - 1] &= lowBitMask(srcBits % APINT_BITS_PER_WORD); } /* Clear high parts. */ @@ -2442,8 +2396,8 @@ } /* DST += RHS + C where C is zero or one. Returns the carry flag. */ -APInt::WordType APInt::tcAdd(WordType *dst, const WordType *rhs, - WordType c, unsigned parts) { +APInt::WordType APInt::tcAdd(WordType *dst, const WordType *rhs, WordType c, + unsigned parts) { assert(c <= 1); for (unsigned i = 0; i < parts; i++) { @@ -2464,13 +2418,12 @@ /// "word" integer array, dst[]. dst[] is modified to reflect the addition and /// 1 is returned if there is a carry out, otherwise 0 is returned. /// @returns the carry of the addition. -APInt::WordType APInt::tcAddPart(WordType *dst, WordType src, - unsigned parts) { +APInt::WordType APInt::tcAddPart(WordType *dst, WordType src, unsigned parts) { for (unsigned i = 0; i < parts; ++i) { dst[i] += src; if (dst[i] >= src) return 0; // No need to carry so exit early. - src = 1; // Carry one to next digit. + src = 1; // Carry one to next digit. } return 1; @@ -2509,7 +2462,7 @@ dst[i] -= src; if (src <= Dst) return 0; // No need to borrow so exit early. - src = 1; // We have to "borrow 1" from next "word" + src = 1; // We have to "borrow 1" from next "word" } return 1; @@ -2534,8 +2487,7 @@ return one. */ int APInt::tcMultiplyPart(WordType *dst, const WordType *src, WordType multiplier, WordType carry, - unsigned srcParts, unsigned dstParts, - bool add) { + unsigned srcParts, unsigned dstParts, bool add) { /* Otherwise our writes of DST kill our later reads of SRC. */ assert(dst <= src || dst >= src + srcParts); assert(dstParts <= srcParts + 1); @@ -2546,13 +2498,13 @@ for (unsigned i = 0; i < n; i++) { WordType low, mid, high, srcPart; - /* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY. + /* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY. - This cannot overflow, because + This cannot overflow, because - (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1) + (n - 1) * (n - 1) + 2 (n - 1) = (n - 1) * (n + 1) - which is less than n^2. */ + which is less than n^2. */ srcPart = src[i]; @@ -2621,16 +2573,15 @@ is filled with the least significant parts of the result. Returns one if overflow occurred, otherwise zero. DST must be disjoint from both operands. */ -int APInt::tcMultiply(WordType *dst, const WordType *lhs, - const WordType *rhs, unsigned parts) { +int APInt::tcMultiply(WordType *dst, const WordType *lhs, const WordType *rhs, + unsigned parts) { assert(dst != lhs && dst != rhs); int overflow = 0; tcSet(dst, 0, parts); for (unsigned i = 0; i < parts; i++) - overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts, - parts - i, true); + overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts, parts - i, true); return overflow; } @@ -2642,7 +2593,7 @@ unsigned rhsParts) { /* Put the narrower number on the LHS for less loops below. */ if (lhsParts > rhsParts) - return tcFullMultiply (dst, rhs, lhs, rhsParts, lhsParts); + return tcFullMultiply(dst, rhs, lhs, rhsParts, lhsParts); assert(dst != lhs && dst != rhs); @@ -2662,9 +2613,8 @@ use by the routine; its contents need not be initialized and are destroyed. LHS, REMAINDER and SCRATCH must be distinct. */ -int APInt::tcDivide(WordType *lhs, const WordType *rhs, - WordType *remainder, WordType *srhs, - unsigned parts) { +int APInt::tcDivide(WordType *lhs, const WordType *rhs, WordType *remainder, + WordType *srhs, unsigned parts) { assert(lhs != remainder && lhs != srhs && remainder != srhs); unsigned shiftCount = tcMSB(rhs, parts) + 1; @@ -2673,7 +2623,7 @@ shiftCount = parts * APINT_BITS_PER_WORD - shiftCount; unsigned n = shiftCount / APINT_BITS_PER_WORD; - WordType mask = (WordType) 1 << (shiftCount % APINT_BITS_PER_WORD); + WordType mask = (WordType)1 << (shiftCount % APINT_BITS_PER_WORD); tcAssign(srhs, rhs, parts); tcShiftLeft(srhs, parts, shiftCount); @@ -2694,7 +2644,7 @@ shiftCount--; tcShiftRight(srhs, parts, 1); if ((mask >>= 1) == 0) { - mask = (WordType) 1 << (APINT_BITS_PER_WORD - 1); + mask = (WordType)1 << (APINT_BITS_PER_WORD - 1); n--; } } @@ -2721,7 +2671,7 @@ Dst[Words] = Dst[Words - WordShift] << BitShift; if (Words > WordShift) Dst[Words] |= - Dst[Words - WordShift - 1] >> (APINT_BITS_PER_WORD - BitShift); + Dst[Words - WordShift - 1] >> (APINT_BITS_PER_WORD - BitShift); } } @@ -2756,33 +2706,8 @@ std::memset(Dst + WordsToMove, 0, WordShift * APINT_WORD_SIZE); } -/* Bitwise and of two bignums. */ -void APInt::tcAnd(WordType *dst, const WordType *rhs, unsigned parts) { - for (unsigned i = 0; i < parts; i++) - dst[i] &= rhs[i]; -} - -/* Bitwise inclusive or of two bignums. */ -void APInt::tcOr(WordType *dst, const WordType *rhs, unsigned parts) { - for (unsigned i = 0; i < parts; i++) - dst[i] |= rhs[i]; -} - -/* Bitwise exclusive or of two bignums. */ -void APInt::tcXor(WordType *dst, const WordType *rhs, unsigned parts) { - for (unsigned i = 0; i < parts; i++) - dst[i] ^= rhs[i]; -} - -/* Complement a bignum in-place. */ -void APInt::tcComplement(WordType *dst, unsigned parts) { - for (unsigned i = 0; i < parts; i++) - dst[i] = ~dst[i]; -} - /* Comparison (unsigned) of two bignums. */ -int APInt::tcCompare(const WordType *lhs, const WordType *rhs, - unsigned parts) { +int APInt::tcCompare(const WordType *lhs, const WordType *rhs, unsigned parts) { while (parts) { parts--; if (lhs[parts] != rhs[parts]) @@ -2792,23 +2717,6 @@ return 0; } -/* Set the least significant BITS bits of a bignum, clear the - rest. */ -void APInt::tcSetLeastSignificantBits(WordType *dst, unsigned parts, - unsigned bits) { - unsigned i = 0; - while (bits > APINT_BITS_PER_WORD) { - dst[i++] = ~(WordType) 0; - bits -= APINT_BITS_PER_WORD; - } - - if (bits) - dst[i++] = ~(WordType) 0 >> (APINT_BITS_PER_WORD - bits); - - while (i < parts) - dst[i++] = 0; -} - APInt llvm::APIntOps::RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM) { // Currently udivrem always rounds down. @@ -2866,11 +2774,11 @@ "Value range width should be less than coefficient width"); assert(RangeWidth > 1 && "Value range bit width should be > 1"); - LLVM_DEBUG(dbgs() << __func__ << ": solving " << A << "x^2 + " << B - << "x + " << C << ", rw:" << RangeWidth << '\n'); + LLVM_DEBUG(dbgs() << __func__ << ": solving " << A << "x^2 + " << B << "x + " + << C << ", rw:" << RangeWidth << '\n'); // Identify 0 as a (non)solution immediately. - if (C.sextOrTrunc(RangeWidth).isNullValue() ) { + if (C.sextOrTrunc(RangeWidth).isNullValue()) { LLVM_DEBUG(dbgs() << __func__ << ": zero solution\n"); return APInt(CoeffWidth, 0); } @@ -2929,12 +2837,12 @@ APInt SqrB = B * B; bool PickLow; - auto RoundUp = [] (const APInt &V, const APInt &A) -> APInt { + auto RoundUp = [](const APInt &V, const APInt &A) -> APInt { assert(A.isStrictlyPositive()); APInt T = V.abs().urem(A); if (T.isNullValue()) return V; - return V.isNegative() ? V+T : V+(A-T); + return V.isNegative() ? V + T : V + (A - T); }; // The vertex of the parabola is at -B/2A, but since A > 0, it's negative @@ -2954,7 +2862,7 @@ // to exist, the discriminant must be non-negative. This means that // C-kR <= B^2/4A is a necessary condition for k, i.e. there is a // lower bound on values of k: kR >= C - B^2/4A. - APInt LowkR = C - SqrB.udiv(2*TwoA); // udiv because all values > 0. + APInt LowkR = C - SqrB.udiv(2 * TwoA); // udiv because all values > 0. // Round LowkR up (towards +inf) to the nearest kR. LowkR = RoundUp(LowkR, R); @@ -2966,7 +2874,7 @@ if (C.sgt(LowkR)) { // If LowkR < C, then such a k is guaranteed to exist because // LowkR itself is a multiple of R. - C -= -RoundUp(-C, R); // C = C - RoundDown(C, R) + C -= -RoundUp(-C, R); // C = C - RoundDown(C, R) // Pick the smaller solution. PickLow = true; } else { @@ -2986,7 +2894,7 @@ LLVM_DEBUG(dbgs() << __func__ << ": updated coefficients " << A << "x^2 + " << B << "x + " << C << ", rw:" << RangeWidth << '\n'); - APInt D = SqrB - 4*A*C; + APInt D = SqrB - 4 * A * C; assert(D.isNonNegative() && "Negative discriminant"); APInt SQ = D.sqrt(); @@ -3007,7 +2915,7 @@ // one, subtract SQ+1 when calculating the low root (for inexact value // of SQ). if (PickLow) - APInt::sdivrem(-B - (SQ+InexactSQ), TwoA, X, Rem); + APInt::sdivrem(-B - (SQ + InexactSQ), TwoA, X, Rem); else APInt::sdivrem(-B + SQ, TwoA, X, Rem); @@ -3021,7 +2929,7 @@ return X; } - assert((SQ*SQ).sle(D) && "SQ = |_sqrt(D)_|, so SQ*SQ <= D"); + assert((SQ * SQ).sle(D) && "SQ = |_sqrt(D)_|, so SQ*SQ <= D"); // The exact value of the square root of D should be between SQ and SQ+1. // This implies that the solution should be between that corresponding to // SQ (i.e. X) and that corresponding to SQ+1. @@ -3030,8 +2938,8 @@ // Actually it must be strictly less than the exact solution, while // X+1 will be greater than or equal to it. - APInt VX = (A*X + B)*X + C; - APInt VY = VX + TwoA*X + A + B; + APInt VX = (A * X + B) * X + C; + APInt VY = VX + TwoA * X + A + B; bool SignChange = VX.isNegative() != VY.isNegative() || VX.isNullValue() != VY.isNullValue(); // If the sign did not change between X and X+1, X is not a valid solution. @@ -3059,7 +2967,7 @@ /// with the integer held in IntVal. void llvm::StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, unsigned StoreBytes) { - assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!"); + assert((IntVal.getBitWidth() + 7) / 8 >= StoreBytes && "Integer too small!"); const uint8_t *Src = (const uint8_t *)IntVal.getRawData(); if (sys::IsLittleEndianHost) { @@ -3085,9 +2993,9 @@ /// from Src into IntVal, which is assumed to be wide enough and to hold zero. void llvm::LoadIntFromMemory(APInt &IntVal, const uint8_t *Src, unsigned LoadBytes) { - assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!"); - uint8_t *Dst = reinterpret_cast( - const_cast(IntVal.getRawData())); + assert((IntVal.getBitWidth() + 7) / 8 >= LoadBytes && "Integer too small!"); + uint8_t *Dst = + reinterpret_cast(const_cast(IntVal.getRawData())); if (sys::IsLittleEndianHost) // Little-endian host - the destination must be ordered from LSB to MSB. diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -60,7 +60,7 @@ EXPECT_EQ(1u, i33minus2.countTrailingZeros()); EXPECT_EQ(32u, i33minus2.countPopulation()); EXPECT_EQ(-2, i33minus2.getSExtValue()); - EXPECT_EQ(((uint64_t)-2)&((1ull<<33) -1), i33minus2.getZExtValue()); + EXPECT_EQ(((uint64_t)-2) & ((1ull << 33) - 1), i33minus2.getZExtValue()); } TEST(APIntTest, i61_Count) { @@ -320,40 +320,40 @@ // sdivrem { - APInt q(8, 0); - APInt r(8, 0); - APInt one(8, 1); - APInt two(8, 2); - APInt nine(8, 9); - APInt four(8, 4); - - EXPECT_EQ(nine.srem(two), one); - EXPECT_EQ(nine.srem(-two), one); - EXPECT_EQ((-nine).srem(two), -one); - EXPECT_EQ((-nine).srem(-two), -one); - - APInt::sdivrem(nine, two, q, r); - EXPECT_EQ(four, q); - EXPECT_EQ(one, r); - APInt::sdivrem(-nine, two, q, r); - EXPECT_EQ(-four, q); - EXPECT_EQ(-one, r); - APInt::sdivrem(nine, -two, q, r); - EXPECT_EQ(-four, q); - EXPECT_EQ(one, r); - APInt::sdivrem(-nine, -two, q, r); - EXPECT_EQ(four, q); - EXPECT_EQ(-one, r); + APInt q(8, 0); + APInt r(8, 0); + APInt one(8, 1); + APInt two(8, 2); + APInt nine(8, 9); + APInt four(8, 4); + + EXPECT_EQ(nine.srem(two), one); + EXPECT_EQ(nine.srem(-two), one); + EXPECT_EQ((-nine).srem(two), -one); + EXPECT_EQ((-nine).srem(-two), -one); + + APInt::sdivrem(nine, two, q, r); + EXPECT_EQ(four, q); + EXPECT_EQ(one, r); + APInt::sdivrem(-nine, two, q, r); + EXPECT_EQ(-four, q); + EXPECT_EQ(-one, r); + APInt::sdivrem(nine, -two, q, r); + EXPECT_EQ(-four, q); + EXPECT_EQ(one, r); + APInt::sdivrem(-nine, -two, q, r); + EXPECT_EQ(four, q); + EXPECT_EQ(-one, r); } } TEST(APIntTest, compare) { std::array testVals{{ - APInt{16, 2}, - APInt{16, 1}, - APInt{16, 0}, - APInt{16, (uint64_t)-1, true}, - APInt{16, (uint64_t)-2, true}, + APInt{16, 2}, + APInt{16, 1}, + APInt{16, 0}, + APInt{16, (uint64_t)-1, true}, + APInt{16, (uint64_t)-2, true}, }}; for (auto &arg1 : testVals) @@ -363,24 +363,24 @@ auto sv1 = arg1.getSExtValue(); auto sv2 = arg2.getSExtValue(); - EXPECT_EQ(uv1 < uv2, arg1.ult(arg2)); + EXPECT_EQ(uv1 < uv2, arg1.ult(arg2)); EXPECT_EQ(uv1 <= uv2, arg1.ule(arg2)); - EXPECT_EQ(uv1 > uv2, arg1.ugt(arg2)); + EXPECT_EQ(uv1 > uv2, arg1.ugt(arg2)); EXPECT_EQ(uv1 >= uv2, arg1.uge(arg2)); - EXPECT_EQ(sv1 < sv2, arg1.slt(arg2)); + EXPECT_EQ(sv1 < sv2, arg1.slt(arg2)); EXPECT_EQ(sv1 <= sv2, arg1.sle(arg2)); - EXPECT_EQ(sv1 > sv2, arg1.sgt(arg2)); + EXPECT_EQ(sv1 > sv2, arg1.sgt(arg2)); EXPECT_EQ(sv1 >= sv2, arg1.sge(arg2)); - EXPECT_EQ(uv1 < uv2, arg1.ult(uv2)); + EXPECT_EQ(uv1 < uv2, arg1.ult(uv2)); EXPECT_EQ(uv1 <= uv2, arg1.ule(uv2)); - EXPECT_EQ(uv1 > uv2, arg1.ugt(uv2)); + EXPECT_EQ(uv1 > uv2, arg1.ugt(uv2)); EXPECT_EQ(uv1 >= uv2, arg1.uge(uv2)); - EXPECT_EQ(sv1 < sv2, arg1.slt(sv2)); + EXPECT_EQ(sv1 < sv2, arg1.slt(sv2)); EXPECT_EQ(sv1 <= sv2, arg1.sle(sv2)); - EXPECT_EQ(sv1 > sv2, arg1.sgt(sv2)); + EXPECT_EQ(sv1 > sv2, arg1.sgt(sv2)); EXPECT_EQ(sv1 >= sv2, arg1.sge(sv2)); } } @@ -388,35 +388,35 @@ TEST(APIntTest, compareWithRawIntegers) { EXPECT_TRUE(!APInt(8, 1).uge(256)); EXPECT_TRUE(!APInt(8, 1).ugt(256)); - EXPECT_TRUE( APInt(8, 1).ule(256)); - EXPECT_TRUE( APInt(8, 1).ult(256)); + EXPECT_TRUE(APInt(8, 1).ule(256)); + EXPECT_TRUE(APInt(8, 1).ult(256)); EXPECT_TRUE(!APInt(8, 1).sge(256)); EXPECT_TRUE(!APInt(8, 1).sgt(256)); - EXPECT_TRUE( APInt(8, 1).sle(256)); - EXPECT_TRUE( APInt(8, 1).slt(256)); + EXPECT_TRUE(APInt(8, 1).sle(256)); + EXPECT_TRUE(APInt(8, 1).slt(256)); EXPECT_TRUE(!(APInt(8, 0) == 256)); - EXPECT_TRUE( APInt(8, 0) != 256); + EXPECT_TRUE(APInt(8, 0) != 256); EXPECT_TRUE(!(APInt(8, 1) == 256)); - EXPECT_TRUE( APInt(8, 1) != 256); + EXPECT_TRUE(APInt(8, 1) != 256); auto uint64max = UINT64_MAX; - auto int64max = INT64_MAX; - auto int64min = INT64_MIN; + auto int64max = INT64_MAX; + auto int64min = INT64_MIN; auto u64 = APInt{128, uint64max}; auto s64 = APInt{128, static_cast(int64max), true}; auto big = u64 + 1; - EXPECT_TRUE( u64.uge(uint64max)); + EXPECT_TRUE(u64.uge(uint64max)); EXPECT_TRUE(!u64.ugt(uint64max)); - EXPECT_TRUE( u64.ule(uint64max)); + EXPECT_TRUE(u64.ule(uint64max)); EXPECT_TRUE(!u64.ult(uint64max)); - EXPECT_TRUE( u64.sge(int64max)); - EXPECT_TRUE( u64.sgt(int64max)); + EXPECT_TRUE(u64.sge(int64max)); + EXPECT_TRUE(u64.sgt(int64max)); EXPECT_TRUE(!u64.sle(int64max)); EXPECT_TRUE(!u64.slt(int64max)); - EXPECT_TRUE( u64.sge(int64min)); - EXPECT_TRUE( u64.sgt(int64min)); + EXPECT_TRUE(u64.sge(int64min)); + EXPECT_TRUE(u64.sgt(int64min)); EXPECT_TRUE(!u64.sle(int64min)); EXPECT_TRUE(!u64.slt(int64min)); @@ -426,14 +426,14 @@ EXPECT_TRUE(!s64.uge(uint64max)); EXPECT_TRUE(!s64.ugt(uint64max)); - EXPECT_TRUE( s64.ule(uint64max)); - EXPECT_TRUE( s64.ult(uint64max)); - EXPECT_TRUE( s64.sge(int64max)); + EXPECT_TRUE(s64.ule(uint64max)); + EXPECT_TRUE(s64.ult(uint64max)); + EXPECT_TRUE(s64.sge(int64max)); EXPECT_TRUE(!s64.sgt(int64max)); - EXPECT_TRUE( s64.sle(int64max)); + EXPECT_TRUE(s64.sle(int64max)); EXPECT_TRUE(!s64.slt(int64max)); - EXPECT_TRUE( s64.sge(int64min)); - EXPECT_TRUE( s64.sgt(int64min)); + EXPECT_TRUE(s64.sge(int64min)); + EXPECT_TRUE(s64.sgt(int64min)); EXPECT_TRUE(!s64.sle(int64min)); EXPECT_TRUE(!s64.slt(int64min)); @@ -441,16 +441,16 @@ EXPECT_TRUE(s64 == int64max); EXPECT_TRUE(s64 != int64min); - EXPECT_TRUE( big.uge(uint64max)); - EXPECT_TRUE( big.ugt(uint64max)); + EXPECT_TRUE(big.uge(uint64max)); + EXPECT_TRUE(big.ugt(uint64max)); EXPECT_TRUE(!big.ule(uint64max)); EXPECT_TRUE(!big.ult(uint64max)); - EXPECT_TRUE( big.sge(int64max)); - EXPECT_TRUE( big.sgt(int64max)); + EXPECT_TRUE(big.sge(int64max)); + EXPECT_TRUE(big.sgt(int64max)); EXPECT_TRUE(!big.sle(int64max)); EXPECT_TRUE(!big.slt(int64max)); - EXPECT_TRUE( big.sge(int64min)); - EXPECT_TRUE( big.sgt(int64min)); + EXPECT_TRUE(big.sge(int64min)); + EXPECT_TRUE(big.sgt(int64min)); EXPECT_TRUE(!big.sle(int64min)); EXPECT_TRUE(!big.slt(int64min)); @@ -466,15 +466,15 @@ auto a = APInt{64, static_cast(edge), true}; EXPECT_TRUE(!a.slt(edge)); - EXPECT_TRUE( a.sle(edge)); + EXPECT_TRUE(a.sle(edge)); EXPECT_TRUE(!a.sgt(edge)); - EXPECT_TRUE( a.sge(edge)); - EXPECT_TRUE( a.slt(edgeP1)); - EXPECT_TRUE( a.sle(edgeP1)); + EXPECT_TRUE(a.sge(edge)); + EXPECT_TRUE(a.slt(edgeP1)); + EXPECT_TRUE(a.sle(edgeP1)); EXPECT_TRUE(!a.sgt(edgeP1)); EXPECT_TRUE(!a.sge(edgeP1)); - EXPECT_TRUE( a.slt(edgeM1)); - EXPECT_TRUE( a.sle(edgeM1)); + EXPECT_TRUE(a.slt(edgeM1)); + EXPECT_TRUE(a.sle(edgeM1)); EXPECT_TRUE(!a.sgt(edgeM1)); EXPECT_TRUE(!a.sge(edgeM1)); } @@ -486,30 +486,30 @@ auto a = APInt{64, edge}; EXPECT_TRUE(!a.ult(edge)); - EXPECT_TRUE( a.ule(edge)); + EXPECT_TRUE(a.ule(edge)); EXPECT_TRUE(!a.ugt(edge)); - EXPECT_TRUE( a.uge(edge)); - EXPECT_TRUE( a.ult(edgeP1)); - EXPECT_TRUE( a.ule(edgeP1)); + EXPECT_TRUE(a.uge(edge)); + EXPECT_TRUE(a.ult(edgeP1)); + EXPECT_TRUE(a.ule(edgeP1)); EXPECT_TRUE(!a.ugt(edgeP1)); EXPECT_TRUE(!a.uge(edgeP1)); EXPECT_TRUE(!a.ult(edgeM1)); EXPECT_TRUE(!a.ule(edgeM1)); - EXPECT_TRUE( a.ugt(edgeM1)); - EXPECT_TRUE( a.uge(edgeM1)); + EXPECT_TRUE(a.ugt(edgeM1)); + EXPECT_TRUE(a.uge(edgeM1)); EXPECT_TRUE(!a.slt(edge)); - EXPECT_TRUE( a.sle(edge)); + EXPECT_TRUE(a.sle(edge)); EXPECT_TRUE(!a.sgt(edge)); - EXPECT_TRUE( a.sge(edge)); - EXPECT_TRUE( a.slt(edgeP1)); - EXPECT_TRUE( a.sle(edgeP1)); + EXPECT_TRUE(a.sge(edge)); + EXPECT_TRUE(a.slt(edgeP1)); + EXPECT_TRUE(a.sle(edgeP1)); EXPECT_TRUE(!a.sgt(edgeP1)); EXPECT_TRUE(!a.sge(edgeP1)); EXPECT_TRUE(!a.slt(edgeM1)); EXPECT_TRUE(!a.sle(edgeM1)); - EXPECT_TRUE( a.sgt(edgeM1)); - EXPECT_TRUE( a.sge(edgeM1)); + EXPECT_TRUE(a.sgt(edgeM1)); + EXPECT_TRUE(a.sge(edgeM1)); } TEST(APIntTest, compareLargeIntegers) { @@ -565,13 +565,9 @@ // Multiword check. uint64_t N = 0xEB6EB136591CBA21ULL; - APInt::WordType E2[4] = { - N, - 0x7B9358BD6A33F10AULL, - 0x7E7FFA5EADD8846ULL, - 0x305F341CA00B613DULL - }; - APInt A2(APInt::APINT_BITS_PER_WORD*4, E2); + APInt::WordType E2[4] = {N, 0x7B9358BD6A33F10AULL, 0x7E7FFA5EADD8846ULL, + 0x305F341CA00B613DULL}; + APInt A2(APInt::APINT_BITS_PER_WORD * 4, E2); EXPECT_EQ(A2 & N, N); EXPECT_EQ(A2 & 0, 0); @@ -730,8 +726,8 @@ EXPECT_EQ(SubLR, Two); EXPECT_EQ(SubLR.getRawData(), RawDataR); - APInt SubRL = getRValue("100000000000000000000000000000001", RawDataL) - - AllOnes; + APInt SubRL = + getRValue("100000000000000000000000000000001", RawDataL) - AllOnes; EXPECT_EQ(SubRL, Two); EXPECT_EQ(SubRL.getRawData(), RawDataL); @@ -948,43 +944,43 @@ TEST(APIntTest, divrem_big1) { // Tests KnuthDiv rare step D6 - testDiv({256, "1ffffffffffffffff", 16}, - {256, "1ffffffffffffffff", 16}, + testDiv({256, "1ffffffffffffffff", 16}, {256, "1ffffffffffffffff", 16}, {256, 0}); } TEST(APIntTest, divrem_big2) { // Tests KnuthDiv rare step D6 - testDiv({1024, "112233ceff" - "cecece000000ffffffffffffffffffff" - "ffffffffffffffffffffffffffffffff" - "ffffffffffffffffffffffffffffffff" - "ffffffffffffffffffffffffffffff33", 16}, - {1024, "111111ffffffffffffffff" - "ffffffffffffffffffffffffffffffff" - "fffffffffffffffffffffffffffffccf" - "ffffffffffffffffffffffffffffff00", 16}, + testDiv({1024, + "112233ceff" + "cecece000000ffffffffffffffffffff" + "ffffffffffffffffffffffffffffffff" + "ffffffffffffffffffffffffffffffff" + "ffffffffffffffffffffffffffffff33", + 16}, + {1024, + "111111ffffffffffffffff" + "ffffffffffffffffffffffffffffffff" + "fffffffffffffffffffffffffffffccf" + "ffffffffffffffffffffffffffffff00", + 16}, {1024, 7919}); } TEST(APIntTest, divrem_big3) { // Tests KnuthDiv case without shift testDiv({256, "80000001ffffffffffffffff", 16}, - {256, "ffffffffffffff0000000", 16}, - {256, 4219}); + {256, "ffffffffffffff0000000", 16}, {256, 4219}); } TEST(APIntTest, divrem_big4) { // Tests heap allocation in divide() enfoced by huge numbers - testDiv(APInt{4096, 5}.shl(2001), - APInt{4096, 1}.shl(2000), - APInt{4096, 4219*13}); + testDiv(APInt{4096, 5}.shl(2001), APInt{4096, 1}.shl(2000), + APInt{4096, 4219 * 13}); } TEST(APIntTest, divrem_big5) { // Tests one word divisor case of divide() - testDiv(APInt{1024, 19}.shl(811), - APInt{1024, 4356013}, // one word + testDiv(APInt{1024, 19}.shl(811), APInt{1024, 4356013}, // one word APInt{1024, 1}); } @@ -997,8 +993,7 @@ TEST(APIntTest, divrem_big7) { // Yet another test for KnuthDiv rare step D6. - testDiv({224, "800000008000000200000005", 16}, - {224, "fffffffd", 16}, + testDiv({224, "800000008000000200000005", 16}, {224, "fffffffd", 16}, {224, "80000000800000010000000f", 16}); } @@ -1035,24 +1030,16 @@ TEST(APIntTest, divremuint) { // Single word APInt - testDiv(APInt{64, 9}, - 2, - APInt{64, 1}); + testDiv(APInt{64, 9}, 2, APInt{64, 1}); // Single word negative APInt - testDiv(-APInt{64, 9}, - 2, - -APInt{64, 1}); + testDiv(-APInt{64, 9}, 2, -APInt{64, 1}); // Multiword dividend with only one significant word. - testDiv(APInt{256, 9}, - 2, - APInt{256, 1}); + testDiv(APInt{256, 9}, 2, APInt{256, 1}); // Negative dividend. - testDiv(-APInt{256, 9}, - 2, - -APInt{256, 1}); + testDiv(-APInt{256, 9}, 2, -APInt{256, 1}); // Multiword dividend testDiv(APInt{1024, 19}.shl(811), @@ -1093,83 +1080,83 @@ } TEST(APIntTest, fromString) { - EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2)); - EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2)); - EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2)); - EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2)); + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 2)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 2)); + EXPECT_EQ(APInt(32, 2), APInt(32, "10", 2)); + EXPECT_EQ(APInt(32, 3), APInt(32, "11", 2)); EXPECT_EQ(APInt(32, 4), APInt(32, "100", 2)); - EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2)); - EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2)); - EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2)); - EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2)); + EXPECT_EQ(APInt(32, 0), APInt(32, "+0", 2)); + EXPECT_EQ(APInt(32, 1), APInt(32, "+1", 2)); + EXPECT_EQ(APInt(32, 2), APInt(32, "+10", 2)); + EXPECT_EQ(APInt(32, 3), APInt(32, "+11", 2)); EXPECT_EQ(APInt(32, 4), APInt(32, "+100", 2)); - EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2)); - EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2)); - EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2)); - EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2)); + EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 2)); + EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 2)); + EXPECT_EQ(APInt(32, uint64_t(-2LL)), APInt(32, "-10", 2)); + EXPECT_EQ(APInt(32, uint64_t(-3LL)), APInt(32, "-11", 2)); EXPECT_EQ(APInt(32, uint64_t(-4LL)), APInt(32, "-100", 2)); - EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8)); - EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8)); - EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8)); - EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8)); - EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8)); - EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8)); - - EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8)); - EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8)); - EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8)); - EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8)); - EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8)); - EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8)); - - EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8)); - EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8)); - EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8)); - EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8)); - EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8)); - EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8)); - - EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10)); - EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10)); - EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10)); + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 8)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 8)); + EXPECT_EQ(APInt(32, 7), APInt(32, "7", 8)); + EXPECT_EQ(APInt(32, 8), APInt(32, "10", 8)); + EXPECT_EQ(APInt(32, 15), APInt(32, "17", 8)); + EXPECT_EQ(APInt(32, 16), APInt(32, "20", 8)); + + EXPECT_EQ(APInt(32, +0), APInt(32, "+0", 8)); + EXPECT_EQ(APInt(32, +1), APInt(32, "+1", 8)); + EXPECT_EQ(APInt(32, +7), APInt(32, "+7", 8)); + EXPECT_EQ(APInt(32, +8), APInt(32, "+10", 8)); + EXPECT_EQ(APInt(32, +15), APInt(32, "+17", 8)); + EXPECT_EQ(APInt(32, +16), APInt(32, "+20", 8)); + + EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 8)); + EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 8)); + EXPECT_EQ(APInt(32, uint64_t(-7LL)), APInt(32, "-7", 8)); + EXPECT_EQ(APInt(32, uint64_t(-8LL)), APInt(32, "-10", 8)); + EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-17", 8)); + EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-20", 8)); + + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 10)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 10)); + EXPECT_EQ(APInt(32, 9), APInt(32, "9", 10)); EXPECT_EQ(APInt(32, 10), APInt(32, "10", 10)); EXPECT_EQ(APInt(32, 19), APInt(32, "19", 10)); EXPECT_EQ(APInt(32, 20), APInt(32, "20", 10)); - EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10)); - EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10)); - EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10)); + EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 10)); + EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 10)); + EXPECT_EQ(APInt(32, uint64_t(-9LL)), APInt(32, "-9", 10)); EXPECT_EQ(APInt(32, uint64_t(-10LL)), APInt(32, "-10", 10)); EXPECT_EQ(APInt(32, uint64_t(-19LL)), APInt(32, "-19", 10)); EXPECT_EQ(APInt(32, uint64_t(-20LL)), APInt(32, "-20", 10)); - EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16)); - EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16)); - EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16)); + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 16)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 16)); + EXPECT_EQ(APInt(32, 15), APInt(32, "F", 16)); EXPECT_EQ(APInt(32, 16), APInt(32, "10", 16)); EXPECT_EQ(APInt(32, 31), APInt(32, "1F", 16)); EXPECT_EQ(APInt(32, 32), APInt(32, "20", 16)); - EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16)); - EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16)); - EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16)); + EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 16)); + EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 16)); + EXPECT_EQ(APInt(32, uint64_t(-15LL)), APInt(32, "-F", 16)); EXPECT_EQ(APInt(32, uint64_t(-16LL)), APInt(32, "-10", 16)); EXPECT_EQ(APInt(32, uint64_t(-31LL)), APInt(32, "-1F", 16)); EXPECT_EQ(APInt(32, uint64_t(-32LL)), APInt(32, "-20", 16)); - EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36)); - EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36)); - EXPECT_EQ(APInt(32, 35), APInt(32, "Z", 36)); + EXPECT_EQ(APInt(32, 0), APInt(32, "0", 36)); + EXPECT_EQ(APInt(32, 1), APInt(32, "1", 36)); + EXPECT_EQ(APInt(32, 35), APInt(32, "Z", 36)); EXPECT_EQ(APInt(32, 36), APInt(32, "10", 36)); EXPECT_EQ(APInt(32, 71), APInt(32, "1Z", 36)); EXPECT_EQ(APInt(32, 72), APInt(32, "20", 36)); - EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36)); - EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36)); - EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36)); + EXPECT_EQ(APInt(32, uint64_t(-0LL)), APInt(32, "-0", 36)); + EXPECT_EQ(APInt(32, uint64_t(-1LL)), APInt(32, "-1", 36)); + EXPECT_EQ(APInt(32, uint64_t(-35LL)), APInt(32, "-Z", 36)); EXPECT_EQ(APInt(32, uint64_t(-36LL)), APInt(32, "-10", 36)); EXPECT_EQ(APInt(32, uint64_t(-71LL)), APInt(32, "-1Z", 36)); EXPECT_EQ(APInt(32, uint64_t(-72LL)), APInt(32, "-20", 36)); @@ -1256,61 +1243,61 @@ } TEST(APIntTest, StringBitsNeeded2) { - EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 2)); - EXPECT_EQ(1U, APInt::getBitsNeeded( "1", 2)); - EXPECT_EQ(2U, APInt::getBitsNeeded( "10", 2)); - EXPECT_EQ(2U, APInt::getBitsNeeded( "11", 2)); + EXPECT_EQ(1U, APInt::getBitsNeeded("0", 2)); + EXPECT_EQ(1U, APInt::getBitsNeeded("1", 2)); + EXPECT_EQ(2U, APInt::getBitsNeeded("10", 2)); + EXPECT_EQ(2U, APInt::getBitsNeeded("11", 2)); EXPECT_EQ(3U, APInt::getBitsNeeded("100", 2)); - EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 2)); - EXPECT_EQ(1U, APInt::getBitsNeeded( "+1", 2)); - EXPECT_EQ(2U, APInt::getBitsNeeded( "+10", 2)); - EXPECT_EQ(2U, APInt::getBitsNeeded( "+11", 2)); + EXPECT_EQ(1U, APInt::getBitsNeeded("+0", 2)); + EXPECT_EQ(1U, APInt::getBitsNeeded("+1", 2)); + EXPECT_EQ(2U, APInt::getBitsNeeded("+10", 2)); + EXPECT_EQ(2U, APInt::getBitsNeeded("+11", 2)); EXPECT_EQ(3U, APInt::getBitsNeeded("+100", 2)); - EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 2)); - EXPECT_EQ(2U, APInt::getBitsNeeded( "-1", 2)); - EXPECT_EQ(3U, APInt::getBitsNeeded( "-10", 2)); - EXPECT_EQ(3U, APInt::getBitsNeeded( "-11", 2)); + EXPECT_EQ(2U, APInt::getBitsNeeded("-0", 2)); + EXPECT_EQ(2U, APInt::getBitsNeeded("-1", 2)); + EXPECT_EQ(3U, APInt::getBitsNeeded("-10", 2)); + EXPECT_EQ(3U, APInt::getBitsNeeded("-11", 2)); EXPECT_EQ(4U, APInt::getBitsNeeded("-100", 2)); } TEST(APIntTest, StringBitsNeeded8) { - EXPECT_EQ(3U, APInt::getBitsNeeded( "0", 8)); - EXPECT_EQ(3U, APInt::getBitsNeeded( "7", 8)); + EXPECT_EQ(3U, APInt::getBitsNeeded("0", 8)); + EXPECT_EQ(3U, APInt::getBitsNeeded("7", 8)); EXPECT_EQ(6U, APInt::getBitsNeeded("10", 8)); EXPECT_EQ(6U, APInt::getBitsNeeded("17", 8)); EXPECT_EQ(6U, APInt::getBitsNeeded("20", 8)); - EXPECT_EQ(3U, APInt::getBitsNeeded( "+0", 8)); - EXPECT_EQ(3U, APInt::getBitsNeeded( "+7", 8)); + EXPECT_EQ(3U, APInt::getBitsNeeded("+0", 8)); + EXPECT_EQ(3U, APInt::getBitsNeeded("+7", 8)); EXPECT_EQ(6U, APInt::getBitsNeeded("+10", 8)); EXPECT_EQ(6U, APInt::getBitsNeeded("+17", 8)); EXPECT_EQ(6U, APInt::getBitsNeeded("+20", 8)); - EXPECT_EQ(4U, APInt::getBitsNeeded( "-0", 8)); - EXPECT_EQ(4U, APInt::getBitsNeeded( "-7", 8)); + EXPECT_EQ(4U, APInt::getBitsNeeded("-0", 8)); + EXPECT_EQ(4U, APInt::getBitsNeeded("-7", 8)); EXPECT_EQ(7U, APInt::getBitsNeeded("-10", 8)); EXPECT_EQ(7U, APInt::getBitsNeeded("-17", 8)); EXPECT_EQ(7U, APInt::getBitsNeeded("-20", 8)); } TEST(APIntTest, StringBitsNeeded10) { - EXPECT_EQ(1U, APInt::getBitsNeeded( "0", 10)); - EXPECT_EQ(2U, APInt::getBitsNeeded( "3", 10)); - EXPECT_EQ(4U, APInt::getBitsNeeded( "9", 10)); + EXPECT_EQ(1U, APInt::getBitsNeeded("0", 10)); + EXPECT_EQ(2U, APInt::getBitsNeeded("3", 10)); + EXPECT_EQ(4U, APInt::getBitsNeeded("9", 10)); EXPECT_EQ(4U, APInt::getBitsNeeded("10", 10)); EXPECT_EQ(5U, APInt::getBitsNeeded("19", 10)); EXPECT_EQ(5U, APInt::getBitsNeeded("20", 10)); - EXPECT_EQ(1U, APInt::getBitsNeeded( "+0", 10)); - EXPECT_EQ(4U, APInt::getBitsNeeded( "+9", 10)); + EXPECT_EQ(1U, APInt::getBitsNeeded("+0", 10)); + EXPECT_EQ(4U, APInt::getBitsNeeded("+9", 10)); EXPECT_EQ(4U, APInt::getBitsNeeded("+10", 10)); EXPECT_EQ(5U, APInt::getBitsNeeded("+19", 10)); EXPECT_EQ(5U, APInt::getBitsNeeded("+20", 10)); - EXPECT_EQ(2U, APInt::getBitsNeeded( "-0", 10)); - EXPECT_EQ(5U, APInt::getBitsNeeded( "-9", 10)); + EXPECT_EQ(2U, APInt::getBitsNeeded("-0", 10)); + EXPECT_EQ(5U, APInt::getBitsNeeded("-9", 10)); EXPECT_EQ(5U, APInt::getBitsNeeded("-10", 10)); EXPECT_EQ(6U, APInt::getBitsNeeded("-19", 10)); EXPECT_EQ(6U, APInt::getBitsNeeded("-20", 10)); @@ -1333,20 +1320,20 @@ } TEST(APIntTest, StringBitsNeeded16) { - EXPECT_EQ(4U, APInt::getBitsNeeded( "0", 16)); - EXPECT_EQ(4U, APInt::getBitsNeeded( "F", 16)); + EXPECT_EQ(4U, APInt::getBitsNeeded("0", 16)); + EXPECT_EQ(4U, APInt::getBitsNeeded("F", 16)); EXPECT_EQ(8U, APInt::getBitsNeeded("10", 16)); EXPECT_EQ(8U, APInt::getBitsNeeded("1F", 16)); EXPECT_EQ(8U, APInt::getBitsNeeded("20", 16)); - EXPECT_EQ(4U, APInt::getBitsNeeded( "+0", 16)); - EXPECT_EQ(4U, APInt::getBitsNeeded( "+F", 16)); + EXPECT_EQ(4U, APInt::getBitsNeeded("+0", 16)); + EXPECT_EQ(4U, APInt::getBitsNeeded("+F", 16)); EXPECT_EQ(8U, APInt::getBitsNeeded("+10", 16)); EXPECT_EQ(8U, APInt::getBitsNeeded("+1F", 16)); EXPECT_EQ(8U, APInt::getBitsNeeded("+20", 16)); - EXPECT_EQ(5U, APInt::getBitsNeeded( "-0", 16)); - EXPECT_EQ(5U, APInt::getBitsNeeded( "-F", 16)); + EXPECT_EQ(5U, APInt::getBitsNeeded("-0", 16)); + EXPECT_EQ(5U, APInt::getBitsNeeded("-F", 16)); EXPECT_EQ(9U, APInt::getBitsNeeded("-10", 16)); EXPECT_EQ(9U, APInt::getBitsNeeded("-1F", 16)); EXPECT_EQ(9U, APInt::getBitsNeeded("-20", 16)); @@ -1419,26 +1406,6 @@ EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1); } -TEST(APIntTest, magic) { - EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16)); - EXPECT_EQ(APInt(32, 3).magic().s, 0U); - EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16)); - EXPECT_EQ(APInt(32, 5).magic().s, 1U); - EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16)); - EXPECT_EQ(APInt(32, 7).magic().s, 2U); -} - -TEST(APIntTest, magicu) { - EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16)); - EXPECT_EQ(APInt(32, 3).magicu().s, 1U); - EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16)); - EXPECT_EQ(APInt(32, 5).magicu().s, 2U); - EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16)); - EXPECT_EQ(APInt(32, 7).magicu().s, 3U); - EXPECT_EQ(APInt(64, 25).magicu(1).m, APInt(64, "A3D70A3D70A3D70B", 16)); - EXPECT_EQ(APInt(64, 25).magicu(1).s, 4U); -} - #ifdef GTEST_HAS_DEATH_TEST #ifndef NDEBUG TEST(APIntTest, StringDeath) { @@ -1446,10 +1413,12 @@ EXPECT_DEATH((void)APInt(32, "", 0), "Invalid string length"); EXPECT_DEATH((void)APInt(32, "0", 0), "Radix should be 2, 8, 10, 16, or 36!"); EXPECT_DEATH((void)APInt(32, "", 10), "Invalid string length"); - EXPECT_DEATH((void)APInt(32, "-", 10), "String is only a sign, needs a value."); + EXPECT_DEATH((void)APInt(32, "-", 10), + "String is only a sign, needs a value."); EXPECT_DEATH((void)APInt(1, "1234", 10), "Insufficient bit width"); EXPECT_DEATH((void)APInt(32, "\0", 10), "Invalid string length"); - EXPECT_DEATH((void)APInt(32, StringRef("1\02", 3), 10), "Invalid character in digit string"); + EXPECT_DEATH((void)APInt(32, StringRef("1\02", 3), 10), + "Invalid character in digit string"); EXPECT_DEATH((void)APInt(32, "1L", 10), "Invalid character in digit string"); } #endif @@ -1468,16 +1437,16 @@ } TEST(APIntTest, Rotate) { - EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(0)); - EXPECT_EQ(APInt(8, 2), APInt(8, 1).rotl(1)); - EXPECT_EQ(APInt(8, 4), APInt(8, 1).rotl(2)); + EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(0)); + EXPECT_EQ(APInt(8, 2), APInt(8, 1).rotl(1)); + EXPECT_EQ(APInt(8, 4), APInt(8, 1).rotl(2)); EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotl(4)); - EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(8)); + EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotl(8)); EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(0)); EXPECT_EQ(APInt(8, 32), APInt(8, 16).rotl(1)); EXPECT_EQ(APInt(8, 64), APInt(8, 16).rotl(2)); - EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotl(4)); + EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotl(4)); EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotl(8)); EXPECT_EQ(APInt(32, 2), APInt(32, 1).rotl(33)); @@ -1504,16 +1473,16 @@ EXPECT_EQ(APInt(7, 6), APInt(7, 3).rotl(APInt(12, 120))); EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(0)); - EXPECT_EQ(APInt(8, 8), APInt(8, 16).rotr(1)); - EXPECT_EQ(APInt(8, 4), APInt(8, 16).rotr(2)); - EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotr(4)); + EXPECT_EQ(APInt(8, 8), APInt(8, 16).rotr(1)); + EXPECT_EQ(APInt(8, 4), APInt(8, 16).rotr(2)); + EXPECT_EQ(APInt(8, 1), APInt(8, 16).rotr(4)); EXPECT_EQ(APInt(8, 16), APInt(8, 16).rotr(8)); - EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(0)); + EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(0)); EXPECT_EQ(APInt(8, 128), APInt(8, 1).rotr(1)); - EXPECT_EQ(APInt(8, 64), APInt(8, 1).rotr(2)); - EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotr(4)); - EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(8)); + EXPECT_EQ(APInt(8, 64), APInt(8, 1).rotr(2)); + EXPECT_EQ(APInt(8, 16), APInt(8, 1).rotr(4)); + EXPECT_EQ(APInt(8, 1), APInt(8, 1).rotr(8)); EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(33)); EXPECT_EQ(APInt(32, (1 << 31)), APInt(32, 1).rotr(APInt(32, 33))); @@ -1562,7 +1531,8 @@ // No out borrow. { - APInt::WordType singleWord = ~APInt::WordType(0) << (APInt::APINT_BITS_PER_WORD - 1); + APInt::WordType singleWord = ~APInt::WordType(0) + << (APInt::APINT_BITS_PER_WORD - 1); APInt::WordType carry = APInt::tcDecrement(&singleWord, 1); EXPECT_EQ(carry, APInt::WordType(0)); EXPECT_EQ(singleWord, ~APInt::WordType(0) >> 1); @@ -1598,7 +1568,8 @@ // 2 across word borrow, no out borrow. { APInt::WordType test[4] = {0x0, 0x0, 0xC, 0x1}; - APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), 0xB, 0x1}; + APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), + 0xB, 0x1}; APInt::WordType carry = APInt::tcDecrement(test, 4); EXPECT_EQ(carry, APInt::WordType(0)); EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0); @@ -1607,7 +1578,8 @@ // 3 across word borrow, no out borrow. { APInt::WordType test[4] = {0x0, 0x0, 0x0, 0x1}; - APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0), 0x0}; + APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), + ~APInt::WordType(0), 0x0}; APInt::WordType carry = APInt::tcDecrement(test, 4); EXPECT_EQ(carry, APInt::WordType(0)); EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0); @@ -1616,7 +1588,8 @@ // 3 across word borrow, with out borrow. { APInt::WordType test[4] = {0x0, 0x0, 0x0, 0x0}; - APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0), ~APInt::WordType(0)}; + APInt::WordType expected[4] = {~APInt::WordType(0), ~APInt::WordType(0), + ~APInt::WordType(0), ~APInt::WordType(0)}; APInt::WordType carry = APInt::tcDecrement(test, 4); EXPECT_EQ(carry, APInt::WordType(1)); EXPECT_EQ(APInt::tcCompare(test, expected, 4), 0); @@ -1628,22 +1601,17 @@ uint64_t E1 = 0x2CA7F46BF6569915ULL; APInt A1(64, E1); for (unsigned i = 0, e = 64; i < e; ++i) { - EXPECT_EQ(bool(E1 & (1ULL << i)), - A1[i]); + EXPECT_EQ(bool(E1 & (1ULL << i)), A1[i]); } // Multiword check. - APInt::WordType E2[4] = { - 0xEB6EB136591CBA21ULL, - 0x7B9358BD6A33F10AULL, - 0x7E7FFA5EADD8846ULL, - 0x305F341CA00B613DULL - }; - APInt A2(APInt::APINT_BITS_PER_WORD*4, E2); + APInt::WordType E2[4] = {0xEB6EB136591CBA21ULL, 0x7B9358BD6A33F10AULL, + 0x7E7FFA5EADD8846ULL, 0x305F341CA00B613DULL}; + APInt A2(APInt::APINT_BITS_PER_WORD * 4, E2); for (unsigned i = 0; i < 4; ++i) { for (unsigned j = 0; j < APInt::APINT_BITS_PER_WORD; ++j) { EXPECT_EQ(bool(E2[i] & (1ULL << j)), - A2[i*APInt::APINT_BITS_PER_WORD + j]); + A2[i * APInt::APINT_BITS_PER_WORD + j]); } } } @@ -1678,17 +1646,17 @@ // Test round up. APInt::WordType I4[4] = {0x0, 0xF, 0x18, 0x0}; - APInt A4(APInt::APINT_BITS_PER_WORD*4, I4); + APInt A4(APInt::APINT_BITS_PER_WORD * 4, I4); EXPECT_EQ(A4.nearestLogBase2(), A4.ceilLogBase2()); // Test round down. APInt::WordType I5[4] = {0x0, 0xF, 0x10, 0x0}; - APInt A5(APInt::APINT_BITS_PER_WORD*4, I5); + APInt A5(APInt::APINT_BITS_PER_WORD * 4, I5); EXPECT_EQ(A5.nearestLogBase2(), A5.logBase2()); // Test ties round up. uint64_t I6[4] = {0x0, 0x0, 0x0, 0x18}; - APInt A6(APInt::APINT_BITS_PER_WORD*4, I6); + APInt A6(APInt::APINT_BITS_PER_WORD * 4, I6); EXPECT_EQ(A6.nearestLogBase2(), A6.ceilLogBase2()); // Test BitWidth == 1 special cases. @@ -1749,7 +1717,7 @@ EXPECT_FALSE(APInt(32, 0xffff0000).isMask()); EXPECT_FALSE(APInt(32, 0xff << 1).isMask()); - for (int N : { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) { + for (int N : {1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256}) { EXPECT_FALSE(APInt(N, 0).isMask()); APInt One(N, 1); @@ -1767,7 +1735,7 @@ EXPECT_TRUE(APInt(32, 0xffff0000).isShiftedMask()); EXPECT_TRUE(APInt(32, 0xff << 1).isShiftedMask()); - for (int N : { 1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256 }) { + for (int N : {1, 2, 3, 4, 7, 8, 16, 32, 64, 127, 128, 129, 256}) { EXPECT_FALSE(APInt(N, 0).isShiftedMask()); APInt One(N, 1); @@ -1871,8 +1839,8 @@ EXPECT_EQ(0x0f0f0f0f0f0f0f0f, APInt(64, 0xf0f0f0f0f0f0f0f0).reverseBits()); EXPECT_EQ(0xf0f0f0f0f0f0f0f0, APInt(64, 0x0f0f0f0f0f0f0f0f).reverseBits()); - for (unsigned N : { 1, 8, 16, 24, 31, 32, 33, - 63, 64, 65, 127, 128, 257, 1024 }) { + for (unsigned N : + {1, 8, 16, 24, 31, 32, 33, 63, 64, 65, 127, 128, 257, 1024}) { for (unsigned I = 0; I < N; ++I) { APInt X = APInt::getOneBitSet(N, I); APInt Y = APInt::getOneBitSet(N, N - (I + 1)); @@ -2025,8 +1993,7 @@ EXPECT_EQ(APInt(48, 0), APInt(144, "281474976710655", 10).extractBits(48, 48)); - EXPECT_EQ(APInt(48, 0), - APInt(144, "281474976710655", 10).lshr(48).trunc(48)); + EXPECT_EQ(APInt(48, 0), APInt(144, "281474976710655", 10).lshr(48).trunc(48)); EXPECT_EQ(APInt(48, 0x0000ffffffffffffull), APInt(144, "281474976710655", 10).extractBits(48, 0)); EXPECT_EQ(APInt(48, 0x0000ffffffffffffull), @@ -2647,7 +2614,8 @@ } { APInt Quo = A.udiv(B); - EXPECT_EQ(Quo, APIntOps::RoundingUDiv(A, B, APInt::Rounding::TOWARD_ZERO)); + EXPECT_EQ(Quo, + APIntOps::RoundingUDiv(A, B, APInt::Rounding::TOWARD_ZERO)); EXPECT_EQ(Quo, APIntOps::RoundingUDiv(A, B, APInt::Rounding::DOWN)); } } @@ -2662,7 +2630,8 @@ APInt Zero(8, 0); EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero, A, APInt::Rounding::UP)); EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero, A, APInt::Rounding::DOWN)); - EXPECT_EQ(0, APIntOps::RoundingSDiv(Zero, A, APInt::Rounding::TOWARD_ZERO)); + EXPECT_EQ(0, + APIntOps::RoundingSDiv(Zero, A, APInt::Rounding::TOWARD_ZERO)); } for (int64_t Bi = -128; Bi <= 127; Bi++) { @@ -2739,52 +2708,51 @@ // Verify that "Solution" is the first non-negative integer that solves // Ax^2 + Bx + C = "0 or overflow", i.e. that it is a correct solution // as calculated by SolveQuadraticEquationWrap. - auto Validate = [] (int A, int B, int C, unsigned Width, int Solution) { + auto Validate = [](int A, int B, int C, unsigned Width, int Solution) { int Mask = (1 << Width) - 1; // Solution should be non-negative. EXPECT_GE(Solution, 0); - auto OverflowBits = [] (int64_t V, unsigned W) { - return V & -(1 << W); - }; + auto OverflowBits = [](int64_t V, unsigned W) { return V & -(1 << W); }; int64_t Over0 = OverflowBits(C, Width); - auto IsZeroOrOverflow = [&] (int X) { - int64_t ValueAtX = A*X*X + B*X + C; + auto IsZeroOrOverflow = [&](int X) { + int64_t ValueAtX = A * X * X + B * X + C; int64_t OverX = OverflowBits(ValueAtX, Width); return (ValueAtX & Mask) == 0 || OverX != Over0; }; - auto EquationToString = [&] (const char *X_str) { + auto EquationToString = [&](const char *X_str) { return (Twine(A) + Twine(X_str) + Twine("^2 + ") + Twine(B) + Twine(X_str) + Twine(" + ") + Twine(C) + Twine(", bitwidth: ") + - Twine(Width)).str(); + Twine(Width)) + .str(); }; - auto IsSolution = [&] (const char *X_str, int X) { + auto IsSolution = [&](const char *X_str, int X) { if (IsZeroOrOverflow(X)) return ::testing::AssertionSuccess() - << X << " is a solution of " << EquationToString(X_str); + << X << " is a solution of " << EquationToString(X_str); return ::testing::AssertionFailure() - << X << " is not an expected solution of " - << EquationToString(X_str); + << X << " is not an expected solution of " + << EquationToString(X_str); }; - auto IsNotSolution = [&] (const char *X_str, int X) { + auto IsNotSolution = [&](const char *X_str, int X) { if (!IsZeroOrOverflow(X)) return ::testing::AssertionSuccess() - << X << " is not a solution of " << EquationToString(X_str); + << X << " is not a solution of " << EquationToString(X_str); return ::testing::AssertionFailure() - << X << " is an unexpected solution of " - << EquationToString(X_str); + << X << " is an unexpected solution of " + << EquationToString(X_str); }; // This is the important part: make sure that there is no solution that // is less than the calculated one. if (Solution > 0) { - for (int X = 1; X < Solution-1; ++X) + for (int X = 1; X < Solution - 1; ++X) EXPECT_PRED_FORMAT1(IsNotSolution, X); } @@ -2795,10 +2763,10 @@ // Generate all possible quadratic equations with Width-bit wide integer // coefficients, get the solution from SolveQuadraticEquationWrap, and // verify that the solution is correct. - auto Iterate = [&] (unsigned Width) { + auto Iterate = [&](unsigned Width) { assert(1 < Width && Width < 32); - int Low = -(1 << (Width-1)); - int High = (1 << (Width-1)); + int Low = -(1 << (Width - 1)); + int High = (1 << (Width - 1)); for (int A = Low; A != High; ++A) { if (A == 0) @@ -2806,8 +2774,7 @@ for (int B = Low; B != High; ++B) { for (int C = Low; C != High; ++C) { Optional S = APIntOps::SolveQuadraticEquationWrap( - APInt(Width, A), APInt(Width, B), - APInt(Width, C), Width); + APInt(Width, A), APInt(Width, B), APInt(Width, C), Width); if (S.hasValue()) Validate(A, B, C, Width, S->getSExtValue()); }