Skip to content

Commit

Permalink
Added rand generator (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe authored Oct 24, 2023
1 parent 7d222c3 commit 37fb9dc
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 26 deletions.
15 changes: 11 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ endif()
# TARGET set to linux, darwin or pico depending on PICO_BOARD
if(PICO_BOARD STREQUAL "linux")
set(TARGET "linux")
add_compile_definitions(TARGET_LINUX)
add_compile_definitions(TARGET_POSIX)
elseif(PICO_BOARD STREQUAL "darwin")
set(TARGET "darwin")
add_compile_definitions(TARGET_DARWIN)
add_compile_definitions(TARGET_POSIX)
else()
set(TARGET "pico")
add_compile_definitions(TARGET_PICO)
include(lib/pico-sdk/pico_sdk_init.cmake)
endif()

# Define project
project(fuse CXX C ASM)
message("Set TARGET to ${TARGET}")
add_compile_definitions(TARGET=${TARGET})
add_compile_definitions(DEBUG=1)

# Libraries
Expand All @@ -41,10 +44,14 @@ if(${TARGET} STREQUAL "pico")
add_subdirectory(src/picofuse)
endif()

# Tests
add_subdirectory(tests/blink)
# Tests - fuse
add_subdirectory(tests/fuseapp)
add_subdirectory(tests/map)
add_subdirectory(tests/panic)
add_subdirectory(tests/pool)
add_subdirectory(tests/random)

# Tests - picofuse
add_subdirectory(tests/blink)
add_subdirectory(tests/temperature)

1 change: 1 addition & 0 deletions include/fuse/fuse.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct fuse_instance fuse_t;
#include <fuse/list.h>
#include <fuse/map.h>
#include <fuse/pool.h>
#include <fuse/random.h>
#include <fuse/sleep.h>
#include <fuse/string.h>

Expand Down
4 changes: 3 additions & 1 deletion src/fuse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ add_library(${NAME} STATIC
panic.c
pool.c
pool_std.c
sleep.c
random_pico.c
random_posix.c
sleep_posix.c
string.c
)

Expand Down
8 changes: 2 additions & 6 deletions src/fuse/epoll.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#if TARGET == darwin
// Noop
#elif TARGET == pico
// Noop
#elif TARGET == linux
#ifdef TARGET_LINUX

///////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
Expand Down Expand Up @@ -63,4 +59,4 @@ void fuse_epoll_destroy(fuse_t *fuse, struct fuse_epoll_instance *epoll)
fuse_free(fuse, epoll);
}

#endif // TARGET == linux
#endif // TARGET_LINUX
4 changes: 1 addition & 3 deletions src/fuse/panic.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
///////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS

#ifdef TARGET
#if TARGET==linux
#ifdef TARGET_POSIX
/*
* Debug to stdout if the debug flag is set
*/
Expand All @@ -14,4 +13,3 @@ void panic(const char* unused)
}

#endif
#endif
12 changes: 12 additions & 0 deletions src/fuse/random.h
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
17 changes: 17 additions & 0 deletions src/fuse/random_pico.c
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
72 changes: 72 additions & 0 deletions src/fuse/random_posix.c
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
4 changes: 1 addition & 3 deletions src/fuse/sleep.c → src/fuse/sleep_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
///////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS

#ifdef TARGET
#if TARGET == darwin || TARGET == linux
#ifdef TARGET_POSIX

#include <stdint.h>
#include <time.h>
Expand All @@ -29,4 +28,3 @@ void sleep_ms(uint32_t ms)
}

#endif
#endif
9 changes: 0 additions & 9 deletions tests/map/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,3 @@ target_include_directories(${NAME} PRIVATE
target_link_libraries(${NAME}
fuse
)

if(${TARGET} STREQUAL "pico")
target_link_libraries(${NAME}
picofuse
)
pico_enable_stdio_usb(${NAME} 1)
pico_enable_stdio_uart(${NAME} 0)
pico_add_extra_outputs(${NAME})
endif()
12 changes: 12 additions & 0 deletions tests/random/CMakeLists.txt
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
)
35 changes: 35 additions & 0 deletions tests/random/main.c
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;
}

0 comments on commit 37fb9dc

Please sign in to comment.