Skip to content

Commit

Permalink
Share prngs across all devices
Browse files Browse the repository at this point in the history
Create snek-random.c and snek-random-small.c, then share those across
all snek implementations. Select snek-random-small.c for smaller
devices to avoid needing 64-bit arithmetic.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Oct 27, 2024
1 parent 72a5851 commit bd9e863
Show file tree
Hide file tree
Showing 33 changed files with 148 additions and 250 deletions.
16 changes: 0 additions & 16 deletions chips/atmega/snek-328p.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,19 +481,3 @@ snek_builtin_stopall(void)
}
return SNEK_NULL;
}

static uint32_t random_next;

snek_poly_t
snek_builtin_random_seed(snek_poly_t a)
{
random_next = a.u;
return SNEK_NULL;
}

snek_poly_t
snek_builtin_random_randrange(snek_poly_t a)
{
random_next = random_next * 1103515245L + 12345L;
return snek_float_to_poly(random_next % (uint32_t) snek_poly_get_float(a));
}
2 changes: 0 additions & 2 deletions chips/atmega/snek-atmega.builtin
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@
time.sleep, 1
time.monotonic, 0
reset, 0
random.seed, 1
random.randrange, 1
2 changes: 2 additions & 0 deletions chips/atmega/snek-atmega.defs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SNEK_ATMEGA_SRC = \
snek-atmega-eeprom.c \
snek-io.c \
snek-atof.c \
snek-random-small.c \
snek-atmega-serial.c \
snek-atmega-time.c

Expand All @@ -51,6 +52,7 @@ SNEK_ATMEGA_MATH_BUILTINS = \
SNEK_ATMEGA_BUILTINS = \
snek-eeprom.builtin \
snek-gpio.builtin \
snek-random.builtin \
snek-atmega.builtin

SNEK_NO_FILE = 1
16 changes: 0 additions & 16 deletions chips/avr/ao-snek-avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,22 +486,6 @@ snek_builtin_time_monotonic(void)
return snek_float_to_poly((float) snek_ticks() * SECONDS_PER_TICK);
}

static uint32_t random_next;

snek_poly_t
snek_builtin_random_seed(snek_poly_t a)
{
random_next = a.u;
return SNEK_NULL;
}

snek_poly_t
snek_builtin_random_randrange(snek_poly_t a)
{
random_next = random_next * 1103515245L + 12345L;
return snek_float_to_poly(random_next % (uint32_t) snek_poly_get_float(a));
}

extern char __snek_data_start__, __snek_data_end__;
extern char __snek_bss_start__, __snek_bss_end__;
extern char __text_start__, __text_end__;
Expand Down
2 changes: 0 additions & 2 deletions chips/avr/snek-avr.builtin
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@
reset, 0
time.sleep, 1
time.monotonic, 0
random.seed, 1
random.randrange, 1
2 changes: 2 additions & 0 deletions chips/avr/snek-avr.defs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ SNEK_LOCAL_VPATH = $(SNEK_PORT):$(SNEK_AVR):$(SNEK_AO)
SNEK_AVR_SRC = \
snek-io.c \
snek-pow.c \
snek-random-small.c \
snek-avr-eeprom.c \
ao-snek-avr.c \
ao-usb-avr.c \
Expand All @@ -43,6 +44,7 @@ SNEK_AVR_INC = \
SNEK_AVR_BUILTINS = \
snek-gpio.builtin \
snek-eeprom.builtin \
snek-random.builtin \
snek-avr.builtin

SNEK_NO_FILE = 1
Expand Down
2 changes: 0 additions & 2 deletions chips/qemu/snek-qemu.builtin
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# General Public License for more details.
#
exit, 1
random.seed, 1
random.randrange, 1
time.monotonic, 0
time.sleep, 1
#include <snek-qemu.h>
29 changes: 0 additions & 29 deletions chips/qemu/snek-qemu.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,6 @@ snek_builtin_exit(snek_poly_t a)
return a;
}

static uint64_t random_x, random_w;

#define random_s 0xb5ad4eceda1ce2a9ULL

snek_poly_t
snek_builtin_random_seed(snek_poly_t a)
{
random_x = a.u;
random_x |= random_x << 32;
random_w = 0;
return SNEK_NULL;
}

snek_poly_t
snek_builtin_random_randrange(snek_poly_t a)
{
uint32_t mod = snek_poly_get_float(a);

if (!mod) {
snek_error_value(a);
return SNEK_NULL;
}
random_x *= random_x;
random_w += random_s;
random_x += random_w;
random_x = (random_x >> 32) | (random_x << 32);
return snek_float_to_poly(random_x % mod);
}

