Browse Source

Add header files; Merge separate RNG algorithm implementations into one file

master
Gitea 2 years ago
parent
commit
2ddd02ffe6
  1. 12
      Makefile
  2. 2
      README.md
  3. 8
      src/endian/endian.c
  4. 14
      src/endian/endian.h
  5. 19
      src/img/ppm.c
  6. 49
      src/img/ppm.h
  7. 14
      src/rng/rng.c
  8. 14
      src/rng/rng.h
  9. 10
      testing/test.c

12
Makefile

@ -1,7 +1,7 @@
CC=gcc CC=gcc
DEFAULTCFLAGS=-Wall -Werror -O2 DEFAULTCFLAGS=-Wall -Werror -O2
SRCDIR=src SRCDIR=src
LIBNAME=auxlib LIBNAME=auxlib.a
BUILDDIR=build BUILDDIR=build
BINDIR=bin BINDIR=bin
TESTDIR=testing TESTDIR=testing
@ -14,14 +14,20 @@ lib:
mv *.o $(BUILDDIR) mv *.o $(BUILDDIR)
mkdir -p $(BINDIR) mkdir -p $(BINDIR)
ar rcs $(BINDIR)/$(LIBNAME).a $(BUILDDIR)/*.o ar rcs $(BINDIR)/$(LIBNAME) $(BUILDDIR)/*.o
test: test:
$(CC) $(DEFAULTCFLAGS) $(TESTDIR)/*.c -o $(TESTDIR)/$(TESTBIN) && \ $(CC) $(DEFAULTCFLAGS) $(TESTDIR)/$(TESTBIN).c $(SRCDIR)/*/*.c -o $(TESTDIR)/$(TESTBIN) && \
cd $(TESTDIR) && \ cd $(TESTDIR) && \
./$(TESTBIN) && \ ./$(TESTBIN) && \
rm $(TESTBIN) rm $(TESTBIN)
test_static: lib
$(CC) $(DEFAULTCFLAGS) $(TESTDIR)/$(TESTBIN).c $(BINDIR)/$(LIBNAME) -static -o $(TESTDIR)/$(TESTBIN) && \
cd $(TESTDIR) && \
./$(TESTBIN) && \
rm $(TESTBIN)
clear: clear:
rm -rf $(BUILDDIR) $(BINDIR) $(TESTDIR)/$(TESTBIN) rm -rf $(BUILDDIR) $(BINDIR) $(TESTDIR)/$(TESTBIN)

2
README.md

@ -12,7 +12,7 @@ Either
or or
- Copy file(s) with needed functionality to your project - Copy header(s) with implementation file(s) with needed functionality to your project
# License # License
Currently auxilib uses MIT license. Currently auxilib uses MIT license.

8
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. 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 <stdint.h> #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() { int endianness() {
// 00000000 00000001 // 00000000 00000001
int16_t n = 1; int16_t n = 1;
@ -25,6 +25,7 @@ int endianness() {
} }
} }
// Swaps 16bit integer's endianness
uint16_t swap_endian16(uint16_t num) { uint16_t swap_endian16(uint16_t num) {
return (uint16_t) 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) { uint32_t swap_endian32(uint32_t num) {
return (uint32_t) 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) { uint64_t swap_endian64(uint64_t num) {
return (uint64_t) return (uint64_t)
( (

14
src/rng/bbs.c → src/endian/endian.h

@ -12,6 +12,14 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include <stdint.h> #include <stdint.h>
int64_t bbs(int64_t seed) { // Determine this machine's endianness. Returns 0 in case it is of Big-Endian type, 1 - Little-Endian
return seed * seed % (2503 * 3571); 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);

19
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. 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 <stdio.h> #include "ppm.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
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 // Read ppm image from file on the disk. Returns NULL if something went wrong
ppm_image* read_ppm(const char* path) { ppm_image* read_ppm(const char* path) {

49
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
// 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);

14
src/rng/xorshift.c → 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. 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 <stdint.h> #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) { uint32_t xorshift32(uint32_t seed) {
seed ^= seed << 13; seed ^= seed << 13;
seed ^= seed >> 17; seed ^= seed >> 17;
@ -20,6 +31,7 @@ uint32_t xorshift32(uint32_t seed) {
return seed; return seed;
} }
// Implementation of XORshift RNG for 64bit integers
uint64_t xorshift64(uint64_t seed) { uint64_t xorshift64(uint64_t seed) {
seed ^= seed << 13; seed ^= seed << 13;
seed ^= seed >> 7; seed ^= seed >> 7;

14
src/rng/lcg.c → src/rng/rng.h

@ -12,6 +12,14 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include <stdint.h> #include <stdint.h>
int64_t lcg(int64_t seed) { // Implementation of BBS RNG with pre-defined variables
return (8121 * seed + 28411) % 134456; 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);

10
testing/test.c

@ -17,13 +17,9 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "../src/rng/lcg.c" #include "../src/rng/rng.h"
#include "../src/rng/xorshift.c" #include "../src/img/ppm.h"
#include "../src/rng/bbs.c" #include "../src/endian/endian.h"
#include "../src/img/ppm.c"
#include "../src/endian/endian.c"
int test_rng() { int test_rng() {
lcg(76); lcg(76);

Loading…
Cancel
Save