-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Open
Description
src calculates the sum of a slice of integers, then the product. tgt does the same in a single loop. LLVM should fuse the two loops into one, since they are side effect free
https://godbolt.org/z/Kdd7z4c88
#include <stddef.h>
#include <stdint.h>
typedef uint32_t u32;
struct tuple {
u32 sum;
u32 prod;
};
struct tuple src(u32* p, size_t len) {
u32 sum = 0;
u32 prod = 1;
for (size_t i = 0; i < len; i++) {
sum += p[i];
}
for (size_t i = 0; i < len; i++) {
prod *= p[i];
}
return (struct tuple){.sum = sum, .prod = prod};
}
struct tuple tgt(u32* p, size_t len) {
u32 sum = 0;
u32 prod = 1;
for (size_t i = 0; i < len; i++) {
sum += p[i];
prod *= p[i];
}
return (struct tuple){.sum = sum, .prod = prod};
}