initial commit

master
Avril 4 years ago
commit 5b10589dcd
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -0,0 +1,77 @@
# Generic C and C++ Makefile project template
# Contains targets for `release', `debug', and `clean'.
PROJECT=bits
AUTHOR=Avril (Flanchan) <flanchan@cumallover.me>
SRC_C = $(wildcard src/*.c)
SRC_CXX = $(wildcard src/*.cpp)
INCLUDE=include
COMMON_FLAGS= -W -Wall -pedantic -fno-strict-aliasing $(addprefix -I,$(INCLUDE))
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 +=
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
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-rebuild:
rm -rf obj
clean: clean-rebuild
rm -f $(PROJECT)-{release,debug,pgo}

@ -0,0 +1,57 @@
/* bits - pipe the bits of each byte from stdin to stdout
*
* usage: bits [<mask>]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 4096
#define IGNORE(param) do { (void)(param); } while(0)
static inline int sfread(void* out, size_t *restrict size, FILE* f)
{
return !!(*size = (size_t)fread(out, 1, *size, f));
}
static int fwrite_all(const void* buf, size_t esz, size_t num, FILE* out)
{
register size_t w, done=0;
register size_t fsz = esz * num;
const char* tbuf = (const char*)buf;
while( (w = fwrite(tbuf+done, 1, fsz, out)) > 0 && done < fsz) done+=w;
if (done!=fsz) return 0;
else return 1;
}
static void pbits(char out[restrict 8], unsigned char byte, const char bit[static 2])
{
for(register int i=8; i --> 0; byte >>= 1) *out++=((int)bit[byte & 1]);
}
static inline int check(int val, const char* msg)
{
if(val) return val;
fprintf(stderr, "error: %s\n", msg);
exit(1);
}
int main(int argc, char **argv)
{
IGNORE(argc);
const char* mask = argv[1] && check(strlen(argv[1]) > 1, "expected 2 character mask") ? argv[1] : "01";
unsigned char buf[BUFFER_SIZE];
char tbuf[BUFFER_SIZE * 8];
size_t bsz = BUFFER_SIZE;
while(sfread(buf, &bsz, stdin))
{
register size_t size = bsz;
while(size --> 0) pbits(tbuf+(8*size), buf[size], mask);
if(!fwrite_all(tbuf, 8, bsz, stdout)) { perror("Failed to write"); return 1; }
}
return 0;
}
Loading…
Cancel
Save