Commit Graph

87 Commits

Author SHA1 Message Date
Chris Lattner 6b69568f85 This adds a bunch of static functions that implement unsigned
two's complement bignum arithmetic.  They could be used to
implement much of APInt, but the idea is they are enough to
implement APFloat as well, which the current APInt interface
is not suited for.

Patch by Neil Booth!

llvm-svn: 41124
2007-08-16 15:56:55 +00:00
Reid Spencer b6af1aae35 Fix an assertion introduced by my last change to the toString method. We
can't use getZExtValue() to extract the low order bits for each digit.
Instead, we need to access the low order word directly.

llvm-svn: 37242
2007-05-19 00:29:55 +00:00
Reid Spencer 5c138133b7 Get rid of leading zeros in the output of toString.
llvm-svn: 37175
2007-05-17 19:23:02 +00:00
Reid Spencer fffdf10566 For lshr by 0 bits, just return *this as a short cut. This also prevents
undefined behavior when the width > 64 bits.

llvm-svn: 37153
2007-05-17 06:26:29 +00:00
Reid Spencer a93c981f66 Fix a bug in the "fromString" method where radix 2,8 and 16 values were
not being generated correctly because the shl operator does not mutate its
object but returns a new value. Also, make the distinction between radix
16 and the others more clear.

llvm-svn: 37111
2007-05-16 19:18:22 +00:00
Reid Spencer 98ed7db7ab Make the results for the rotate functions correct when rotateAmt == 0.
llvm-svn: 37026
2007-05-14 00:15:28 +00:00
Reid Spencer 4c50b52f63 Add some things needed by the llvm-gcc version supporting bit accurate integer
types:
1. Functions to compute div/rem at the same time.
2. Further assurance that an APInt with 0 bitwidth cannot be constructed.
3. Left and right rotate operations.
4. An exactLogBase2 function which requires an exact power of two or it
   returns -1.

llvm-svn: 37025
2007-05-13 23:44:59 +00:00
Reid Spencer 81ee020f64 Fix shl to produce the correct result when the bitwidth is > 64 and the
shift amount is 0. Previously this code would do a lshr by the bit width
which can lead to incorrect results.

llvm-svn: 37010
2007-05-12 18:01:57 +00:00
Chris Lattner dad2d0939c silence some annoying gcc 4.3 warnings
llvm-svn: 36680
2007-05-03 18:15:36 +00:00
Reid Spencer 67378b22bb We want the number of bits needed, not the power of 2.
llvm-svn: 35977
2007-04-14 00:00:10 +00:00
Reid Spencer 9329e7b626 Implement a getBitsNeeded method to determine how many bits are needed to
represent a string in binary form by an APInt.

llvm-svn: 35968
2007-04-13 19:19:07 +00:00
Reid Spencer c49f2ef236 Fix an approximate calculation in an assertion not to give false negatives.
llvm-svn: 35901
2007-04-11 13:00:04 +00:00
Zhou Sheng 0458c1db71 Fix a bug in getAllOnesValue() which broke
some test cases for bitwidth > 64.

llvm-svn: 35232
2007-03-21 04:34:37 +00:00
Jeff Cohen e06855e684 Fix (and simplify) 48-bit byte swap.
Get pos/neg infinity the correct way.

llvm-svn: 35223
2007-03-20 20:42:36 +00:00
Reid Spencer b1ec2e8ec7 Fix coding standards violation.
llvm-svn: 35189
2007-03-19 21:19:02 +00:00
Reid Spencer 6fae35acd3 Implement extension of sign bits for negative values in the uint64_t
constructor. This helps to fix test/Assembler/2007-03-19-NegValue.ll

