English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 10 December 2020, 12:42   #1
Ernst Blofeld
<optimized out>
 
Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 321
gcc and 64 bit, long long ints, not behaving for me?

I'm looking into using 64 bit long long ints in my code for one small part of a calculation that overflows a 32 bit int.

While trying them out I saw things that surprised me, so I started writing some test code. I've now got three functions to test what happens when the 64 bit numbers overflow. I think all three functions should behave the same, but they don't, only Test2 behaves as I expect and exits when the 64 bit number overflows and becomes negative.

Can anyone explain why?

Code:
typedef long long LONG64;

void Test1(void) {
	KPrintF("Test 1:");
	KPrintF("-------");
	
	LONG64 x = 1;
	int i = 0;
	for (;;) {
		i++;
		x = 2 * x;

		if (x > 0) {
			KPrintF("1 - Yes: %ld", i);
		} else {
			KPrintF("1 - No:  %ld", i);
			break;
		}
	}
}

void Test2(void) {
	KPrintF("Test 2:");
	KPrintF("-------");

	LONG64 x = 1;
	int i = 0;
	for (;;) {
		i++;
		LONG64 new_x = 2 * x;

		if (new_x > x) {
			KPrintF("2 - Yes: %ld", i);
		} else {
			KPrintF("2 - No:  %ld", i);
			break;
		}

		x = new_x;
	}
}

void Test3(void) {
	KPrintF("Test 3:");
	KPrintF("-------");

	LONG64 x = 1;
	int i = 0;
	for (;;) {
		i++;
		LONG64 new_x = 2 * x;

		if (new_x > 0) {
			KPrintF("3 - Yes: %ld", i);
		} else {
			KPrintF("3 - No:  %ld", i);
			break;
		}

		x = new_x;
	}
}
Ernst Blofeld is offline  
Old 10 December 2020, 12:48   #2
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,311
Quote:
Originally Posted by Ernst Blofeld View Post
Can anyone explain why?
Your code triggers undefined behaivour as nothing is guaranteed if a signed type "overflows". The compiler can, for example, assume that a positive number times 2 is never becoming negative.


Note that this is different from unsigned numbers which are guaranteed to implement modulo arithmetics.
Thomas Richter is offline  
Old 10 December 2020, 12:58   #3
Ernst Blofeld
<optimized out>
 
Ernst Blofeld's Avatar
 
Join Date: Sep 2020
Location: <optimized out>
Posts: 321
Quote:
Originally Posted by Thomas Richter View Post
Your code triggers undefined behaivour as nothing is guaranteed if a signed type "overflows". The compiler can, for example, assume that a positive number times 2 is never becoming negative.


Note that this is different from unsigned numbers which are guaranteed to implement modulo arithmetics.
Thank you, exactly the answer I needed.
Ernst Blofeld is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting Registered - How Long?? scrapser project.WHDLoad 8 05 May 2008 12:11
Towers (Warning, long shot and long post) Drake1009 Looking for a game name ? 2 13 May 2005 00:11
has it really been so long?... ShuDog New to Emulation or Amiga scene 4 29 April 2003 07:57
A very long, LONG shot... staticgerbil Coders. General 0 13 April 2003 11:18
using latest snapshot...(a bit long) supergoz support.WinFellow 3 12 December 2001 23:43

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 15:36.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.08077 seconds with 13 queries