From d5dd86ed24d0e2a2eb8fb62e0d5520666d0e9daa Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 27 Nov 2020 15:19:01 +0000 Subject: [PATCH] Update to 2.0! --- Makefile | 75 ++-- README.md | 11 +- TODO | 1 - lean/TODO.org => TODO.org | 0 build/.gitkeep | 0 {lean/include => include}/colours.h | 0 {lean/include => include}/debug.h | 0 {lean/include => include}/map.h | 0 {lean/include => include}/panic.h | 0 {lean/include => include}/reinterpret.h | 0 {lean/include => include}/reinterpret.hpp | 0 include/rng.h | 74 +++- {lean/include => include}/rng/drng.hpp | 0 {lean/include => include}/rng/frng.hpp | 0 {lean/include => include}/rng/impl.hpp | 0 .../rng/xoroshiro128plus.hpp | 0 {lean/include => include}/shuffle.hpp | 0 {lean/include => include}/shuffle3.h | 0 {lean/include => include}/work.h | 0 lean/Makefile | 73 ---- lean/include/rng.h | 59 --- lean/src/main.c | 78 ---- old/Makefile | 50 +++ {include => old/include}/array.h | 0 {include => old/include}/ptr_store.h | 0 old/include/rng.h | 25 ++ {include => old/include}/rng_algos.h | 0 {include => old/include}/rng_impl.h | 0 {src => old/src}/array.c | 0 {src => old/src}/drng.c | 0 {src => old/src}/frng.c | 0 old/src/main.c | 373 ++++++++++++++++ {src => old/src}/ptr_store.c | 0 {src => old/src}/rng.c | 0 {src => old/src}/xoroshiro.c | 0 {lean/src => src}/debug.c | 0 src/main.c | 399 +++--------------- {lean/src => src}/map.c | 0 {lean/src => src}/map_callback.cpp | 0 {lean/src => src}/panic.c | 0 {lean/src => src}/reinterpret.cpp | 0 {lean/src => src}/rng.cpp | 0 {lean/src => src}/rng/drng.cpp | 0 {lean/src => src}/rng/frng.cpp | 0 {lean/src => src}/rng/test.cpp | 0 {lean/src => src}/rng/xoroshiro128plus.cpp | 0 {lean/src => src}/work.cpp | 0 47 files changed, 610 insertions(+), 608 deletions(-) delete mode 100644 TODO rename lean/TODO.org => TODO.org (100%) delete mode 100644 build/.gitkeep rename {lean/include => include}/colours.h (100%) rename {lean/include => include}/debug.h (100%) rename {lean/include => include}/map.h (100%) rename {lean/include => include}/panic.h (100%) rename {lean/include => include}/reinterpret.h (100%) rename {lean/include => include}/reinterpret.hpp (100%) rename {lean/include => include}/rng/drng.hpp (100%) rename {lean/include => include}/rng/frng.hpp (100%) rename {lean/include => include}/rng/impl.hpp (100%) rename {lean/include => include}/rng/xoroshiro128plus.hpp (100%) rename {lean/include => include}/shuffle.hpp (100%) rename {lean/include => include}/shuffle3.h (100%) rename {lean/include => include}/work.h (100%) delete mode 100644 lean/Makefile delete mode 100644 lean/include/rng.h delete mode 100644 lean/src/main.c create mode 100644 old/Makefile rename {include => old/include}/array.h (100%) rename {include => old/include}/ptr_store.h (100%) create mode 100644 old/include/rng.h rename {include => old/include}/rng_algos.h (100%) rename {include => old/include}/rng_impl.h (100%) rename {src => old/src}/array.c (100%) rename {src => old/src}/drng.c (100%) rename {src => old/src}/frng.c (100%) create mode 100644 old/src/main.c rename {src => old/src}/ptr_store.c (100%) rename {src => old/src}/rng.c (100%) rename {src => old/src}/xoroshiro.c (100%) rename {lean/src => src}/debug.c (100%) rename {lean/src => src}/map.c (100%) rename {lean/src => src}/map_callback.cpp (100%) rename {lean/src => src}/panic.c (100%) rename {lean/src => src}/reinterpret.cpp (100%) rename {lean/src => src}/rng.cpp (100%) rename {lean/src => src}/rng/drng.cpp (100%) rename {lean/src => src}/rng/frng.cpp (100%) rename {lean/src => src}/rng/test.cpp (100%) rename {lean/src => src}/rng/xoroshiro128plus.cpp (100%) rename {lean/src => src}/work.cpp (100%) diff --git a/Makefile b/Makefile index 343642d..5a65e66 100644 --- a/Makefile +++ b/Makefile @@ -1,50 +1,73 @@ -SRC = $(wildcard src/*.c) -INCLUDE = include/ +SRC_C = $(wildcard src/*.c) +SRC_CXX = $(wildcard src/*.cpp) +SRC_CXX+= $(wildcard src/rng/*.cpp) + +INCLUDE = include PROJECT=shuffle3 -BUILD=build +COMMON_FLAGS = -Wall -pedantic $(addprefix -I,$(INCLUDE)) -fno-strict-aliasing OPT_FLAGS?= -march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ - -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block - -CFLAGS+= $(addprefix -I,$(INCLUDE)) -Wall -pedantic --std=gnu11 -LDFLAGS+= -lm + -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ + -fno-stack-check -RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) -RELEASE_LDFLAGS?= -O3 -flto +CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors -DEBUG_CFLAGS?= -g -O0 -DEBUG_LDFLAGS?= -O0 +CFLAGS += $(COMMON_FLAGS) --std=gnu11 +CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20 -fno-exceptions +LDFLAGS += -lfmt STRIP=strip -OBJ = $(addprefix obj/,$(SRC:.c=.o)) +RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) +RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS) +RELEASE_LDFLAGS?= -O3 -flto + +DEBUG_CFLAGS?= -O0 -g -DDEBUG +DEBUG_CXXFLAGS?= $(DEBUG_CFLAGS) +DEBUG_LDFLAGS?= + +# Objects + +OBJ_C = $(addprefix obj/c/,$(SRC_C:.c=.o)) +OBJ_CXX = $(addprefix obj/cxx/,$(SRC_CXX:.cpp=.o)) +OBJ = $(OBJ_C) $(OBJ_CXX) + +# Phonies .PHONY: release -release: | dirs $(BUILD)/$(PROJECT)-release +release: | dirs $(PROJECT)-release .PHONY: debug -debug: | dirs $(BUILD)/$(PROJECT)-debug +debug: | dirs $(PROJECT)-debug + +# Targets dirs: - @mkdir -p obj/src - @mkdir -p $(BUILD) + @mkdir -p obj/c{,xx}/src + @mkdir -p obj/cxx/src/rng -obj/%.o: %.c +obj/c/%.o: %.c $(CC) -c $< $(CFLAGS) -o $@ $(LDFLAGS) -$(BUILD)/$(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS) -$(BUILD)/$(PROJECT)-release: LDFLAGS+= $(RELEASE_LDFLAGS) -$(BUILD)/$(PROJECT)-release: $(OBJ) - $(CC) $^ $(CFLAGS) -o $@ $(LDFLAGS) +obj/cxx/%.o: %.cpp + $(CXX) -c $< $(CXXFLAGS) -o $@ $(LDFLAGS) + + +$(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS) +$(PROJECT)-release: CXXFLAGS += $(RELEASE_CXXFLAGS) +$(PROJECT)-release: LDFLAGS += $(RELEASE_LDFLAGS) +$(PROJECT)-release: $(OBJ) + $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) $(STRIP) $@ -$(BUILD)/$(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS) -$(BUILD)/$(PROJECT)-debug: LDFLAGS+= $(DEBUG_LDFLAGS) -$(BUILD)/$(PROJECT)-debug: $(OBJ) - $(CC) $^ $(CFLAGS) -o $@ $(LDFLAGS) +$(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS) +$(PROJECT)-debug: CXXFLAGS += $(DEBUG_CXXFLAGS) +$(PROJECT)-debug: LDFLAGS += $(DEBUG_LDFLAGS) +$(PROJECT)-debug: $(OBJ) + $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) clean: rm -rf obj - rm -f $(BUILD)/* + rm -f $(PROJECT)-{release,debug} diff --git a/README.md b/README.md index 7ecbad1..64d7007 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# shuffle3 - 3 stage byte shuffler +# `shuffle3-lean` - Improved 3 stage byte shuffler Deterministically and reversably shuffle a file's bytes around. @@ -17,24 +17,25 @@ $ shuffle3 -u file ``` ## Other options -Run with `--help` for more options. +Run with `-h` for more options. # Building Run `make` to build the normal binary. It will output to `shuffle3-release`. ## Release target -The `release` (default) target uses the variables `RELEASE_CFLAGS` and `RELEASE_LDFLAGS` to specify opitimisations. These can be set by you if you wish. +The `release` (default) target uses the variables `RELEASE_CFLAGS`, `RELEASE_CXXFLAGS` and `RELEASE_LDFLAGS` to specify opitimisations, as well as the `OPT_FLAGS` variable. These can be set by you if you wish. ### Note -The default `RELEASE_CFLAGS` contains the flag `-march=native`. This may be underisable for you, in which case set the variable or modify the makefile to remove it. +The default `OPT_FLAGS` contains the flag `-march=native`. This may be underisable for you, in which case set the variable or modify the makefile to remove it. ## Debug target -To build with debug information, run `make debug`. Extra debug flags can be provided with the `DEBUG_CFLAGS` and `DEBUG_LDFLAGS` variables which have default values in the Makefile. +To build with debug information, run `make debug`. Extra debug flags can be provided with the `DEBUG_CFLAGS`, `DEBUG_CXXFLAGS` and `DEBUG_LDFLAGS` variables which have default values in the Makefile. The build and unstripped binary will be `shuffle3-debug`. ## Notes Before switching between `release` and `debug` targets, remember to run `make clean`. +To disable stripping of release build binaries, run with `make STRIP=: release` # License GPL'd with <3 diff --git a/TODO b/TODO deleted file mode 100644 index 51b0f0e..0000000 --- a/TODO +++ /dev/null @@ -1 +0,0 @@ -Complete rewrite using `mmap` et al. Maybe in Rust of C++ idk diff --git a/lean/TODO.org b/TODO.org similarity index 100% rename from lean/TODO.org rename to TODO.org diff --git a/build/.gitkeep b/build/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/lean/include/colours.h b/include/colours.h similarity index 100% rename from lean/include/colours.h rename to include/colours.h diff --git a/lean/include/debug.h b/include/debug.h similarity index 100% rename from lean/include/debug.h rename to include/debug.h diff --git a/lean/include/map.h b/include/map.h similarity index 100% rename from lean/include/map.h rename to include/map.h diff --git a/lean/include/panic.h b/include/panic.h similarity index 100% rename from lean/include/panic.h rename to include/panic.h diff --git a/lean/include/reinterpret.h b/include/reinterpret.h similarity index 100% rename from lean/include/reinterpret.h rename to include/reinterpret.h diff --git a/lean/include/reinterpret.hpp b/include/reinterpret.hpp similarity index 100% rename from lean/include/reinterpret.hpp rename to include/reinterpret.hpp diff --git a/include/rng.h b/include/rng.h index e92830e..4a19152 100644 --- a/include/rng.h +++ b/include/rng.h @@ -1,25 +1,59 @@ #ifndef _RNG_H #define _RNG_H -typedef struct rng_algo *RNG; //RNG algorithm reference - -#define rng_reinterpret(rng, value, type) (*((type*)rng_next(rng, &value, sizeof(type)))) -#define rng_reinterpret_new(rng, value, type) (*((type*)rng_next(rng, malloc(sizeof(type)), sizeof(type)))) -#define rng_reinterpret_stackalloc(rng, value, type) rng_reinterpret(rng, alloca(sizeof(type)), type) - -#define RNG_LEXBIND(rng, type, name) type name = rng_reinterpret(rng, name, type) - -double rng_next_double(RNG algo); -int rng_next_int_bounded(RNG algo, int min, int max); -int rng_next_int(RNG algo, int max); -void* rng_next(RNG algo, void* data, int len); -int rng_chance(RNG algo, double d); -void rng_free(RNG algo); -void rng_seed(RNG algo, void* seed); -RNG rng_new(RNG (*instantiate)(void)); - -#define RNG_IMPL_DEFINITION(name) RNG __rng_impl_ ## name(void) -#define RNG_ALGO(name) &__rng_impl_ ## name -#define RNG_NEW(name) rng_new(RNG_ALGO(name)) +#include "shuffle3.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum rng_kind { + RNG_KIND_FRNG, + RNG_KIND_DRNG, + RNG_KIND_XORNG, +}; + +typedef struct rng_init +{ + enum rng_kind kind; + union { + struct { + double state[2]; + } frng; + struct { + int32_t state; + } drng; + struct { + uint64_t state[2]; + } xorng; + } init; + +} rng_init_opt; + +typedef struct rng_impl* _UNIQUE rng_t; + +rng_t rng_new(rng_init_opt kind); +#define RNG_INIT(_kind,...) ((rng_init_opt){.kind=(_kind), .init.__VA_ARGS__ }) +void rng_free(rng_t ptr); + +// Tests +extern void rng_test(); +extern void rng_test_spec(rng_t rng); + +#ifdef __cplusplus +} +// RNG interfaces +#include +#include +#include + +namespace rng { + void test_algo(RNG&& rng); + +template::value>::value __fuck> + inline void test_algo(R&& rng) { test_algo(static_cast(rng)); } +} +#endif #endif /* _RNG_H */ diff --git a/lean/include/rng/drng.hpp b/include/rng/drng.hpp similarity index 100% rename from lean/include/rng/drng.hpp rename to include/rng/drng.hpp diff --git a/lean/include/rng/frng.hpp b/include/rng/frng.hpp similarity index 100% rename from lean/include/rng/frng.hpp rename to include/rng/frng.hpp diff --git a/lean/include/rng/impl.hpp b/include/rng/impl.hpp similarity index 100% rename from lean/include/rng/impl.hpp rename to include/rng/impl.hpp diff --git a/lean/include/rng/xoroshiro128plus.hpp b/include/rng/xoroshiro128plus.hpp similarity index 100% rename from lean/include/rng/xoroshiro128plus.hpp rename to include/rng/xoroshiro128plus.hpp diff --git a/lean/include/shuffle.hpp b/include/shuffle.hpp similarity index 100% rename from lean/include/shuffle.hpp rename to include/shuffle.hpp diff --git a/lean/include/shuffle3.h b/include/shuffle3.h similarity index 100% rename from lean/include/shuffle3.h rename to include/shuffle3.h diff --git a/lean/include/work.h b/include/work.h similarity index 100% rename from lean/include/work.h rename to include/work.h diff --git a/lean/Makefile b/lean/Makefile deleted file mode 100644 index 5a65e66..0000000 --- a/lean/Makefile +++ /dev/null @@ -1,73 +0,0 @@ -SRC_C = $(wildcard src/*.c) -SRC_CXX = $(wildcard src/*.cpp) -SRC_CXX+= $(wildcard src/rng/*.cpp) - -INCLUDE = include - -PROJECT=shuffle3 - -COMMON_FLAGS = -Wall -pedantic $(addprefix -I,$(INCLUDE)) -fno-strict-aliasing - -OPT_FLAGS?= -march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ - -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ - -fno-stack-check - -CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors - -CFLAGS += $(COMMON_FLAGS) --std=gnu11 -CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20 -fno-exceptions -LDFLAGS += -lfmt - -STRIP=strip - -RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) -RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS) -RELEASE_LDFLAGS?= -O3 -flto - -DEBUG_CFLAGS?= -O0 -g -DDEBUG -DEBUG_CXXFLAGS?= $(DEBUG_CFLAGS) -DEBUG_LDFLAGS?= - -# Objects - -OBJ_C = $(addprefix obj/c/,$(SRC_C:.c=.o)) -OBJ_CXX = $(addprefix obj/cxx/,$(SRC_CXX:.cpp=.o)) -OBJ = $(OBJ_C) $(OBJ_CXX) - -# Phonies - -.PHONY: release -release: | dirs $(PROJECT)-release - -.PHONY: debug -debug: | dirs $(PROJECT)-debug - -# Targets - -dirs: - @mkdir -p obj/c{,xx}/src - @mkdir -p obj/cxx/src/rng - -obj/c/%.o: %.c - $(CC) -c $< $(CFLAGS) -o $@ $(LDFLAGS) - -obj/cxx/%.o: %.cpp - $(CXX) -c $< $(CXXFLAGS) -o $@ $(LDFLAGS) - - -$(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS) -$(PROJECT)-release: CXXFLAGS += $(RELEASE_CXXFLAGS) -$(PROJECT)-release: LDFLAGS += $(RELEASE_LDFLAGS) -$(PROJECT)-release: $(OBJ) - $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) - $(STRIP) $@ - -$(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS) -$(PROJECT)-debug: CXXFLAGS += $(DEBUG_CXXFLAGS) -$(PROJECT)-debug: LDFLAGS += $(DEBUG_LDFLAGS) -$(PROJECT)-debug: $(OBJ) - $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) - -clean: - rm -rf obj - rm -f $(PROJECT)-{release,debug} diff --git a/lean/include/rng.h b/lean/include/rng.h deleted file mode 100644 index 4a19152..0000000 --- a/lean/include/rng.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef _RNG_H -#define _RNG_H - -#include "shuffle3.h" - -#ifdef __cplusplus -extern "C" { -#endif - -enum rng_kind { - RNG_KIND_FRNG, - RNG_KIND_DRNG, - RNG_KIND_XORNG, -}; - -typedef struct rng_init -{ - enum rng_kind kind; - union { - struct { - double state[2]; - } frng; - struct { - int32_t state; - } drng; - struct { - uint64_t state[2]; - } xorng; - } init; - -} rng_init_opt; - -typedef struct rng_impl* _UNIQUE rng_t; - -rng_t rng_new(rng_init_opt kind); -#define RNG_INIT(_kind,...) ((rng_init_opt){.kind=(_kind), .init.__VA_ARGS__ }) -void rng_free(rng_t ptr); - -// Tests -extern void rng_test(); -extern void rng_test_spec(rng_t rng); - -#ifdef __cplusplus -} -// RNG interfaces -#include -#include -#include - -namespace rng { - void test_algo(RNG&& rng); - -template::value>::value __fuck> - inline void test_algo(R&& rng) { test_algo(static_cast(rng)); } -} -#endif - -#endif /* _RNG_H */ diff --git a/lean/src/main.c b/lean/src/main.c deleted file mode 100644 index 7e5e736..0000000 --- a/lean/src/main.c +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include - -#include - - -#include -#include -#include -#include -#include -#include - -#include - -#define noreturn __attribute__((noreturn)) void - -_Static_assert(sizeof(float)==sizeof(uint32_t), "float is not 32 bits"); - -const char* _prog_name; - -noreturn help_then_exit() -{ - fprintf(stderr, "Try passing `-h`\n"); - exit(1); -} - -void usage() -{ - printf( "shuffle3 - 3 pass binary shuffler\n" - "Usage: %s -s \n" - "Usage: %s -u \n", _prog_name, _prog_name); - printf("\nOPTIONS:\n" - " -s\tShuffle file in place\n" - " -u\tUnshuffle file in place\n"); -} - -int main(int argc, char** argv) -{ - _prog_name = argv[0]; - - work_args_t parsed; - - if( !argv[1] || *(argv[1]) != '-') help_then_exit(); - - D_dprintf("Parsing `%c'", argv[1][1]); - switch(argv[1][1]) - { - case 's': - parsed.op = OP_SHUFFLE_IP; - if(!(parsed.data.op_shuffle_ip.file = argv[2])) - { - fprintf(stderr, "Error: -s expected file argument.\n"); - return 1; - } - D_dprintf("parsed.op = %d", OP_SHUFFLE_IP); - break; - case 'u': - parsed.op = OP_UNSHUFFLE_IP; - if(!(parsed.data.op_unshuffle_ip.file = argv[2])) - { - fprintf(stderr, "Error: -u expected file argument.\n"); - return 1; - } - D_dprintf("parsed.op = %d", OP_UNSHUFFLE_IP); - break; - case 'h': - usage(); - return 0; - default: - fprintf(stderr, "Error: unknown argument `%s'\n\n", argv[1]); - help_then_exit(); - panic("Unreachable"); - } - - return do_work(parsed); -} - diff --git a/old/Makefile b/old/Makefile new file mode 100644 index 0000000..343642d --- /dev/null +++ b/old/Makefile @@ -0,0 +1,50 @@ +SRC = $(wildcard src/*.c) +INCLUDE = include/ + +PROJECT=shuffle3 + +BUILD=build + +OPT_FLAGS?= -march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ + -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block + +CFLAGS+= $(addprefix -I,$(INCLUDE)) -Wall -pedantic --std=gnu11 +LDFLAGS+= -lm + +RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) +RELEASE_LDFLAGS?= -O3 -flto + +DEBUG_CFLAGS?= -g -O0 +DEBUG_LDFLAGS?= -O0 + +STRIP=strip + +OBJ = $(addprefix obj/,$(SRC:.c=.o)) + +.PHONY: release +release: | dirs $(BUILD)/$(PROJECT)-release + +.PHONY: debug +debug: | dirs $(BUILD)/$(PROJECT)-debug + +dirs: + @mkdir -p obj/src + @mkdir -p $(BUILD) + +obj/%.o: %.c + $(CC) -c $< $(CFLAGS) -o $@ $(LDFLAGS) + +$(BUILD)/$(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS) +$(BUILD)/$(PROJECT)-release: LDFLAGS+= $(RELEASE_LDFLAGS) +$(BUILD)/$(PROJECT)-release: $(OBJ) + $(CC) $^ $(CFLAGS) -o $@ $(LDFLAGS) + $(STRIP) $@ + +$(BUILD)/$(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS) +$(BUILD)/$(PROJECT)-debug: LDFLAGS+= $(DEBUG_LDFLAGS) +$(BUILD)/$(PROJECT)-debug: $(OBJ) + $(CC) $^ $(CFLAGS) -o $@ $(LDFLAGS) + +clean: + rm -rf obj + rm -f $(BUILD)/* diff --git a/include/array.h b/old/include/array.h similarity index 100% rename from include/array.h rename to old/include/array.h diff --git a/include/ptr_store.h b/old/include/ptr_store.h similarity index 100% rename from include/ptr_store.h rename to old/include/ptr_store.h diff --git a/old/include/rng.h b/old/include/rng.h new file mode 100644 index 0000000..e92830e --- /dev/null +++ b/old/include/rng.h @@ -0,0 +1,25 @@ +#ifndef _RNG_H +#define _RNG_H + +typedef struct rng_algo *RNG; //RNG algorithm reference + +#define rng_reinterpret(rng, value, type) (*((type*)rng_next(rng, &value, sizeof(type)))) +#define rng_reinterpret_new(rng, value, type) (*((type*)rng_next(rng, malloc(sizeof(type)), sizeof(type)))) +#define rng_reinterpret_stackalloc(rng, value, type) rng_reinterpret(rng, alloca(sizeof(type)), type) + +#define RNG_LEXBIND(rng, type, name) type name = rng_reinterpret(rng, name, type) + +double rng_next_double(RNG algo); +int rng_next_int_bounded(RNG algo, int min, int max); +int rng_next_int(RNG algo, int max); +void* rng_next(RNG algo, void* data, int len); +int rng_chance(RNG algo, double d); +void rng_free(RNG algo); +void rng_seed(RNG algo, void* seed); +RNG rng_new(RNG (*instantiate)(void)); + +#define RNG_IMPL_DEFINITION(name) RNG __rng_impl_ ## name(void) +#define RNG_ALGO(name) &__rng_impl_ ## name +#define RNG_NEW(name) rng_new(RNG_ALGO(name)) + +#endif /* _RNG_H */ diff --git a/include/rng_algos.h b/old/include/rng_algos.h similarity index 100% rename from include/rng_algos.h rename to old/include/rng_algos.h diff --git a/include/rng_impl.h b/old/include/rng_impl.h similarity index 100% rename from include/rng_impl.h rename to old/include/rng_impl.h diff --git a/src/array.c b/old/src/array.c similarity index 100% rename from src/array.c rename to old/src/array.c diff --git a/src/drng.c b/old/src/drng.c similarity index 100% rename from src/drng.c rename to old/src/drng.c diff --git a/src/frng.c b/old/src/frng.c similarity index 100% rename from src/frng.c rename to old/src/frng.c diff --git a/old/src/main.c b/old/src/main.c new file mode 100644 index 0000000..dfc3d24 --- /dev/null +++ b/old/src/main.c @@ -0,0 +1,373 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +_Static_assert(sizeof(float)==4, "Float must be 32 bits."); + +int surpress_out=0; + +/*void test_destructor(object* ptr) +{ + printf("die\n"); +}*/ +void _rng_object_destructor(object* obj) +{ + rng_free(obj->state); +} +RNG rng_object(S_LEXENV, RNG rng) +{ + object proto = OBJ_PROTO; + proto.state = rng; + proto.destructor = &_rng_object_destructor; + + return snew_obj(proto)->state; +} + +void shuffle(RNG with, array_t data) +{ + if(!surpress_out) printf(" -> shuffling %d objects...", (int)ar_size(data)); + for(int i=ar_size(data)-1;i>0;i--) + { + int j = rng_next_int(with, i); + ar_swap(data, i, j); + } + if(!surpress_out) printf(" Okay\n"); +} + +void unshuffle(RNG with, array_t data) +{ + int rng_values[ar_size(data)-1]; + int k=0; + if(!surpress_out) printf(" -> unshuffling %d objects...", (int)ar_size(data)); + for(int i=ar_size(data)-1;i>0;i--) + rng_values[k++] = rng_next_int(with, i); + + for(int i=1;i *max) *max = ar_get_v(data, int64_t, i); + if(ar_get_v(data, int64_t, i) < *min) *min = ar_get_v(data, int64_t, i); + } +} +int valid_float(float f) +{ + return !( (f!=f) || (f< -FLT_MAX || f> FLT_MAX)); + +} +void minmax_floats(float* min, float* max, const array_t data) +{ + for(register int i=0;i *max) *max = ar_get_v(data, float, i); + if(ar_get_v(data, float, i) < *min) *min = ar_get_v(data, float, i); + } +} + +void minmax_sbytes(int8_t* min, int8_t* max, const array_t data) +{ + for(register int i=0;i *max) *max = ar_get_v(data, int8_t, i); + if(ar_get_v(data, int8_t, i) < *min) *min = ar_get_v(data, int8_t, i); + } +} + +void print_array(const array_t data) +{ + printf("---%d elements---\n", (int)ar_size(data)); + for(register int i=0;i []\nUsage: %s -[b]u []\n", argv[0], argv[0]); + printf("Usage: %s -S |-\nUsage: %s -U |-\n", argv[0], argv[0]); + printf(" \t\tFile to shuffle\n"); + printf(" \tOutput file (only valid for buffered mode)\n"); + printf(" \tThe string to shuffle (for -(S|U)). If `-`, read from stdin\n"); + printf("\ts\tShuffle\n"); + printf("\tu\tReverse shuffle\n"); + printf("\tS\tShuffle string\n"); + printf("\tU\tReverse shuffle string\n"); + printf("\n\tb\tUse buffered mode instead of shuffling in place (must specify output file)\n"); +} + +array_t read_whole_file(S_NAMED_LEXENV(base), FILE* fp) +{ + fseek(fp,0,SEEK_END); + int64_t sz = ftell(fp); + fseek(fp,0,SEEK_SET); + + array_t ar; + MANAGED({ + void* buf = smalloc(sz); + + register size_t read; + register size_t full=0; + while ((read=fread(buf, 1, sz-full, fp))>0) + full+=read; + + assert(full == sz); + + ar = ar_create_memory_from(base, buf, 1, sz); + }); + return ar; +} + +#define BUFSIZE 512 +#define BUFMIN 128 + +char* read_stdin() +{ + char* input, *p; + int len,remain,n,size; + + size = BUFSIZE; + input = malloc(size); + memset(input,0,size); + len=0; + remain = size; + + while(!feof(stdin)) + { + if(remain<= BUFMIN) + { + remain+=size; + size *=2; + p = realloc(input, size); + if(p==NULL) { + free(input); + return NULL; + } + input = p; + } + fgets(input+len, remain, stdin); + n+=strlen(input+len); + len+=n; + remain-=n; + } + return input; +} + +int string_shuffle(char* string, int un) +{ + if(!string) + { + char* ptr = read_stdin(); + if(!ptr) { + printf("! read from stdin failed\n"); + return -1; + } + int ret = string_shuffle(ptr, un); + free(ptr); + return ret; + } + else if(strcmp(string, "-") == 0) + { + return string_shuffle(NULL, un); + } + else { + surpress_out=1; + MANAGED({ + array_t ar = ar_create_memory_from(LEXENV, string, 1, strlen(string)); + + if(un) unshuffle3(LEXENV, ar); + else shuffle3(LEXENV, ar); + + ar_reinterpret(ar, sizeof(char)); + + for(int i=0;i #include -#include -#include + #include -#include + +#include +#include +#include +#include #include -#include -#include -#include +#include -_Static_assert(sizeof(float)==4, "Float must be 32 bits."); +#include -int surpress_out=0; +#define noreturn __attribute__((noreturn)) void -/*void test_destructor(object* ptr) -{ - printf("die\n"); -}*/ -void _rng_object_destructor(object* obj) -{ - rng_free(obj->state); -} -RNG rng_object(S_LEXENV, RNG rng) -{ - object proto = OBJ_PROTO; - proto.state = rng; - proto.destructor = &_rng_object_destructor; +_Static_assert(sizeof(float)==sizeof(uint32_t), "float is not 32 bits"); - return snew_obj(proto)->state; -} +const char* _prog_name; -void shuffle(RNG with, array_t data) +noreturn help_then_exit() { - if(!surpress_out) printf(" -> shuffling %d objects...", (int)ar_size(data)); - for(int i=ar_size(data)-1;i>0;i--) - { - int j = rng_next_int(with, i); - ar_swap(data, i, j); - } - if(!surpress_out) printf(" Okay\n"); + fprintf(stderr, "Try passing `-h`\n"); + exit(1); } -void unshuffle(RNG with, array_t data) +void usage() { - int rng_values[ar_size(data)-1]; - int k=0; - if(!surpress_out) printf(" -> unshuffling %d objects...", (int)ar_size(data)); - for(int i=ar_size(data)-1;i>0;i--) - rng_values[k++] = rng_next_int(with, i); - - for(int i=1;i *max) *max = ar_get_v(data, int64_t, i); - if(ar_get_v(data, int64_t, i) < *min) *min = ar_get_v(data, int64_t, i); - } -} -int valid_float(float f) -{ - return !( (f!=f) || (f< -FLT_MAX || f> FLT_MAX)); - -} -void minmax_floats(float* min, float* max, const array_t data) -{ - for(register int i=0;i *max) *max = ar_get_v(data, float, i); - if(ar_get_v(data, float, i) < *min) *min = ar_get_v(data, float, i); - } -} - -void minmax_sbytes(int8_t* min, int8_t* max, const array_t data) -{ - for(register int i=0;i *max) *max = ar_get_v(data, int8_t, i); - if(ar_get_v(data, int8_t, i) < *min) *min = ar_get_v(data, int8_t, i); - } -} - -void print_array(const array_t data) -{ - printf("---%d elements---\n", (int)ar_size(data)); - for(register int i=0;i (flanchan.moe) with <3\n" + " licensed with GPL v3.0 or later\n\n" + "Usage: %s -s \n" + "Usage: %s -u \n", _prog_name, _prog_name); + printf("\nOPTIONS:\n" + " -s\tShuffle file in place\n" + " -u\tUnshuffle file in place\n"); } -void print_usage(char** argv) +int main(int argc, char** argv) { - printf("Usage: %s -[b]s []\nUsage: %s -[b]u []\n", argv[0], argv[0]); - printf("Usage: %s -S |-\nUsage: %s -U |-\n", argv[0], argv[0]); - printf(" \t\tFile to shuffle\n"); - printf(" \tOutput file (only valid for buffered mode)\n"); - printf(" \tThe string to shuffle (for -(S|U)). If `-`, read from stdin\n"); - printf("\ts\tShuffle\n"); - printf("\tu\tReverse shuffle\n"); - printf("\tS\tShuffle string\n"); - printf("\tU\tReverse shuffle string\n"); - printf("\n\tb\tUse buffered mode instead of shuffling in place (must specify output file)\n"); -} + _prog_name = argv[0]; -array_t read_whole_file(S_NAMED_LEXENV(base), FILE* fp) -{ - fseek(fp,0,SEEK_END); - int64_t sz = ftell(fp); - fseek(fp,0,SEEK_SET); + work_args_t parsed; - array_t ar; - MANAGED({ - void* buf = smalloc(sz); - - register size_t read; - register size_t full=0; - while ((read=fread(buf, 1, sz-full, fp))>0) - full+=read; - - assert(full == sz); + if( !argv[1] || *(argv[1]) != '-') help_then_exit(); - ar = ar_create_memory_from(base, buf, 1, sz); - }); - return ar; -} - -#define BUFSIZE 512 -#define BUFMIN 128 - -char* read_stdin() -{ - char* input, *p; - int len,remain,n,size; - - size = BUFSIZE; - input = malloc(size); - memset(input,0,size); - len=0; - remain = size; - - while(!feof(stdin)) + D_dprintf("Parsing `%c'", argv[1][1]); + switch(argv[1][1]) { - if(remain<= BUFMIN) - { - remain+=size; - size *=2; - p = realloc(input, size); - if(p==NULL) { - free(input); - return NULL; - } - input = p; - } - fgets(input+len, remain, stdin); - n+=strlen(input+len); - len+=n; - remain-=n; - } - return input; -} - -int string_shuffle(char* string, int un) -{ - if(!string) - { - char* ptr = read_stdin(); - if(!ptr) { - printf("! read from stdin failed\n"); - return -1; - } - int ret = string_shuffle(ptr, un); - free(ptr); - return ret; - } - else if(strcmp(string, "-") == 0) - { - return string_shuffle(NULL, un); - } - else { - surpress_out=1; - MANAGED({ - array_t ar = ar_create_memory_from(LEXENV, string, 1, strlen(string)); - - if(un) unshuffle3(LEXENV, ar); - else shuffle3(LEXENV, ar); - - ar_reinterpret(ar, sizeof(char)); - - for(int i=0;i