16 ms·
New integer types I’d like to see
- bsder 4y agoWow, someone is advocating for one's complement numerics? In 2022? Good grief. The author needs to go back and study some history as to why we dropped that idea 40+ years ago.
- codeflo 4y ago> Instead, let’s just say arithmetic on INT_NAN is undefined behavior; sanitizers can then insert assertions in debug mode. I mean, if using that value is supposed to be UB anyway, then the current behavior if standard ints conforms to the spec. Doesn’t that make the proposal largely pointless?
- IYasha 4y agoSorry, bro. Even if we have time machines, we're in the bad timeline.
- churnedgodotdev 4y agoAda solved these problems in 1983. More recently, you can use Frama-C to constrain allowable sequences of 0's and 1's for C types and formally verify correctness. In Ada since 1983 you can, e.g, declare your own 8 bit signed symmetric type without the wart -128 like so: type Sym_8 is new Integer range -127 .. 127; Then this fails at compile time: My_Signed_Byte : Sym_8 := -128; SPARK can prove all execution paths through your program are free of such constraint violations. This means safe SPARK code can disable runtime checks and run faster than the safest Rust/Zig dev settings, which insert runtime checks for over/under flow. In Frama-C, say you want a function that returns an absolute value. This function will fail to verify: /*@ ensures (x >= 0 ==> \result == x) && (x < 0 ==> \result == -x); assigns \nothing; */ int abs (int x) { if (x >=0) return x; return -x; } It fails to verify because you might have x==INT_MIN. So this will verify: #include <limits.h> /*@ requires x > INT_MIN; ensures (x >= 0 ==> \result == x) && (x < 0 ==> \result == -x); assigns \nothing; */ int abs (int x) { if (x >=0) return x; return -x; }
- Ygg2 4y agoHow does this work in presence of user supplied values? It has to have a check somewhere, no?
- churnedgodotdev 4y agoOf course. The checks are performed where the values arrive. If a file or network socket provides a number N that is going to be used as an array index, then N must be proved to be within array bounds at compile time before being used as an array index, which means you are forced to catch the bug at compile time and decide how to signal bad values of N.
- Ygg2 4y agoOk, but you can't check _runtime_ values at _compile_ time. If you have user input/file system/network supply N (where 0<N<100) you still HAVE to check it somewhere. You still have to have a runtime check when interacting with outside world.
- churnedgodotdev 4y agoOf course, that's what I thought I said in my previous comment but I guess I wasn't crystal clear. The formal verification stack basically tells you, "There's no guarantee 0<N<100 holds if you get N from an I/O stream. N can be anything." So it's your job to insert a runtime check for N<=0 or N>=100 and decide how best to handle that, e.g. panic and quit, or clamp N to a valid range if it makes sense, or avoid accessing the array if it makes sense. But the system won't let you use a possibly out of range N as an array subscript so you are forced to code a solution to this special case "at compile time", typically by inserting a runtime check. Though if you are reading data from a trusted ROM you can also use a pragma or special assert to say, "This data can be trusted to have this invariant" and avoid the runtime check.
- KsassPeuk 4y agoFor the sake of completeness: Frama-C (+ WP plugin) can verify that the program conforms to the first specification, however, it indeed can't verify that the program does not contain an undefined behavior (reason why we write the second specification).
- helloooooooo 4y agoAh so they want to replace undefined behaviour with a different kind of undefined behaviour
- klysm 4y agoI’m not sure this is a particularly useful critique. I learned a good amount from this post.
- wyldfire 4y agoI'd like to see integer types for C/C++ that are able to saturate or trap on overflow - or both types. I'm pretty sure it's even been proposed as a C standard but was rejected or postponed. It seems like a really obvious feature that's easy to support but isn't.
- klysm 4y agoTotally agree. Overflowing silently is almost never what you want. Sometimes it’s useful but that’s the rare case.
- ljosifov 4y ago+1. Would be great to have integer NAN, INF and -INF supported in h/w.
- icsa 4y agoThe k language has implemented this capability (in software) for decades. https://code.kx.com/q/ref/#datatypes https://code.kx.com/q/ref/#datatypes
- ljosifov 4y agoYes, true - thanks. Love that in K in particular, and I like K and array languages in general. Another thing they get right. :-)
- Symmetry 4y agoLikewise. That's what I was expecting going in rather than Lovecraftian monstrosities like INT_NAN. Plus they're actually something that's supported in some existing ISAs.
- deleted 4y ago[deleted]
- beardyw 4y ago> trap on overflow What do you see happening in that case?
- tialaramex 4y agoIn the first part (balanced signed integers) I had sort of expected this to eventually say "Oh, this is basically how Rust's niches work, huh" but it doesn't. This is especially weird because Jonathan explicitly calls out the "new languages" like Carbon and whatever Herb will call his thing. Rust's Option<NonZeroU32> is guaranteed to be the exact same size as u32 (4 bytes) and the standard library could do the same thing with a BalancedI32 type if it wanted to.
- jxf 4y agoQ: Who is Herb and what's the "new thing"?
- tialaramex 4y agoHerb Sutter is the WG21 convener. Versions of the C++ programming language are standards from the ISO/IEC Joint Technical Committee JTC1, sub-committee WG21 often referred to as just "the committee" in this context. The convener is in effect head of the committee. For comparison WG14 is the C language commitee. Herb has made lots of proposals for changing C++ over the past half a decade or so, a few of them have shipped in newer C++ versions, most didn't go anywhere. At CppCon (a big C++ convention) Herb's keynote was about a transpiler which takes a different syntax, including most of Herb's features that didn't make it and some new features, and turns that into C++. This has been referred to as either "cppfront" or "cpp2".
- foonathan 4y ago> This is especially weird because Jonathan explicitly calls out the "new languages" like Carbon and whatever Herb will call his thing. You overestimate my knowledge of Rust :D I wasn't aware that NonZeroU32 is a thing, thanks for pointing it out.
- TakeBlaster16 4y agoI was expecting that too. It seems strictly more expressive, since once you have the niche, you can do anything you like with it. And INT_MIN is a better place for it than zero imo. If Rust had a `BalancedI32` I would reach for it a lot more than I use the NonZero types. In my code at least, I've found zero is a pretty useful number.
- pfdietz 4y agoArbitrary length integers. That is, integers that actually act like integers.
- Zamiel_Snawley 4y agoI agree, this could be a good solution for a lot of situations. People fret about the performance impact, but I'm confident that clever engineers could come up with a way to treat numbers within the typical range(say, signed 64 bit) just as they are today, but if an operation would go out of bounds do the expensive, arbitrary length operation. Emitting some kind of extra warning when those expensive operations are actually done would also be nice.
- pfdietz 4y agoLisp compilers do this (although the integers are 61 bit, not 64, for fixnums in general values), including compile-time style warnings if generic arithmetic operations cannot be optimized. Declarations can be added at performance hotspots, but in almost all places the performance impact doesn't matter.
- tialaramex 4y agoI guess this could arguably make sense as a vocabulary type (provided in the standard library so that everybody agrees on it, not for performance reasons). Right now though you will probably get such a type for free with a library of common maths features you'd likely want to use with it. So the only question is whether there are several different libraries and people would prefer to mix and match.
- pfdietz 4y agoOne can get reasonable performance from them if the language (and ABI) is designed for it. Many implementations of lisp family languages, for example. Most computations will be on fixnums, with a fallback to bignums as required. The fixnums are unboxed. It's nice to require analysis for maximum performance rather than for correctness, since maximum performance usually doesn't matter but correctness usually does.
- amluto 4y agoI would much rather see most of this be generalized to a parametrized bounded integer type, e.g. int<lowerbound, upperbound>.
- astrange 4y agoThis would be a big improvement over how a lot of languages handle types - namely it’d let you stop calling “i16” “i32” types when they’re actually storage sizes. “Safe” languages like Java try to achieve safety by making you write boilerplate casts here - “short = int + int” is illegal without a (short) cast even though that doesn’t change the meaning of the program. If it was “x = i32<0,3> + i32<0,3>” then it highlights how silly this is and maybe they wouldn’t make you write a (i16<0,6>) cast.
- IAmLiterallyAB 4y agoExactly. In fact, I would go far as to make this the _only_ integer type. u16, u32 etc can just be typedefs. The ability to be precise with integer ranges could prevent many types of arithmetic and OOB errors.
- tialaramex 4y agoWUFFS types can be refined as well as having a native representation so e.g. base.u8[0 .. =99] is a single byte with values from 0 to 99 inclusive base.u16[0 .. =99] is a 16-bit type with the same values base.u16[100 .. =400] is still 16 bits but values from 100 to 400 inclusive In WUFFS all arithmetic overflow or array bounds misses are compile time errors. From the point of view of WUFFS if pixels is an array with 200 elements, and idx is a base.u8[0.. = 240] then pixels[idx] is a type error as it wouldn't make sense to index 240 into an array of 200 elements so that doesn't compile. As well as the obvious safety implication, this also means WUFFS can in principle go just as fast as if you were inhumanly careful with a conventional low level language and skipped any safety checks, since it knows it already took care of that. In practice it transpiles to C (which doesn't know what safety checks are).
- jeffffff 4y agoi think you could actually do that in C++ as a library with a bit of metaprogramming. the arithmetic operations will get weird though unless everything is the same type, for example what is the result type of int<-10, 10>+int<0, 20>? (the "right" answer is probably a megabyte of compiler errors)
- mlatu 4y agothe symetric signed int with int_nan i would like to have too but with the uint63_t i dont quite see the point but admit it would be neat having an unsigned int that cant overflow
- renox 4y agoZig has the unsigned int with the nice overflow/UB behaviour. Symmetric int + Int_NAN sounds nice but the performance cost..
- shultays 4y agoAny reasons for not adding these as a library and expect it from the language? C++ gives you all tools for that
- manwe150 4y agoHardware support. It is trivial to write libraries that can emulate these in many languages, but the performance drop is going to be pretty sharp if you are coding your basic arithmetic by hand. Unless you are writing code for an FPGA...
- shultays 4y agoYou could use the same hardware support in your library couldn't you? Compile flags to check your target hardware and enable/disable code if you have the support
- jeffffff 4y agothe one about unsigned integers with one bit missing would be trivial to implement as a library in C++ with no significant downside. all you have to do is make a class wrapping a signed int and put debug checks for if the high 1 bit is set behind an if constexpr in operator= and the copy constructor. in most other languages this would bring a big performance penalty, but this is one thing that C++ is actually very good at.
- eklitzke 4y agoThis is incorrect. For one thing, you're going to need to overload all the operators that mutate the integer in place, e.g. operator++, operator +=, operator -=, operator *=, shifts, etc. And those checks can be quite expensive. For example, your code is probably littered with for loops that are implemented using operator++ or operator+=, and that means on every loop iteration you need to check for overflow, which is expensive if the loop body is simple. GCC and Clang already implement -ftrapv which does something similar (it adds compiler checks at all places where signed integers can overflow and trap if overflow occurs). I've used -ftrapv in debug builds but for most programs you don't want it in non-debug builds because the overhead is too high.
- 0xblood 4y agoI don't really understand it, wouldn't this just trade the checks and corner cases you have to pay attention to with different set of corner cases? If I have an integer that can approach INT_MIN, I know I have to pay attention to overflows when using it and this proposal helps with at most some fraction of the problems that can arise. And what's the great benefit, that it get's a bit more "elegant"? If you want to write "safe" software and have the compiler enforce it, go use Rust/Ada/Spark instead.
- e63f67dd-065b 4y agoI'm far more partial to the idea that NaN was a mistake than a desired feature. Including it then making operations that involve it UB is a road to hell and sadness, far worse than abs(INT_MIN). The idea to separate bit vectors from numbers is a good one though, it always seemed a little weird to me that we're passing around numeric types for FLAG_FOO & FLAG_BAR.
- codeflo 4y agoI mean, strongly typed flag values are a solved problem. It’s not rocket science, just bothersome to do in C++.
- adrian_b 4y agoAlready in 1964 the language PL/I (still named NPL in 1964) had "bit strings" as a distinct type from integer numbers. Also "character strings" were a distinct type. I also believe that these types must be clearly distinguished, even if conversion functions must be provided between them, and even if in certain contexts it may be convenient for the conversions to be implicit.
- gumby 4y ago> Already in 1964 the language PL/I (still named NPL in 1964) had "bit strings" as a distinct type from integer numbers. That’s because lots of hardware back then had support for bit addressing. The machine I love the best, the PDP-6/PDP-10 had addressable bytes of width ranging from 1-36 bits, which were extremely handy. The few C compilers for those machines didn’t support them of course because the PDP-11s didn’t support them, but C on the PDP-10 was only used for porting stuff anyway. Honestly I’m surprised they haven’t been revived. Packed bitfields are really handy and should be fast.
- kevin_thibedeau 4y ago> I’m surprised they haven’t been revived. ARM has bit addressing with their bit-banding feature where individual bits are mapped onto byte addresses.
- liminal 4y agoCompletely agree that moving bit operations to library calls and freeing up their syntax characters makes a lot of sense. Also seems impossible!
- dahfizz 4y agoDo you feel the same about +-/*% ? I think people say this because they rarely use bitwise operators, and so are scared of them.
- samatman 4y agoI don't, no. LuaJIT uses a bit library, while Lua 5.3 and up have the native bitwise operators. I prefer the former.
- liminal 4y agoI'm not scared of them, but very rarely use them and believe that is generally true for others. Basic arithmetic operators that operate on numbers (as opposed to bitwise) are used much more frequently, so I feel very differently about them.
- gwbas1c 4y ago> Distinct bit vectors vs integer type This. I always find bitwise operations an unneeded exercise in mental gymnastics. On the other hand, I wonder if a lot of the other quirks about numbers could be handled via a library?
- klysm 4y ago> unneeded exercise in mental gymnastics Great characterization! I’ve always been confused why most languages don’t have better tools for bit vectors. They’re incredible common and it’s really confusing to have to use the underlying representation of numbers to do anything with them.
- morelisp 4y agoIt is solved by the standard library. https://en.cppreference.com/w/cpp/utility/bitset https://en.cppreference.com/w/cpp/utility/bitset
- Scaless 4y agoIt is not "solved" at all. std::bitset has terrible properties and an awful interface. 1. No dynamic resize, have to know size at compile time or allocate based on max expectations. And yes, std::vector<bool> sucks too. 2. Despite being only statically sized, several classes of bugs are not prevented at compile-time. For example: std::bitset<4> fail1{"10001"}; // This produces a value of 0b1000, no warnings or exceptions thrown std::bitset<10> fail2; fail2.set(11); // Exception at runtime. Why is this not a static_assert? 3. Size is implementation defined. std::bitset<1> can use up to 8 bytes depending on compiler/platform. 4. Debug performance is 20x slower than Release. In many cases you are going from what would be a single assembly instruction to multiple function calls. 5. Limited options for accessing underlying storage efficiently (for serialization, etc). to_ullong() will work up to 64 bits, but beyond that it will throw exceptions. 6. Uses exceptions. This is still a deal breaker for many. 7. Cannot toggle a range of bits at once. It's either one or all.
- deleted 4y ago[deleted]
- 0xblood 4y agoIf I want to do bitwise operations on an integer, and I want to set/mask the MSB (maybe it's just a bitfield for me), I mask with INT_NAN? That also sounds very weird
- MarkusWandel 4y agoThis is probably not what you're looking for because it might not be high performance. But in hardware synthesis/modeling, we use the so called "AC data types" and I just googled that and found it on Github. https://github.com/hlslibs/ac_types https://github.com/hlslibs/ac_types Arbitrary bit length, symmetrical and unsymmetrical, rounding and wraparound behaviour specifiable etc. etc.
- deleted 4y ago[deleted]
- torstenvl 4y agoCongratulations on inventing slower one's complement. > I’d like to have . . . a 63 bit unsigned integer. . . . However, it is undefined behavior if it ever stores a negative value. No. There is no "debug mode" for undefined behavior. Undefined behavior is always undefined behavior. It also doesn't "automatically come[] with a precondition that it cannot be negative" - only that if it's ever negative, your program might crash for reasons that cannot be found in your code. If you really want this, use uint64_t and treat any value over 0x7fffffffffffffff as an error. Curious about the drive-by downvoters: Can you point out where you think I'm wrong?
- Kranar 4y ago> Undefined behavior is always undefined behavior. This is not true; the standard explicitly allows implementations to, and I quote directly from the standard, "behave during translation or program execution in a documented manner characteristic of the environment". This idea that undefined behavior is always undefined behavior and there's no way to reason about it is purely academic, incorrect, and not in any way justified either by source material or in practice. As for debugging undefined behavior, UBSan [1] is an excellent tool that is well supported by GCC and clang, and MSVC is working to add support for it as well. [1] https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
- deleted 4y ago[deleted]
- jeffffff 4y agoof course the standard allows implementations to "behave during translation or program execution in a documented manner characteristic of the environment" if you do something that is undefined behavior, implementations are literally allowed to do anything they want if you do something that is undefined behavior. the key point is that they are not required to do anything, so what's the point of adding more undefined behavior for this case?
- 4y ago
- sharikous 4y agoFrom a computational point of view it would be nice to see, actually, a type that has no zero and is symmetric around it= ... -3/2,-1/2,1/2,3/2 ... It would be trivial to implement efficiently in hardware (as opposite to the checks required for INT_NAN or INT_MIN) and it would have sense in some cases (like some kinds of discretizations) From a general programming viewpoint pervasive bigints, ints with NaN/+Inf/-Inf and bitfields would be interesting too, but I don't know if the they are worth the complexity they introduce
- dheera 4y ago> So here’s my wish: a signed integer where INT_MIN == -INT_MAX Just set INT_MIN to -127 and never use -128. > Second, you’re getting symmetry back. All the operations mentioned above are now symmetric and can’t overflow. This makes them a lot easier to reason about. But you seem to want the actual bits to be symmetric with the sign bit just being a literal + or -. This makes it easier for humans to reason with, but it does NOT make it easier for a CPU's adder to add -15 and +63, which in the current standard signed int implementation does not even require the CPU adder to know that it's a signed int. Unsigned and signed int adding work exactly the same bit-wise, and can use the same hardwired circuits to process at blazing speed. That's the beauty of the current standard implementation.
- deleted 4y ago[deleted]
- CalChris 4y agoMaybe this is just too prosaic, but I'd like 64-bit unsigned integers in Java. I'd like Unums, which are not integers, especially the low precision variants.
- Viliam1234 4y agoWhat is the purpose of having a 64-bit unsigned integer? Could you tell me an example of a use case where 9×10^18 is not enough (so you cannot use unsigned 64-bit integer), but 18×10^18 will definitely be enough (so you do not need BigInteger)?
- Sniffnoy 4y agoInterestingly, in Ethereum smart contract languages (e.g. Solidity and Vyper), having separate types for bit vectors vs integers is common. I don't know why it's caught on there but nowhere else. Obviously those languages have a pretty specialized niche and aren't really going to be used outside of that context.
- zelphirkalt 4y agoDoes this really help? Sometimes being able to treat integers as bitvectors and vice versa is something desirable. I think one needs appropriate functions, which treat integers or bitvectors as such, but not necessarily a split in types, possibly requiring any kind of type conversion.
- jmull 4y agoThe argument starts here: > I really don’t like this asymmetry – it leads to annoying edge cases in all sort of integer APIs. And gets to here: > Third, you’re getting an unused bit pattern 0b1'0000000, the old INT_MIN, which you can interpret however you like. This isn't solving the original problem. If you don't like annoying edge cases in all sorts of integer APIs, it's worse. edit: I can add: the article goes on to suggest that "old INT_MIN" should be called INT_NAN and "let’s just say arithmetic on INT_NAN is undefined behavior". So now what's the result of a + b? Undefined behavior! a * b? Undefined behavior! The general result of any function taking an integer or returning one is undefined behavior!
- turminal 4y agoArithmetic on INT_MIN is already undefined behaviour.
- renox 4y ago? No! INT_MIN +1 is 'implementation defined behaviour' but that's a very different thing.
- kzrdude 4y agoIn C, with int a, b then a + b is already potential UB. Just depends on the values of the variables. It is remarkable to note this.. but not new! :)
- wyager 4y agoIf you find yourself wanting any of this, just use a language with algebraic data types and save everyone (especially yourself) some pain.