Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Lessons in Optimization on Modern X86-64 Silicon
View unanswered posts
View posts from last 24 hours

Goto page Previous  1, 2  
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
eccerr0r
Watchman
Watchman


Joined: 01 Jul 2004
Posts: 9927
Location: almost Mile High in the USA

PostPosted: Mon Mar 10, 2025 4:04 pm    Post subject: Reply with quote

On another note, ultimately since we're doing popcount on constants, I'm doubly surprised gcc didn't optimize all these away to another constant since f(constant) = another constant (at least standard arithmetic operators it's smart enough to do)...

granted fibonacci(999999) or factorial(999999) do take a lot of iterations to run (if not using their closed form solutions) and the compiler would still need to do it, so there's probably some stuff best left for runtime ... else it'd need to know a lot of solutions to iterative functions.
_________________
Intel Core i7 2700K/Radeon R7 250/24GB DDR3/256GB SSD
What am I supposed watching?
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10748
Location: Somewhere over Atlanta, Georgia

PostPosted: Mon Mar 10, 2025 8:14 pm    Post subject: Reply with quote

Hu wrote:
You may like the related trick that value & (value - 1) is false when value is a power of 2, and true otherwise.
Yeah, that would follow, wouldn't it, because all powers of two only have a single one bit.

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10748
Location: Somewhere over Atlanta, Georgia

PostPosted: Mon Mar 10, 2025 8:18 pm    Post subject: Reply with quote

eccerr0r wrote:
On another note, ultimately since we're doing popcount on constants, I'm doubly surprised gcc didn't optimize all these away to another constant since f(constant) = another constant (at least standard arithmetic operators it's smart enough to do)...
At LTO time, you mean? I've seen a rule that says you can't optimize out of existence a function with global visibility. Certainly the compiler doesn't know how it might be used elsewhere.

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23177

PostPosted: Mon Mar 10, 2025 8:39 pm    Post subject: Reply with quote

Right, the same principle enables the mask shortcut that I mentioned. Detecting power-of-2 values is common enough in some areas that I thought it would be worth bringing up explicitly.

Regarding eccerr0r's comment, yes, the compiler needs to leave the definition of the function in case a separate object file needs it, but it could still choose to inline the effect of a known function into a known caller, if that inlining benefits from so many constant evaluations that it becomes cheaper to store the constant-propagated form than to call the function.
Code:
void f(int i);
void g(int i)
{
    f(i * 5);
}

void h()
{
    g(20);
}
The compiler might choose to inline g into h, producing:
Code:
void g(int i) { f(i * 5); } // retained in case external callers exist
void h() { f(100); } // avoid runtime computation of i * 5, when i is known to be 20
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10748
Location: Somewhere over Atlanta, Georgia

PostPosted: Mon Mar 10, 2025 8:48 pm    Post subject: Reply with quote

Ah. Good point.

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum