2

Using signed integers can yield significant performance improvements.

And when we use unsigned integers it might just mean we need bigger numbers and not that overflow needs to be defined.

So why is there no unsigned integer types with the benefits of signed integer types?

Hrisip
  • 900
  • 4
  • 13
  • what do you mean with "benefits of signed integer types" ? You consider overflow being UB a benefit ? Why ? – 463035818_is_not_an_ai Nov 08 '20 at 15:35
  • @idclev463035818, as I said, using them(signed integers) yields performance improvements – Hrisip Nov 08 '20 at 15:37
  • so your point is that presence of UB is a performance improvement and thats why you want to use signed, but when you need a wider type you use unsigeds but are not happy about the missing "performance improvement" because there is no UB ? Just trying to understand your line of reasoning, because to me it is a bit strange – 463035818_is_not_an_ai Nov 08 '20 at 15:39
  • 1
    _"...Using signed integers can yield significant performance improvements...."_ unsubstantiated - you need to provide a justification for this statement. Without substantiating this claim the question is meaningless. – Richard Critten Nov 08 '20 at 15:45
  • @idclev463035818, you follow me correctly. I want a wider type and I don't need the defined behaviour of overflow. And having this UB MIGHT give me SIGNIFICANT performance improvements. So what's the reason to not have that? That is clearly beneficial if you don't care about overflow. – Hrisip Nov 08 '20 at 15:45
  • Usually when the UB of signed integers gives a substantial performance improvement, it's because the program no longer computes what you wanted it to compute. – harold Nov 08 '20 at 15:48
  • @RichardCritten https://stackoverflow.com/questions/18195715/why-is-unsigned-integer-overflow-defined-behavior-but-signed-integer-overflow-is#:~:text=%E2%80%94%20the%20sign%20bit%20has%20the%20value%20%E2%88%92(2N%20%E2%88%92,undefinedness%20to%20help%20with%20optimization. – Hrisip Nov 08 '20 at 15:48
  • 2
    I don't think this is an unfair question. It's hardly a secret that compilers make use of the rules (or lack thereof) surrounding signed overflow to make optimisations, optimisations which we cannot even opt into if we want the whole unsigned int range and don't care about having wraparound. – Asteroids With Wings Nov 08 '20 at 15:50
  • 1
    @Hrisip nothing about __performance__ in that link – Richard Critten Nov 08 '20 at 15:50
  • 1
    @RichardCritten How is optimisation not linked to performance?? – Asteroids With Wings Nov 08 '20 at 15:51
  • @Hrisip _"..may be able to optimize..."_ this is speculation in the link and no examples are given or optimization techniques described. – Richard Critten Nov 08 '20 at 15:53
  • @RichardCritten https://www.gnu.org/software/autoconf/manual/autoconf-2.67/html_node/Signed-Overflow-Examples.html https://kristerw.blogspot.com/2016/02/how-undefined-signed-overflow-enables.html https://stackoverflow.com/a/23889238/4386278 – Asteroids With Wings Nov 08 '20 at 15:54
  • 1
    @Hrisip Unfortunately, I don't have an answer for you. I can only guess that there was never a strong enough use case to have the best of both worlds here, the expanded range of an unsigned int being considered a pleasing bonus that you get with the wrapping behaviour, rather than the other way around. Also with the advent of 64-bit types, even signed integers can be huge now. There may be some reasoning further down in typical architectures but I'm not sufficiently familiar with that layer to go there. Hopefully someone knowledgeable will come along with an answer. – Asteroids With Wings Nov 08 '20 at 16:00
  • 1
    I never knew about this phenomenon, so I made a quick example for anyone who was as confused as I was using the example in asteroid's link. https://godbolt.org/z/Md5o1o – JohnFilleau Nov 08 '20 at 16:30
  • @JohnFilleau you miss the point completely – Hrisip Nov 08 '20 at 16:46
  • 1
    @Hrisip that's likely because I'm not a smart man, but I was excited to learn something new and wanted to share for anyone who may be confused. I never considered that compilers would make these types of optimizations. I'm not trying to answer your question. I'm just making a comment, which is arguably what the comment section is for. I would appreciate if you let me know where the disconnect is, but I also acknowledge you're under no obligation to. – JohnFilleau Nov 08 '20 at 16:50

0 Answers0