#include <iostream>
#include <inttypes.h>
uint64_t test_overflow( uint64_t start, uint64_t add, uint64_t check)
{
uint64_t count = 0 ;
uint64_t sum1 = start;
uint64_t sum2 = start;
do
{
sum2 + = sum1 + = add;
count++ ;
} while ( sum1 < check && sum2 < check) ;
return count;
}
int main( ) {
std:: cout << "Overflow of uint16_t after: " << test_overflow( 0 , UINT8_MAX, UINT16_MAX - UINT8_MAX) << " rounds (start value 0)." << std:: endl ;
std:: cout << "Overflow of uint32_t after: " << test_overflow( 0 , UINT16_MAX, UINT32_MAX - UINT16_MAX) << " rounds (start value 0)." << std:: endl ;
std:: cout << "Overflow of uint64_t after: " << test_overflow( 0 , UINT32_MAX, UINT64_MAX - UINT32_MAX) << " rounds (start value 0)." << std:: endl ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW50dHlwZXMuaD4KCnVpbnQ2NF90IHRlc3Rfb3ZlcmZsb3codWludDY0X3Qgc3RhcnQsIHVpbnQ2NF90IGFkZCwgdWludDY0X3QgY2hlY2spCnsKCXVpbnQ2NF90IGNvdW50ID0gMDsKCXVpbnQ2NF90IHN1bTEgPSBzdGFydDsKCXVpbnQ2NF90IHN1bTIgPSBzdGFydDsKCWRvCgl7CiAgICAJc3VtMiArPSBzdW0xICs9IGFkZDsKICAgIAljb3VudCsrOwogICAgfSB3aGlsZShzdW0xIDwgY2hlY2sgJiYgc3VtMiA8IGNoZWNrKTsKICAgIHJldHVybiBjb3VudDsKfQoKaW50IG1haW4oKSB7CglzdGQ6OmNvdXQgPDwgIk92ZXJmbG93IG9mIHVpbnQxNl90IGFmdGVyOiAiIDw8IHRlc3Rfb3ZlcmZsb3coMCwgVUlOVDhfTUFYLCBVSU5UMTZfTUFYIC0gVUlOVDhfTUFYKSA8PCAiIHJvdW5kcyAoc3RhcnQgdmFsdWUgMCkuIiA8PCBzdGQ6OmVuZGw7CglzdGQ6OmNvdXQgPDwgIk92ZXJmbG93IG9mIHVpbnQzMl90IGFm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