static uint32_t
centisecs(void)
{
Expand Down
2 changes: 2 additions & 0 deletions chips/qemu/snek-qemu.defs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

SNEK_LOCAL_SRC = \
snek-math.c \
snek-random.c \
snek-io.c \
snek-input.c \
snek-qemu.c
Expand All @@ -24,6 +25,7 @@ SNEK_LOCAL_INC = \
SNEK_LOCAL_BUILTINS = \
snek-qemu.builtin \
snek-math.builtin \
snek-random.builtin \
snek-input.builtin

SNEK_LOCAL_VPATH = $(SNEK_QEMU)
Expand Down
2 changes: 0 additions & 2 deletions chips/samd21/snek-altos.builtin
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@ time.sleep, 1
time.monotonic, 0
pulldown, 1
reset, 0
random.seed, 1
random.randrange, 1
neopixel, 1
#include <ao-snek.h>
29 changes: 0 additions & 29 deletions chips/samd21/snek-altos.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,3 @@ snek_builtin_time_monotonic(void)
{
return snek_float_to_poly(ao_time_ns() / 1e9f);
}

static uint64_t random_x, random_w;

#define random_s 0xb5ad4eceda1ce2a9ULL

snek_poly_t
snek_builtin_random_seed(snek_poly_t a)
{
random_x = a.u;
random_x |= random_x << 32;
random_w = 0;
return SNEK_NULL;
}

snek_poly_t
snek_builtin_random_randrange(snek_poly_t a)
{
uint32_t mod = snek_poly_get_float(a);

if (!mod) {
snek_error_value(a);
return SNEK_NULL;
}
random_x *= random_x;
random_w += random_s;
random_x += random_w;
random_x = (random_x >> 32) | (random_x << 32);
return snek_float_to_poly(random_x % mod);
}
2 changes: 2 additions & 0 deletions chips/samd21/snek-samd21.defs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ SNEK_SAMD21_SRC = \
snek-altos.c \
snek-eeprom.c \
snek-math.c \
snek-random.c \
snek-gpio.c \
snek-io.c \
snek-input.c \
Expand Down Expand Up @@ -64,6 +65,7 @@ SNEK_SAMD21_BUILTINS = \
snek-eeprom.builtin \
snek-altos.builtin \
snek-math.builtin \
snek-random.builtin \
snek-input.builtin

PICOLIBC_PRINTF_CFLAGS = -DPICOLIBC_FLOAT_PRINTF_SCANF
Expand Down
7 changes: 6 additions & 1 deletion hosts/windows/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ SNEK_ROOT=../..
SNEK_LOCAL_SRC = \
snek-windows.c \
snek-math.c \
snek-random.c \
snek-input.c

SNEK_LOCAL_INC = snek-windows.h
SNEK_LOCAL_BUILTINS = snek-windows.builtin $(SNEK_ROOT)/snek-math.builtin $(SNEK_ROOT)/snek-input.builtin
SNEK_LOCAL_BUILTINS = \
snek-windows.builtin \
snek-math.builtin \
snek-random.builtin \
snek-input.builtin

INF=altusmetrum.inf
WINDOWS_FILES=snek.exe snek.ico snekde.py $(PDF) $(SNEK_ROOT)/examples $(SNEK_ROOT)/COPYING $(FIRMWARE) $(USBFIRMWARE) $(INF)
Expand Down
2 changes: 0 additions & 2 deletions hosts/windows/snek-windows.builtin
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@
exit, 1
time.sleep, 1
time.monotonic, 0
random.seed, 1
random.randrange, 1
#include "snek-windows.h"
#define SNEK_POOL 262144
14 changes: 0 additions & 14 deletions hosts/windows/snek-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,3 @@ snek_builtin_time_monotonic(void)
{
return snek_float_to_poly(GetTickCount() / 1000.0f);
}

snek_poly_t
snek_builtin_random_seed(snek_poly_t a)
{
srand(a.u);
return SNEK_NULL;
}

snek_poly_t
snek_builtin_random_randrange(snek_poly_t a)
{
return snek_float_to_poly(rand() % (long int) snek_poly_get_float(a));
}

6 changes: 4 additions & 2 deletions ports/esp32/main/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SNEK_ROOT=../../..
SNEK_LOCAL_SRC = \
snek-main.c \
snek-math.c \
snek-random.c \
snek-input.c \
snek-esp32.c \
snek-io.c
Expand All @@ -26,8 +27,9 @@ SNEK_LOCAL_INC = \

SNEK_LOCAL_BUILTINS = \
snek-esp32.builtin \
$(SNEK_ROOT)/snek-math.builtin \
$(SNEK_ROOT)/snek-input.builtin
snek-math.builtin \
snek-random.builtin \
snek-input.builtin

include $(SNEK_ROOT)/snek.defs

Expand Down
2 changes: 0 additions & 2 deletions ports/esp32/main/snek-esp32.builtin
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
reset, 0
time.sleep, 1
time.monotonic, 0
random.seed, 1
random.randrange, 1
#include "snek-esp32.h"
29 changes: 0 additions & 29 deletions ports/esp32/main/snek-esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,3 @@ snek_builtin_time_monotonic(void)
TickType_t ticks = xTaskGetTickCount();
return snek_float_to_poly((float) ticks * (portTICK_PERIOD_MS / 1000.0f));
}