llvm-svn: 35180
2007-03-19 20:37:47 +00:00
Reid Spencer 6223c05805 Revert the last patch as it violates the conditions of sext/zext.
llvm-svn: 35068
2007-03-12 18:37:25 +00:00
Zhou Sheng 3999ffa0c8 For APInt::z/sext(width), if width == BitWidth, just return *this.
llvm-svn: 35065
2007-03-12 17:47:45 +00:00
Jeff Cohen b622c11f77 Unbreak VC++ build.
llvm-svn: 34917
2007-03-05 00:00:42 +00:00
Reid Spencer 1825dd0d7c Fix ashr for bitwidths > 64. This is now validated up to 1024 bits.
llvm-svn: 34852
2007-03-02 22:39:11 +00:00
Reid Spencer cf8175617d Use a better algorithm for rounding sqrt results. Change the FIXME about
this to a NOTE: because pari/gp results start to get rounded incorrectly
after 192 bits of precision. APInt and pari/gp never differ by more than
1, but APInt is more accurate because it does not lose precision after 192
bits as does pari/gp.

llvm-svn: 34834
2007-03-02 04:21:55 +00:00
Reid Spencer c44bdae16b Add a FIXME
llvm-svn: 34828
2007-03-02 02:01:34 +00:00
Reid Spencer c442c84c8f Fix a problem where shifting by 64-bits leads to incorrect results on PPC
but not on X86 becuase shift by word size is "undefined".

llvm-svn: 34825
2007-03-02 01:19:42 +00:00
Reid Spencer 1b8dfcbaaa Combine two lines that can be.
llvm-svn: 34818
2007-03-01 23:30:25 +00:00
Reid Spencer 2f6ad4d3c3 Make the static table of results in sqrt const.
llvm-svn: 34791
2007-03-01 17:47:31 +00:00
Reid Spencer 742d1704e1 Add methods for bit width modification: sextOrTrunc, zextOrTrunc.
llvm-svn: 34789
2007-03-01 17:15:32 +00:00
Reid Spencer c8841d2ea2 Use a real table in sqrt to shorten and quicken the code.
Thanks for the idea Chris.

llvm-svn: 34779
2007-03-01 06:23:32 +00:00
Reid Spencer d99feafd5f Add a square root function.
llvm-svn: 34775
2007-03-01 05:39:56 +00:00
Reid Spencer 91d3b3fce9 Make the trunc/sext/zext methods return APInt& so that these operations
can be chained together with other operations.

llvm-svn: 34743
2007-02-28 17:34:32 +00:00
Reid Spencer 66d0d572c2 Fix a bug in RoundDoubleToAPInt where it would force the size to 64 bits
instead of honoring the client's requested bit width.

llvm-svn: 34712
2007-02-28 01:30:08 +00:00
Reid Spencer c2d433d47a Join two lines that can be joined.
llvm-svn: 34705
2007-02-27 23:49:07 +00:00
Reid Spencer 31acef50ad Implement countLeadingOnes() and getMinSignedBits(). This helps to minimize
the bit width of negative numbers by computing the minimum bit width for a
negative value. E.g. 0x1800000000000000 could be just 0x8000000000000000

llvm-svn: 34695
2007-02-27 21:59:26 +00:00
Reid Spencer 70cb5d4287 Improve APInt interface:
1. Add unsigned and signed versions of methods so a "bool" argument doesn't
   need to be passed in.
2. Make the various getMin/getMax functions all be inline since they are
   so simple.
3. Simplify sdiv and srem code.

llvm-svn: 34680
2007-02-27 20:24:31 +00:00
Reid Spencer 997d9e4585 Conform to single person attribution in file comment.
llvm-svn: 34678
2007-02-27 19:31:09 +00:00
Reid Spencer 54abdcf30b 1. Fix three serious bugs in the comparison code.
2. Change RoundDoubleToAPInt to take a bit width parameter. Use that
   parameter to limit the bit width of the result.

