-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
167 additions
and
26 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
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
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,12 @@ | ||
/** @file random.h | ||
* @brief Private function prototypes and structure definitions for random numbers. | ||
*/ | ||
#ifndef FUSE_PRIVATE_RANDOM_H | ||
#define FUSE_PRIVATE_RANDOM_H | ||
#include <stdint.h> | ||
|
||
struct fuse_random_state { | ||
uint64_t seed; | ||
}; | ||
|
||
#endif |
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,17 @@ | ||
|
||
/////////////////////////////////////////////////////////////////////////////// | ||
// PUBLIC METHODS | ||
|
||
#ifdef TARGET_PICO | ||
#include <stdint.h> | ||
#include <pico.h> | ||
|
||
inline uint32_t rand_u32() { | ||
return get_rand_u32(); | ||
} | ||
|
||
inline uint64_t rand_u64() { | ||
return get_rand_u64(); | ||
} | ||
|
||
#endif |
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,72 @@ | ||
#include <limits.h> | ||
#include <time.h> | ||
#include <fuse/fuse.h> | ||
|
||
// Private includes | ||
#include "random.h" | ||
|
||
/////////////////////////////////////////////////////////////////////////////// | ||
// PUBLIC METHODS | ||
|
||
#ifdef TARGET_POSIX | ||
#include <stdint.h> | ||
#include "random.h" | ||
|
||
// Seed | ||
static struct fuse_random_state seed = {0}; | ||
|
||
/** @brief Initalize a random number generator | ||
* | ||
* @param self A pointer to the random number generator to initialize | ||
* @param seed The seed to use for the random number generator, or zero to use the current time | ||
*/ | ||
void fuse_random_init(struct fuse_random_state *self, uint64_t seed) | ||
{ | ||
assert(self); | ||
|
||
if (self->seed == 0) | ||
{ | ||
time_t t = time(NULL); | ||
self->seed = (uint64_t)t; | ||
} else { | ||
self->seed = seed; | ||
} | ||
} | ||
|
||
/** @brief Generate a uint64 random number between 0 and UINT64_MAX using a generator seed | ||
* | ||
* @param self A pointer to the random number generator to use | ||
* @return A random number between 0 and UINT64_MAX | ||
*/ | ||
uint64_t fuse_random_u64(struct fuse_random_state *self) | ||
{ | ||
assert(self); | ||
|
||
self->seed = self->seed + 0x9E3779B97F4A7C15; | ||
uint64_t value = self->seed; | ||
value = (value ^ (value >> 30)) * 0xBF58476D1CE4E5B9; | ||
value = (value ^ (value >> 27)) * 0x94D049BB133111EB; | ||
value = value ^ (value >> 31); | ||
return value; | ||
} | ||
|
||
inline uint32_t rand_u32() | ||
{ | ||
if(seed.seed == 0) | ||
{ | ||
fuse_random_init(&seed, 0); | ||
} | ||
return fuse_random_u64(&seed); | ||
} | ||
|
||
|
||
inline uint64_t rand_u64() | ||
{ | ||
if(seed.seed == 0) | ||
{ | ||
fuse_random_init(&seed, 0); | ||
} | ||
return fuse_random_u64(&seed); | ||
} | ||
|
||
#endif |
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
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,12 @@ | ||
set(NAME "random") | ||
add_executable(${NAME} | ||
main.c | ||
) | ||
|
||
target_include_directories(${NAME} PRIVATE | ||
${CMAKE_CURRENT_LIST_DIR}/../../include | ||
) | ||
|
||
target_link_libraries(${NAME} | ||
fuse | ||
) |
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,35 @@ | ||
|
||
#include <fuse/fuse.h> | ||
|
||
int TEST_001() | ||
{ | ||
fuse_debugf(NULL, "Returning 100 random numbers - uint32\n"); | ||
|
||
for(int i = 0; i < 100; i++) { | ||
fuse_debugf(NULL, "Random number %d: %u\n", i, rand_u32()); | ||
} | ||
|
||
// Return | ||
return 0; | ||
} | ||
|
||
int TEST_002() | ||
{ | ||
fuse_debugf(NULL, "Returning 100 random numbers - uint64\n"); | ||
|
||
for(int i = 0; i < 100; i++) { | ||
fuse_debugf(NULL, "Random number %d: %lu\n", i, rand_u64()); | ||
} | ||
|
||
// Return | ||
return 0; | ||
} | ||
|
||
int main() | ||
{ | ||
assert(TEST_001() == 0); | ||
assert(TEST_002() == 0); | ||
|
||
// Return success | ||
return 0; | ||
} |