From 2ddd02ffe67be792aeddd523e5fe6fe932055a43 Mon Sep 17 00:00:00 2001 From: Gitea Date: Mon, 1 Aug 2022 18:36:14 +0300 Subject: [PATCH] Add header files; Merge separate RNG algorithm implementations into one file --- Makefile | 12 ++++++-- README.md | 2 +- src/endian/endian.c | 8 +++-- src/{rng/bbs.c => endian/endian.h} | 14 +++++++-- src/img/ppm.c | 19 +----------- src/img/ppm.h | 49 ++++++++++++++++++++++++++++++ src/rng/{xorshift.c => rng.c} | 14 ++++++++- src/rng/{lcg.c => rng.h} | 14 +++++++-- testing/test.c | 10 ++---- 9 files changed, 103 insertions(+), 39 deletions(-) rename src/{rng/bbs.c => endian/endian.h} (76%) create mode 100644 src/img/ppm.h rename src/rng/{xorshift.c => rng.c} (78%) rename src/rng/{lcg.c => rng.h} (76%) diff --git a/Makefile b/Makefile index 90ba7ac..d8bbfd8 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=gcc DEFAULTCFLAGS=-Wall -Werror -O2 SRCDIR=src -LIBNAME=auxlib +LIBNAME=auxlib.a BUILDDIR=build BINDIR=bin TESTDIR=testing @@ -14,14 +14,20 @@ lib: mv *.o $(BUILDDIR) mkdir -p $(BINDIR) - ar rcs $(BINDIR)/$(LIBNAME).a $(BUILDDIR)/*.o + ar rcs $(BINDIR)/$(LIBNAME) $(BUILDDIR)/*.o test: - $(CC) $(DEFAULTCFLAGS) $(TESTDIR)/*.c -o $(TESTDIR)/$(TESTBIN) && \ + $(CC) $(DEFAULTCFLAGS) $(TESTDIR)/$(TESTBIN).c $(SRCDIR)/*/*.c -o $(TESTDIR)/$(TESTBIN) && \ cd $(TESTDIR) && \ ./$(TESTBIN) && \ rm $(TESTBIN) +test_static: lib + $(CC) $(DEFAULTCFLAGS) $(TESTDIR)/$(TESTBIN).c $(BINDIR)/$(LIBNAME) -static -o $(TESTDIR)/$(TESTBIN) && \ + cd $(TESTDIR) && \ + ./$(TESTBIN) && \ + rm $(TESTBIN) + clear: rm -rf $(BUILDDIR) $(BINDIR) $(TESTDIR)/$(TESTBIN) \ No newline at end of file diff --git a/README.md b/README.md index f00d406..f046f78 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Either or -- Copy file(s) with needed functionality to your project +- Copy header(s) with implementation file(s) with needed functionality to your project # License Currently auxilib uses MIT license. diff --git a/src/endian/endian.c b/src/endian/endian.c index 8bdeb30..0ff334a 100644 --- a/src/endian/endian.c +++ b/src/endian/endian.c @@ -10,9 +10,9 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include "endian.h" -// Determine this machine's endianness. Returns 0 in case it is of Big-Endian type, 1 - Little-Endian +// Determines this machine's endianness. Returns 0 in case it is of Big-Endian type, 1 - Little-Endian int endianness() { // 00000000 00000001 int16_t n = 1; @@ -25,6 +25,7 @@ int endianness() { } } +// Swaps 16bit integer's endianness uint16_t swap_endian16(uint16_t num) { return (uint16_t) ( @@ -33,6 +34,7 @@ uint16_t swap_endian16(uint16_t num) { ); } +// Swaps 32bit integer's endianness uint32_t swap_endian32(uint32_t num) { return (uint32_t) ( @@ -43,7 +45,7 @@ uint32_t swap_endian32(uint32_t num) { ); } -// 00000000 01101110 11101110 11110101 00000000 01101110 11101110 11110101 +// Swaps 64bit integer's endianness uint64_t swap_endian64(uint64_t num) { return (uint64_t) ( diff --git a/src/rng/bbs.c b/src/endian/endian.h similarity index 76% rename from src/rng/bbs.c rename to src/endian/endian.h index b6e4393..a96d258 100644 --- a/src/rng/bbs.c +++ b/src/endian/endian.h @@ -12,6 +12,14 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I #include -int64_t bbs(int64_t seed) { - return seed * seed % (2503 * 3571); -} \ No newline at end of file +// Determine this machine's endianness. Returns 0 in case it is of Big-Endian type, 1 - Little-Endian +int endianness(); + +// Swaps 16bit integer's endianness +uint16_t swap_endian16(uint16_t num); + +// Swaps 32bit integer's endianness +uint32_t swap_endian32(uint32_t num); + +// Swaps 64bit integer's endianness +uint64_t swap_endian64(uint64_t num); \ No newline at end of file diff --git a/src/img/ppm.c b/src/img/ppm.c index fc90310..c373ce6 100644 --- a/src/img/ppm.c +++ b/src/img/ppm.c @@ -10,24 +10,7 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include -#include -#include - -typedef struct rgb8 { - uint8_t r; - uint8_t g; - uint8_t b; -} rgb8; - -// minimal ppm image structure -typedef struct ppm_image { - unsigned int width; - unsigned int height; - rgb8* pixels; -} ppm_image; +#include "ppm.h" // Read ppm image from file on the disk. Returns NULL if something went wrong ppm_image* read_ppm(const char* path) { diff --git a/src/img/ppm.h b/src/img/ppm.h new file mode 100644 index 0000000..2f338c9 --- /dev/null +++ b/src/img/ppm.h @@ -0,0 +1,49 @@ +/* +The MIT License (MIT) + +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + + +#include +#include +#include +#include +#include + +// 8bit Red-Green-Blue color representation +typedef struct rgb8 { + uint8_t r; + uint8_t g; + uint8_t b; +} rgb8; + +// Minimal ppm image structure +typedef struct ppm_image { + unsigned int width; + unsigned int height; + rgb8* pixels; +} ppm_image; + +// Read ppm image from file on the disk. Returns NULL if something went wrong +ppm_image* read_ppm(const char* path); + +// Write ppm image to the disk. Returns 0 if everything is alright, 1 otherwise +int write_ppm(const ppm_image* ppm, const char* path); + +// Put pixel with specified rgb8 color at x,y coordinates. Returns 0 if pixel has been replaced, 1 if +// coordinates are out of bounds +int put_pixel_at(const unsigned int x, const unsigned int y, const rgb8 color, ppm_image* ppm); + +// Get pixel color at specified coordinates. Returns a const pointer to that pixel color if +// it is present, NULL if coordinates are out of bounds +const rgb8* get_pixel_at(const unsigned int x, const unsigned int y, ppm_image* ppm); + +// Create a new ppm image with specified dimensions. All pixels are {0, 0, 0} (black) by default +ppm_image* new_ppm_image(const unsigned int width, const unsigned int height); \ No newline at end of file diff --git a/src/rng/xorshift.c b/src/rng/rng.c similarity index 78% rename from src/rng/xorshift.c rename to src/rng/rng.c index 65d8a37..920382e 100644 --- a/src/rng/xorshift.c +++ b/src/rng/rng.c @@ -10,8 +10,19 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include "rng.h" +// Implementation of BBS RNG with pre-defined variables +int64_t bbs(int64_t seed) { + return seed * seed % (2503 * 3571); +} + +// Implementation of LCG RNG with pre-defined variables +int64_t lcg(int64_t seed) { + return (8121 * seed + 28411) % 134456; +} + +// Implementation of XORshift RNG for 32bit integers uint32_t xorshift32(uint32_t seed) { seed ^= seed << 13; seed ^= seed >> 17; @@ -20,6 +31,7 @@ uint32_t xorshift32(uint32_t seed) { return seed; } +// Implementation of XORshift RNG for 64bit integers uint64_t xorshift64(uint64_t seed) { seed ^= seed << 13; seed ^= seed >> 7; diff --git a/src/rng/lcg.c b/src/rng/rng.h similarity index 76% rename from src/rng/lcg.c rename to src/rng/rng.h index 1918aa3..e7754c8 100644 --- a/src/rng/lcg.c +++ b/src/rng/rng.h @@ -12,6 +12,14 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I #include -int64_t lcg(int64_t seed) { - return (8121 * seed + 28411) % 134456; -} \ No newline at end of file +// Implementation of BBS RNG with pre-defined variables +int64_t bbs(int64_t seed); + +// Implementation of LCG RNG with pre-defined variables +int64_t lcg(int64_t seed); + +// Implementation of XORshift RNG for 32bit integers +uint32_t xorshift32(uint32_t seed); + +// Implementation of XORshift RNG for 64bit integers +uint64_t xorshift64(uint64_t seed); \ No newline at end of file diff --git a/testing/test.c b/testing/test.c index 507721d..8a094f4 100644 --- a/testing/test.c +++ b/testing/test.c @@ -17,13 +17,9 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I #include #include -#include "../src/rng/lcg.c" -#include "../src/rng/xorshift.c" -#include "../src/rng/bbs.c" - -#include "../src/img/ppm.c" - -#include "../src/endian/endian.c" +#include "../src/rng/rng.h" +#include "../src/img/ppm.h" +#include "../src/endian/endian.h" int test_rng() { lcg(76);