summaryrefslogtreecommitdiff
path: root/llvm/lib/Support/APInt.cpp
AgeCommit message (Collapse)Author
2025-11-18[APInt] Introduce carry-less multiply primitives (#168527)Ramkumar Ramachandra
In line with a std proposal to introduce std::clmul, and in preparation to introduce a clmul intrinsic, implement carry-less multiply primitives for APIntOps, clmul[rh]. Ref: https://isocpp.org/files/papers/P3642R3.html
2025-08-21[ADT] Add fshl/fshr operations to APInt (#153790)Chaitanya Koparkar
These operations are required for #153151.
2025-08-17[Support] Remove an unnecessary cast (NFC) (#154048)Kazu Hirata
qp is already of uint64_t.
2025-08-13[ADT] Add signed and unsigned mulExtended to APInt (#153399)Pedro Lobo
Adds `mulsExtended` and `muluExtended` methods to `APInt`, as suggested in #153293. These are based on the `MULDQ` and `MULUDQ` x86 intrinsics.
2025-05-22[APInt] APInt::clearBitsSlowCase - fix cut+paste typo when merging ↵Simon Pilgrim
lo/himasks (#141108) Fixes #141098
2025-05-21[NFC][ADT/Support] Add {} for else when if body has {} (#140758)Rahul Joshi
2025-05-19[APInt] Added APInt::clearBits() method (#137098)Liam Semeria
Added APInt::clearBits(unsigned loBit, unsigned hiBit) that clears bits within a certain range. Fixes #136550 --------- Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
2025-01-17remove extra ; (#123352)Iman Hosseini
Remove erroneous extra semicolon in: https://github.com/llvm/llvm-project/pull/122788 Co-authored-by: ImanHosseini <imanhosseini.17@gmail.com>
2025-01-17add power function to APInt (#122788)Iman Hosseini
I am trying to calculate power function for APFloat, APInt to constant fold vector reductions: https://github.com/llvm/llvm-project/pull/122450 I need this utility to fold N `mul`s into power. --------- Co-authored-by: ImanHosseini <imanhosseini.17@gmail.com> Co-authored-by: Jakub Kuderski <kubakuderski@gmail.com>
2024-10-17APInt.cpp: Prune a stray semicolon.NAKAMURA Takumi
2024-10-05[APInt] Slightly simplify APInt::ashrSlowCase. NFC (#111220)Craig Topper
Use an arithmetic shift for the last word copy when BitShift!=0. This avoids an explicit sign extend after the shift.
2024-09-04[APInt] improve initialization performance (#106945)Princeton Ferro
The purpose is to save an extra memset in both cases: 1. When `int64_t(val) < 0`, zeroing out is redundant as the subsequent for-loop will initialize to `val .. 0xFFFFF ....`. Instead we should only create an uninitialized buffer, and transform the slow for-loop into a memset to initialize the higher words to `0xFF`. 2. In the other case, first we create an uninitialized array (`new int64_t[]`) and _then_ we zero it out with `memset`. But this can be combined in one operation with `new int64_t[]()`, which default-initializes the array. On one example where use of APInt was heavy, this improved compile time by 1%.
2024-09-02[APInt] Add default-disabled assertion to APInt constructor (#106524)Nikita Popov
If the uint64_t constructor is used, assert that the value is actually a signed or unsigned N-bit integer depending on whether the isSigned flag is set. Provide an implicitTrunc flag to restore the previous behavior, where the argument is silently truncated instead. In this commit, implicitTrunc is enabled by default, which means that the new assertions are disabled and no actual change in behavior occurs. The plan is to flip the default once all places violating the assertion have been fixed. See #80309 for the scope of the necessary changes. The primary motivation for this change is to avoid incorrectly specified isSigned flags. A recurring problem we have is that people write something like `APInt(BW, -1)` and this works perfectly fine -- until the code path is hit with `BW > 64`. Most of our i128 specific miscompilations are caused by variants of this issue. The cost of the change is that we have to specify the correct isSigned flag (and make sure there are no excess bits) for uses where BW is always <= 64 as well.
2024-08-14[APInt] Correct backwards static_assert condition. (#103641)Craig Topper
In order to guarantee that extracting 64 bits doesn't require more than 2 words, the word size would need to be 64 bits or more. If the word size was smaller than 64, like 32, you may need to read 3 words to get 64 bits.
2024-08-13[APInt] Use APINT_BITS_PER_WORD instead of recomputing it. NFCCraig Topper
2024-04-12Fix typos (#88565)Victor Toni
2024-04-10[APInt] Remove accumulator initialization from tcMultiply and ↵Craig Topper
tcFullMultiply. NFCI (#88202) The tcMultiplyPart routine has a flag that says whether to add to the accumulator or overwrite it. By using the overwrite mode on the first iteration we don't need to initialize the accumulator to zero. Note, the initialization in tcFullMultiply was only initializing the first rhsParts of dst. tcMultiplyPart always overwrites the rhsParts+1 part that just contains the last carry. The first write to each part of dst past rhsParts is a carry write so that's how the upper part of dst is initialized.
2024-04-08[APInt] Use a std::move() to avoid a copy in the loop in ↵Craig Topper
multiplicativeInverse. (#87655) This allows the subtract to reuse the storage of T. T will be assigned over by the condition on the next iteration. I think assigning over a moved from value should be ok.
2024-04-04[APInt] Remove multiplicativeInverse with explicit modulus (#87644)Jay Foad
All callers have been changed to use the new simpler overload with an implicit modulus of 2^BitWidth. The old form was never used or tested with non-power-of-two modulus anyway.
2024-04-04[APInt] Add a simpler overload of multiplicativeInverse (#87610)Jay Foad
The current APInt::multiplicativeInverse takes a modulus which can be any value, but all in-tree callers use a power of two. Moreover, most callers want to use two to the power of the width of an existing APInt, which is awkward because 2^N is not representable as an N-bit APInt. Add a new overload of multiplicativeInverse which implicitly uses 2^BitWidth as the modulus.
2024-04-02[ADT] Add signed and unsigned mulh to APInt (#84719)Atousa Duprat
Fixes #84207
2024-03-15[ADT][APInt] add sfloordiv_ov APInt's member function (#84720)long.chen
for mlir fold to avoid too many overflow state check
2024-03-14[APInt] Implement average functions without sign/zero-extension. NFC. (#85212)Jay Foad
Removing the extension to FullWidth should make them much more efficient in the 64-bit case, because 65-bit APInts use a separate allocation for their bits.
2024-03-14[ADT] Add implementations for avgFloor and avgCeil to APInt (#84431)Atousa Duprat
Supports both signed and unsigned expansions. SelectionDAG now calls the APInt implementation of these functions. Fixes #84211.
2024-03-05[clang] Use separator for large numeric values in overflow diagnostic (#80939)Atousa Duprat
Add functionality to APInt::toString() that allows it to insert separators between groups of digits, using the C++ literal separator ' between groups. Fixes issue #58228 Reviewers: @AaronBallman, @cjdb, @tbaederr
2023-06-27[Align] Add isAligned taking an APIntGuillaume Chatelet
This showed up in https://reviews.llvm.org/D153308 Reviewed By: courbet, nikic Differential Revision: https://reviews.llvm.org/D153356
2023-05-31[APInt] Support zero-width extract in extractBitsAsZExtValue()Nikita Popov
D111241 added support for extractBits() with zero width. Extend this to extractBitsAsZExtValue() as well for consistency (in which case it will always return zero). Differential Revision: https://reviews.llvm.org/D151788
2023-05-25[APInt] Add unsigned overloads of shift functionsJay Foad
Add overloads of sshl_ov, ushl_ov, sshl_sat and ushl_sat that take the shift amount as unsigned instead of APInt. This matches what we do for the normal shift operators and can help to avoid creating temporary APInts in some cases. Differential Revision: https://reviews.llvm.org/D151420
2023-05-19Add control of hex casing in APInt::toStringThomas Preud'homme
This will be used in implementing arbitrary precision support to FileCheck's numeric variables and expressions. Reviewed By: foad, RKSimon Differential Revision: https://reviews.llvm.org/D150879
2023-02-19Use APInt::count{l,r}_{zero,one} (NFC)Kazu Hirata
2023-02-14[NFC] Replace -1U{LL} expressions with appropriate *_MAX macros in Support ↵Pavel Kopyl
library. This makes a code a bit more clear and also gets rid of C4146 warning on MSVC compiler: 'unary minus operator applied to unsigned type, result still unsigned'. In case uint64_t variable is initialized or compared against -1U expression, which corresponds to 32-bit constant, UINT_MAX macro is used to preserve NFC semantics; -1ULL is replaced with UINT64_MAX. Reviewed By: dblaikie, craig.topper Differential Revision: https://reviews.llvm.org/D143942
2023-01-28Use llvm::byteswap instead of ByteSwap_{16,32,64} (NFC)Kazu Hirata
2023-01-28Use llvm::count{lr}_{zero,one} (NFC)Kazu Hirata
2023-01-25[Support] Use llvm::countr_zero and llvm::Log2_64 in APInt.cpp (NFC)Kazu Hirata
partMSB and partLSB never get 0 as the argument. That is, we don't rely on find{First,Last}Set's ability to return std::numeric_limits<T>::max() on input 0. This patch replaces partLSB and partMSB with llvm::countr_zero and llvm::Log2_64, respectively. FWIW, nobody in LLVM (except unit test MathExtrasTest.cpp) relies on find{First,Last}Set's ability to return std::numeric_limits<T>::max() on input 0.
2023-01-22Use llvm::popcount instead of llvm::countPopulation(NFC)Kazu Hirata
2023-01-15[Support] clang-format partMSBpartMSB and partLSB (NFC)Kazu Hirata
2023-01-15Use the default parameters of countTrailingZeros and find{First,Last}Set (NFC)Kazu Hirata
This patch uses the default parameters of countTrailingZeros, findFirstSet, and findLastSet, which are ZB_Width, ZB_Max, and ZB_Max, respectively.
2023-01-05Move from llvm::makeArrayRef to ArrayRef deduction guides - llvm/ partserge-sans-paille
Use deduction guides instead of helper functions. The only non-automatic changes have been: 1. ArrayRef(some_uint8_pointer, 0) needs to be changed into ArrayRef(some_uint8_pointer, (size_t)0) to avoid an ambiguous call with ArrayRef((uint8_t*), (uint8_t*)) 2. CVSymbol sym(makeArrayRef(symStorage)); needed to be rewritten as CVSymbol sym{ArrayRef(symStorage)}; otherwise the compiler is confused and thinks we have a (bad) function prototype. There was a few similar situation across the codebase. 3. ADL doesn't seem to work the same for deduction-guides and functions, so at some point the llvm namespace must be explicitly stated. 4. The "reference mode" of makeArrayRef(ArrayRef<T> &) that acts as no-op is not supported (a constructor cannot achieve that). Per reviewers' comment, some useless makeArrayRef have been removed in the process. This is a follow-up to https://reviews.llvm.org/D140896 that introduced the deduction guides. Differential Revision: https://reviews.llvm.org/D140955
2022-12-14Don't include Optional.hKazu Hirata
These files no longer use llvm::Optional.
2022-12-10[APInt] Convert GetMostSignificantDifferentBit to std::optionalKrzysztof Parzyszek
2022-12-08[SCEV] Convert Optional to std::optionalKrzysztof Parzyszek
2022-12-02[llvm] Use std::nullopt instead of None (NFC)Kazu Hirata
This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-10-23[ADT] APInt.cpp - remove <cstring> duplicate. NFC.Simon Pilgrim
This is already included in APInt.h
2022-07-01[ISel] Match all bits when merge undefs for DAG combineXiang1 Zhang
Reviewed By: RKSimon Differential Revision: https://reviews.llvm.org/D128570
2022-07-01Revert "[ISel] Match all bits when merge undef(s) for DAG combine"Xiang1 Zhang
This reverts commit 5fe5aa284efed1ee1492e1f266351b35f0a8bb69.
2022-07-01[ISel] Match all bits when merge undef(s) for DAG combineXiang1 Zhang
2022-06-07[APInt] Remove truncOrSelf, zextOrSelf and sextOrSelfJay Foad
Differential Revision: https://reviews.llvm.org/D125559
2022-05-19[APInt] Remove all uses of zextOrSelf, sextOrSelf and truncOrSelfJay Foad
Most clients only used these methods because they wanted to be able to extend or truncate to the same bit width (which is a no-op). Now that the standard zext, sext and trunc allow this, there is no reason to use the OrSelf versions. The OrSelf versions additionally have the strange behaviour of allowing extending to a *smaller* width, or truncating to a *larger* width, which are also treated as no-ops. A small amount of client code relied on this (ConstantRange::castOp and MicrosoftCXXNameMangler::mangleNumber) and needed rewriting. Differential Revision: https://reviews.llvm.org/D125557
2022-05-14[APInt] Allow extending and truncating to the same widthJay Foad
Allow zext, sext, trunc, truncUSat and truncSSat to extend or truncate to the same bit width, which is a no-op. Disallowing this forced clients to use workarounds like using zextOrTrunc (even though they never wanted truncation) or zextOrSelf (even though they did not want its strange behaviour of allowing a *smaller* bit width, which is also treated as a no-op). Differential Revision: https://reviews.llvm.org/D125556
2022-03-14Implement literal suffixes for _BitIntAaron Ballman
WG14 adopted N2775 (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2775.pdf) at our Feb 2022 meeting. This paper adds a literal suffix for bit-precise types that automatically sizes the bit-precise type to be the smallest possible legal _BitInt type that can represent the literal value. The suffix chosen is wb (for a signed bit-precise type) which can be combined with the u suffix (for an unsigned bit-precise type). The preprocessor continues to operate as-if all integer types were intmax_t/uintmax_t, including bit-precise integer types. It is a constraint violation if the bit-precise literal is too large to fit within that type in the context of the preprocessor (when still using a pp-number preprocessing token), but it is not a constraint violation in other circumstances. This allows you to make bit-precise integer literals that are wider than what the preprocessor currently supports in order to initialize variables, etc.