Skip to content

Commit 1d394dd

Browse files
committed
Create generate_cube.c
1 parent 5629769 commit 1d394dd

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

LookupTable/generate_cube.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @file generate_cube.c
3+
* @author Xuhua Huang
4+
* @brief
5+
* @version 0.1
6+
* @date 2025-06-08
7+
*
8+
* @copyright Copyright (c) 2025
9+
*
10+
*/
11+
12+
#include <stdint.h>
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
16+
typedef uint32_t (*lut_func_t)(uint32_t);
17+
18+
// Function to generate a LUT dynamically
19+
uint32_t* generate_lut(size_t size, lut_func_t func) {
20+
uint32_t* table = malloc(size * sizeof(uint32_t));
21+
if (table == NULL) {
22+
return NULL;
23+
}
24+
25+
for (size_t i = 0; i < size; ++i) {
26+
table[i] = func(i);
27+
}
28+
return table;
29+
}
30+
31+
// Example transformation function
32+
uint32_t cube(uint32_t x) {
33+
return x * x * x;
34+
}
35+
36+
int main(void) {
37+
size_t size = 10;
38+
uint32_t* cube_table = generate_lut(size, cube);
39+
40+
if (cube_table) {
41+
for (size_t i = 0; i < size; ++i) {
42+
printf("Cube of %zu is %u\n", i, cube_table[i]);
43+
}
44+
free(cube_table);
45+
}
46+
47+
return 0;
48+
}

0 commit comments

Comments
 (0)