llvm-svn: 34673
2007-02-27 18:23:40 +00:00
Reid Spencer 011651745c Get rid of extraneous // in file comment.
llvm-svn: 34662
2007-02-27 07:35:18 +00:00
Reid Spencer 974551a7e9 Simplify and document RoundDoubleToAPInt.
llvm-svn: 34648
2007-02-27 01:28:10 +00:00
Reid Spencer 7c16cd2746 1. Make sure all delete operators of arrays use the array form of delete.
2. Rewrite operator=(const APInt& RHS) to allow the RHS to be a different
   bit width than the LHS. This makes it possible to use APInt as the key
   of a DenseMap, as needed for the IntConstants map in Constants.cpp
3. Fix operator=(uint64_t) to clear unused bits in case the client assigns
   a value that has more bits than the APInt allows.
4. Assert that bit widths are equal in operator==
5. Revise getHashValue() to put the bit width in the low order six bits.
   This should help to make i1 0, i2 0, ... i64 0 all distinct in the
   IntConstants DenseMap.

llvm-svn: 34646
2007-02-26 23:38:21 +00:00
Reid Spencer b2bc985a4e Implement the getHashValue method.
Fix toString use of getValue to use getZExtValue()

llvm-svn: 34642
2007-02-26 21:02:27 +00:00
Reid Spencer f09418d428 Re-enable this. The header was committed.
llvm-svn: 34634
2007-02-26 08:10:54 +00:00
Chris Lattner d9fc268035 another missing header :( :( :(
llvm-svn: 34632
2007-02-26 07:51:00 +00:00
Reid Spencer aa8dcfe441 1. Remove redundant calls to clearUsedBits().
2. Fix countTrailingZeros to use a faster algorithm.
3. Simplify sext() slightly by using isNegative().
4. Implement ashr using word-at-a-time logic instead of bit-at-a-time
5. Rename locals named isNegative so they don't clash with method name.
6. Fix fromString to compute negated value correctly.

llvm-svn: 34629
2007-02-26 07:44:38 +00:00
Reid Spencer 44eef169fa Rewrite lshr to not do bit by bit copy but to copy and shift whole words.
This makes it much more efficient.

llvm-svn: 34618
2007-02-26 01:19:48 +00:00
Reid Spencer fb55b7b99d Fix sext operation. Shifting by zero would leave an incorrect mask.
llvm-svn: 34617
2007-02-25 23:54:00 +00:00
Reid Spencer b6b5cc3e2f 1. Fix the flip() method to correctly flip all words of the APInt.
2. Implement the trunc, sext, and zext operations.
3. Improve fromString to accept negative values as input.

llvm-svn: 34616
2007-02-25 23:44:53 +00:00
Reid Spencer a41e93be3d 1. Provide more detail in file comment.
2. Move comments for methods to .h file, delete them in .cpp file.
3. All places that were doing manual clear of high order bits now call the
   clearUnusedBits() method in order to not depend on undefined behavior
   of the >> operator when the number of bits shifted equals the word size.
4. Reduced # of loc by using the new result of clearUnusedBits() method.
5. Simplified logic (decreased indentation) in a few places.
6. Added code comments to larger functions that needed them.
7. Added FIXME notes about weak implementations of things (e.g. bit-by-bit
   shift right is sub-optimal).

llvm-svn: 34603
2007-02-25 19:32:03 +00:00
Reid Spencer 28555779b4 Allow this to compile now that the header file is checked in.
llvm-svn: 34581
2007-02-25 07:30:14 +00:00
Chris Lattner 4ae31ba82c this doesn't compile, disable it
llvm-svn: 34571
2007-02-25 02:32:44 +00:00
Reid Spencer 522ca7cd8b Clean up lshr and ashr to coding standards.
Handle the single word cases for shiftAmt == BitWidth.

llvm-svn: 34569
2007-02-25 01:56:07 +00:00
Reid Spencer ab0e08ae2f Whoops, last word with bits in large shift left wasn't correct.
llvm-svn: 34565
2007-02-25 01:08:58 +00:00