static uint64_t random_x, random_w;

#define random_s 0xb5ad4eceda1ce2a9ULL

snek_poly_t
snek_builtin_random_seed(snek_poly_t a)
{
random_x = a.u;
random_x |= random_x << 32;
random_w = 0;
return SNEK_NULL;
}

snek_poly_t
snek_builtin_random_randrange(snek_poly_t a)
{
uint32_t mod = snek_poly_get_float(a);

if (!mod) {
snek_error_value(a);
return SNEK_NULL;
}
random_x *= random_x;
random_w += random_s;
random_x += random_w;
random_x = (random_x >> 32) | (random_x << 32);
return snek_float_to_poly(random_x % mod);
}
8 changes: 5 additions & 3 deletions ports/ev3/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SNEK_LOCAL_SRC = \
snek-main.c \
snek-posix.c \
snek-math.c \
snek-random.c \
snek-input.c \
snek-io.c \
sensors.c \
Expand All @@ -35,9 +36,10 @@ SNEK_LOCAL_INC = snek-ev3.h utils.h sensors.h motors.h
SNEK_LOCAL_CFLAGS = -DSNEK_USE_GLIBC_2_4_MATH
SNEK_LOCAL_BUILTINS = \
snek-ev3.builtin \
$(SNEK_ROOT)/snek-math.builtin \
$(SNEK_ROOT)/snek-eeprom.builtin \
$(SNEK_ROOT)/snek-input.builtin
snek-math.builtin \
snek-random.builtin \
snek-eeprom.builtin \
snek-input.builtin

include $(SNEK_ROOT)/snek-install.defs

Expand Down
2 changes: 0 additions & 2 deletions ports/ev3/snek-ev3.builtin
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
exit, 1
time.sleep, 1
time.monotonic, 0
random.seed, 1
random.randrange, 1

# light sensor mode
light_reflected, 1
Expand Down
2 changes: 2 additions & 0 deletions ports/hifive1revb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ SNEK_LOCAL_SRC = \
watchdog.c \
snek-metal.c \
snek-math.c \
snek-random.c \
snek-io.c \
snek-input.c \
snek-metal-gpio.c \
Expand All @@ -138,6 +139,7 @@ SNEK_LOCAL_INC = \

SNEK_LOCAL_BUILTINS = \
snek-math.builtin \
snek-random.builtin \
snek-gpio.builtin \
snek-input.builtin \
snek-hifive1revb.builtin
Expand Down
2 changes: 0 additions & 2 deletions ports/hifive1revb/snek-hifive1revb.builtin
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,4 @@ SDA, -2, 20
SCL, -2, 21
time.sleep, 1
time.monotonic, 0
random.seed, 1
random.randrange, 1
reset, 0
29 changes: 0 additions & 29 deletions ports/hifive1revb/snek-metal-builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,6 @@ snek_builtin_time_monotonic(void)
return snek_float_to_poly(snek_millis() / 1000.0f);
}

static uint64_t random_x, random_w;

#define random_s 0xb5ad4eceda1ce2a9ULL

snek_poly_t
snek_builtin_random_seed(snek_poly_t a)
{
random_x = a.u;
random_x |= random_x << 32;
random_w = 0;
return SNEK_NULL;
}

snek_poly_t
snek_builtin_random_randrange(snek_poly_t a)
{
uint32_t mod = snek_poly_get_float(a);

if (!mod) {
snek_error_value(a);
return SNEK_NULL;
}
random_x *= random_x;
random_w += random_s;
random_x += random_w;
random_x = (random_x >> 32) | (random_x << 32);
return snek_float_to_poly(random_x % mod);
}

snek_poly_t
snek_builtin_reset(void)
{
Expand Down
Loading

0 comments on commit bd9e863

Please sign in to comment.