-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contrast ifunc with alternative approaches
- Loading branch information
1 parent
fd4e2e4
commit b1df507
Showing
2 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <stdio.h> | ||
|
||
// Declare the function pointer and the "real" implementations. We are | ||
// pretending here that multiplication isn't available without SSE4.2. | ||
static int (*triple)(int) = 0; | ||
int triple_sse42(int n) { return 3 * n; } | ||
int triple_plain(int n) { return n + n + n; } | ||
|
||
// Make use of the global function pointer since we know it was resolved | ||
// in main. | ||
void print_fifteen() { | ||
int fifteen = triple(5); | ||
printf("%d\n", fifteen); | ||
} | ||
|
||
int main() { | ||
// GNU IFUNC resolves all functions up front anyways, so we will | ||
// incur the same startup cost by doing it in main. | ||
__builtin_cpu_init(); | ||
if (__builtin_cpu_supports("sse4.2")) { | ||
triple = triple_sse42; | ||
} else { | ||
triple = triple_plain; | ||
} | ||
|
||
// Call a function that uses the resolved function pointer. | ||
print_fifteen(); | ||
return 0; | ||
} |