diff options
author | jonsykkel <jonrevold@gmail.com> | 2020-04-02 19:33:03 +0200 |
---|---|---|
committer | jonsykkel <jonrevold@gmail.com> | 2020-04-02 19:33:03 +0200 |
commit | 196b3d947d2747652977a1d26d14f8361ce13e1a (patch) | |
tree | 3421e9023099dfccc6bf3f9adbde818655004edc | |
download | ct_sequence-196b3d947d2747652977a1d26d14f8361ce13e1a.tar.gz |
vork
-rw-r--r-- | .gitignore | 5 | ||||
-rw-r--r-- | makefile | 114 | ||||
-rw-r--r-- | src/main.c | 40 |
3 files changed, 159 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..364334e --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/* +!.gitignore +!makefile +!src/ + diff --git a/makefile b/makefile new file mode 100644 index 0000000..eb75271 --- /dev/null +++ b/makefile @@ -0,0 +1,114 @@ +#version 5 + +out_name := out +build := release +tmp_dir := tmp +bin_dir := bin +lib_dir := lib +src_dir := src + +CFLAGS := -std=c11 -fdata-sections -ffunction-sections -D_POSIX_C_SOURCE=200809L +CFLAGS += -pedantic -Wall -Wextra -Wcast-qual -Wstrict-aliasing -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes -Wvla -Wno-parentheses -Wno-unused-variable -Wno-unused-function -Wno-unused-parameter -Wno-unused-label +LDFLAGS := -Wl,--gc-sections +LDLIBS := +ARFLAGS := -rcs + +compile = $(CC) $(CFLAGS) -MMD -c $< -o $@ +link = $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) + +ifeq ($(build),release) + out_file := $(out_name) +else + out_file := $(out_name)_$(build) +endif + +bin := $(bin_dir)/$(out_file) + +ifneq ($(MSYSTEM),) + bin := $(bin).exe + LDFLAGS += -static + RELFLAGS := -s -mwindows +else + RELFLAGS := +endif + +out := $(bin) + +ifeq ($(build),debug) + CFLAGS += -g +else ifeq ($(build),release) + CFLAGS += -O3 + #CFLAGS += -Wunused-variable -Wunused-function -Wunused-parameter -Wunused-label + LDFLAGS += $(RELFLAGS) +else ifeq ($(build),analyze) + CC := clang + CFLAGS += --analyze -Xanalyzer -analyzer-output=text + link := +else + $(error invalid build: "$(build)") +endif + +LDLIBS := -lm $(LDLIBS) + +#################################################################### + +ifneq ($(run),) + run_cmd := @echo "run $(bin)" + run_cmd += && cd bin + run_cmd += && echo "----------------------------------------------------------------" + run_cmd += && time ./$(out_file) + run_cmd += ; echo "----------------------------------------------------------------" + run_cmd += && cd .. +endif + +ifneq ($(debug),) + CC := gcc + run_cmd := @echo "debug $(bin)" + run_cmd += && cd bin + run_cmd += && echo "----------------------------------------------------------------" + run_cmd += && gdb $(out_file) + run_cmd += ; echo "----------------------------------------------------------------" + run_cmd += && cd .. +endif + +ifeq ($(PREFIX),) + PREFIX := /usr/local +endif + +ifeq ($(CC),clang) + CFLAGS += -fcolor-diagnostics -fansi-escape-codes + LDFLAGS := $(filter-out -pthread,$(LDFLAGS)) + LDLIBS += -lpthread +endif + +rwildcard = $(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)) + +build_dir := $(tmp_dir)/$(build) +src := $(foreach dir,$(src_dir),$(call rwildcard,$(dir)/,*.c)) +obj := $(src:.c=.o) +obj := $(addprefix $(build_dir)/,$(obj)) +dep := $(obj:.o=.d) +dst_dir := $(DESTDIR)$(PREFIX) + +#################################################################### + +.PHONY: all +all: $(out) + @$(run_cmd) + +$(out): $(obj) + @echo "link $@" + @mkdir -p $(@D) + @$(link) + +-include $(dep) + +$(build_dir)/%.o: %.c + @echo "compile $<" + @mkdir -p $(@D) + @$(compile) + +.PHONY: clean +clean: + @-rm -rf $(tmp_dir)/* + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..433ec72 --- /dev/null +++ b/src/main.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> + +//#define MAX 100000000 +#define MAX 1000000000 + +static void primt(uint32_t x,uint32_t t){ + printf("[%3d] %d\n",x+1,t); +} + +int main(int argc,char **argv){ + uint32_t *h; + uint32_t t; + uint32_t n; + + h = malloc(sizeof(uint32_t)*MAX); + if(!h) return 1; + memset(h,0xFF,sizeof(uint32_t)*MAX); + + t = 0; + n = 0; + + for(uint32_t x = 0;x < MAX;x++){ + t = n; + if((int32_t)h[t] >= 0){ + n = x-h[t]; + }else{ + n = 0; + } + h[t] = x; + //primt(x,t); + } + + primt(MAX-1,t); + + return 0; +} + |