Daniel Lemire's blog

, 11 min read

Fast midpoint between two integers without overflow

18 thoughts on “Fast midpoint between two integers without overflow”

  1. moonchild says:

    ((x^y)>>1) + (x&y) is 4 operations. Whereas x + (y-x)>>1 is only 3 operations (and has the same span). Am I missing something?

    1. moonchild says:

      nevermind, the given algorithms are commutative

    2. Q says:

      `y-x` can overflow when operating on signed integers, which is UB is C/C++.

  2. BartekF says:

    you can also compare it with a C++20’s addition: std::midpoint from

    1. Champok Das says:
      1. Lockal says:

        It is kind of sad that not a single compiler implemented an optimal template specialization for std::midpoint.

        1. Lockal says:

          Update: after reading cppreference now I see why: unlike optimized versions, std::midpoint provides very specific requirements for rounding, which are not supported by optimized versions.

  3. Peter Boos says:

    (int)(x + y)*.5

  4. Stefan Kanthak says:

    EVERY properly written optimising compiler SHOULD emit code like

    ADD RDI, RSI
    RCR RDI, 1
    MOV RAX, RDI

    for this function.
    If the target processor lacks the equivalent of the RCR (Rotate through carry right) instruction, but has a ROR (Rotate right) instruction, it can emit

    ADD RDI, RSI
    ADC RDI, 0
    ROR RDI, 1
    MOV RAX, RDI

    instead.

    1. RCR is an appealing solution but, on some processors, it is significantly more expensive than a simple shift.

      1. Stefan Kanthak says:

        That’s the other reason why I mentioned to substitute it by ADC/ROR
        ALSO: RCR is (if available) ALWAYS less expensive than the “pure” C formula/expression.

  5. Sergei Sitnikov says:

    I think the example with 1 and 9223372036854775807 doesn’t demonstrate the problem: the problem is negative numbers, otherwise one can always do (uint) (x + y) >> 1.

    1. If you only have positive integers, and you are using a two’s complement signed type, then I agree that you can always work around overflows with relative ease. I did not make this assumption.

    2. John says:

      Actually, it still contains a possible overflow. You need to cast before the addition.

      1. Sergei Sitnikov says:

        It doesn’t matter, actually. The addition works exactly the same for signed and unsigned types in two’s complement representation. The cast is needed to perform the unsigned bit shift which doesn’t preserve the sign (unlike the signed shift). In some sense the signed addition of non-negative values overflows to the sign bit, then we interpret the result as unsigned and do the unsigned division by 2.

        1. That’s still an overflow though.

          $ swift repl                                                  130
          Welcome to Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51).
          Type :help for assistance.
            1> 9223372036854775807+1
          expression failed to parse:
          error: repl.swift:1:20: error: arithmetic operation '9223372036854775807 + 1' (on type 'Int') results in an overflow
          9223372036854775807+1
          ~~~~~~~~~~~~~~~~~~~^~
          

          You may say that the overflow, if it is not trapped, may be ignored, and you will be right because modern C/C++ and most other systems rely on two’s complement. However, it is still, by definition, an overflow.

          1. Sergei Sitnikov says:

            You are right guys. Today I learned that signed integer overflow behavior is undefined in C(++). Sorry for inconvenience.

  6. Norbert Juffa says:

    For historical context: The first publicly stated instance of the second formula that I am aware of appeared in a post by Peter L. Montgomery in newsgroup comp.arch on 2000/02/11; see
    https://groups.google.com/d/msg/comp.arch/gXFuGZtZKag/_5yrz2zDbe4J:


    If XOR is available, then this can be used to average
    two unsigned variables A and B when the sum might overflow:

    (A+B)/2 = (A AND B) + (A XOR B)/2