View previous topic :: View next topic |
Author |
Message |
eccerr0r Watchman

Joined: 01 Jul 2004 Posts: 9927 Location: almost Mile High in the USA
|
Posted: Mon Mar 10, 2025 4:04 pm Post subject: |
|
|
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 |
|
 |
John R. Graham Administrator


Joined: 08 Mar 2005 Posts: 10748 Location: Somewhere over Atlanta, Georgia
|
Posted: Mon Mar 10, 2025 8:14 pm Post subject: |
|
|
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 |
|
 |
John R. Graham Administrator


Joined: 08 Mar 2005 Posts: 10748 Location: Somewhere over Atlanta, Georgia
|
Posted: Mon Mar 10, 2025 8:18 pm Post subject: |
|
|
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 |
|
 |
Hu Administrator

Joined: 06 Mar 2007 Posts: 23177
|
Posted: Mon Mar 10, 2025 8:39 pm Post subject: |
|
|
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 |
|
 |
John R. Graham Administrator


Joined: 08 Mar 2005 Posts: 10748 Location: Somewhere over Atlanta, Georgia
|
Posted: Mon Mar 10, 2025 8:48 pm Post subject: |
|
|
Ah. Good point.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
 |
|