Skip to content

Commit

Permalink
lib: Un-GNU-ify C codebase
Browse files Browse the repository at this point in the history
Until now, I had specified -std=c99, but defined _GNU_SOURCE, meaning I
was using a bunch of GNU extensions. It's comfy to write code that way,
but portability is even more comfy. I enabled -Wpedantic for this pass,
but I just can't bear cluttering the dlsym() stuff with unnecessary
casts just because ia64 was weird back in the day, so I didn't leave it
on. I'll check on things now and then, but for now, this is a good start
to better POSIX C99 adherence :]
  • Loading branch information
vkoskiv committed Feb 5, 2024
1 parent f306102 commit 44b980c
Show file tree
Hide file tree
Showing 35 changed files with 102 additions and 101 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC=cc
CFLAGS=-I./include/ -Wall -Wextra -Wno-missing-field-initializers -std=c99 -D_GNU_SOURCE -O2 -ftree-vectorize
CFLAGS=-I./include/ -Wall -Wextra -Wno-missing-field-initializers -std=c99 -D_POSIX_C_SOURCE -O2 -ftree-vectorize
LDFLAGS=-lpthread -lm -ldl
BIN=bin/c-ray
OBJDIR=bin/obj
Expand Down
6 changes: 3 additions & 3 deletions src/common/dyn_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ static inline size_t grow_x_2(size_t capacity, size_t elem_size) {
T##_arr_free(b); \
}

dyn_array_def(int);
dyn_array_def(float);
dyn_array_def(size_t);
dyn_array_def(int)
dyn_array_def(float)
dyn_array_def(size_t)

#endif
1 change: 0 additions & 1 deletion src/common/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ file_data file_load(const char *file_path) {
logr(warning, "Couldn't mmap '%.*s': %s\n", (int)strlen(file_path), file_path, strerror(errno));
return (file_data){ 0 };
}
madvise(data, size, MADV_SEQUENTIAL);
file_data file = (file_data){ .items = data, .count = size, .capacity = size };
return file;
#else
Expand Down
2 changes: 1 addition & 1 deletion src/common/fileio.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum fileType {
};

typedef byte file_bytes;
dyn_array_def(file_bytes);
dyn_array_def(file_bytes)
typedef struct file_bytes_arr file_data;

enum fileType match_file_type(const char *ext);
Expand Down
2 changes: 1 addition & 1 deletion src/common/loaders/formats/wavefront/mtlloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ struct material {
};

typedef struct material material;
dyn_array_def(material);
dyn_array_def(material)

// FIXME: Delete these and use ones in node.c instead
static struct cr_shader_node *alloc(struct cr_shader_node d) {
Expand Down
6 changes: 3 additions & 3 deletions src/common/loaders/meshloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ struct mesh_material {
};

typedef struct mesh_material mesh_material;
dyn_array_def(mesh_material);
dyn_array_def(mesh_material)

typedef struct cr_face cr_face;
dyn_array_def(cr_face);
dyn_array_def(cr_face)

struct ext_mesh {
struct vertex_buffer *vbuf;
Expand All @@ -32,7 +32,7 @@ struct ext_mesh {
};

typedef struct ext_mesh ext_mesh;
dyn_array_def(ext_mesh);
dyn_array_def(ext_mesh)

static inline void ext_mesh_free(struct ext_mesh *m) {
if (m->vbuf) vertex_buf_free(m->vbuf);
Expand Down
6 changes: 3 additions & 3 deletions src/common/platform/terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool isTeleType(void) {
static void show_cursor(bool show) {
(void)show;
#ifndef NO_COLOR
if (isTeleType()) show ? fputs("\e[?25h", stdout) : fputs("\e[?25l", stdout);
if (isTeleType()) show ? fputs("\033[?25h", stdout) : fputs("\033[?25l", stdout);
#endif
}

Expand All @@ -46,7 +46,7 @@ static void handler(int sig) {
}
}

void term_init() {
void term_init(void) {
if (registerHandler(sigint, handler)) {
logr(warning, "Unable to catch SIGINT\n");
}
Expand All @@ -70,7 +70,7 @@ void term_init() {
#endif
}

void term_restore() {
void term_restore(void) {
#ifndef WINDOWS
show_cursor(true);
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/common/platform/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct cr_cond {
};

typedef struct cr_thread cr_thread;
dyn_array_def(cr_thread);
dyn_array_def(cr_thread)

// Create & detach a thread. Used by thread pool.
int thread_create_detach(struct cr_thread *t);
Expand Down
2 changes: 1 addition & 1 deletion src/common/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct texture_asset {
};

typedef struct texture_asset texture_asset;
dyn_array_def(texture_asset);
dyn_array_def(texture_asset)

//FIXME: These are opposite states, which is kinda confusing.
#define SRGB_TRANSFORM 0x01
Expand Down
6 changes: 4 additions & 2 deletions src/common/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright © 2018-2020 Valtteri Koskivuori. All rights reserved.
//

#include "../includes.h"
#include "timer.h"

#ifdef WINDOWS
Expand Down Expand Up @@ -67,6 +66,9 @@ void timer_sleep_ms(int ms) {
ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
usleep(ms * 1000);
struct timeval tv = { 0 };
tv.tv_sec = ms / 1000;
tv.tv_usec = ms % 1000 * 1000;
select(0, NULL, NULL, NULL, &tv);
#endif
}
2 changes: 2 additions & 0 deletions src/common/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#pragma once

#include "../includes.h"

#ifdef WINDOWS
typedef struct timeval {
long tv_sec;
Expand Down
6 changes: 3 additions & 3 deletions src/common/transforms.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ float rad_to_deg(float radians) {
return radians * (180.0f / PI);
}

struct matrix4x4 mat_id() {
struct matrix4x4 mat_id(void) {
return (struct matrix4x4) {
.mtx = {
{1.0f, 0.0f, 0.0f, 0.0f},
Expand All @@ -34,7 +34,7 @@ struct matrix4x4 mat_id() {
};
}

struct transform tform_new() {
struct transform tform_new(void) {
struct transform tf;
tf.A = mat_id();
tf.Ainv = tf.A; // Inverse of I == I
Expand Down Expand Up @@ -212,7 +212,7 @@ float findDeterminant4x4(const float A[4][4]) {
}

//Find det of a given 4x4 matrix A
float findDeterminant(float A[4][4], int n) {
float findDeterminant(const float A[4][4], int n) {
float det = 0.0f;

if (n == 1)
Expand Down
10 changes: 5 additions & 5 deletions src/common/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct vector {
};

typedef struct vector vector;
dyn_array_def(vector);
dyn_array_def(vector)

struct base {
struct vector i, j, k;
Expand All @@ -29,7 +29,7 @@ struct coord {
};

typedef struct coord coord;
dyn_array_def(coord);
dyn_array_def(coord)

struct intCoord {
int x, y;
Expand All @@ -38,11 +38,11 @@ struct intCoord {
static const struct vector g_world_up = { 0.0f, 1.0f, 0.0f };

//For defaults
static inline struct vector vec_zero() {
static inline struct vector vec_zero(void) {
return (struct vector){ 0.0f, 0.0f, 0.0f };
}

static inline struct coord coord_zero() {
static inline struct coord coord_zero(void) {
return (struct coord){ 0.0f, 0.0f };
}

Expand All @@ -53,7 +53,7 @@ struct vertex_buffer {
};

typedef struct vertex_buffer vertex_buffer;
dyn_array_def(vertex_buffer);
dyn_array_def(vertex_buffer)


static inline void vertex_buf_free(struct vertex_buffer *buf) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/datatypes/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct camera {
};

typedef struct camera camera;
dyn_array_def(camera);
dyn_array_def(camera)

struct sampler;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/datatypes/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "../../common/vector.h"

typedef struct cr_face cr_face;
dyn_array_def(cr_face);
dyn_array_def(cr_face)

struct mesh {
struct vertex_buffer *vbuf;
Expand All @@ -27,6 +27,6 @@ struct mesh {
};

typedef struct mesh mesh;
dyn_array_def(mesh);
dyn_array_def(mesh)

void mesh_free(struct mesh *mesh);
2 changes: 1 addition & 1 deletion src/lib/datatypes/poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct poly {
};

typedef struct poly poly;
dyn_array_def(poly);
dyn_array_def(poly)

struct lightRay;
struct hitRecord;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/datatypes/sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ struct sphere {
};

typedef struct sphere sphere;
dyn_array_def(sphere);
dyn_array_def(sphere)

bool rayIntersectsWithSphere(const struct lightRay *ray, const struct sphere *sphere, struct hitRecord *isect);
2 changes: 1 addition & 1 deletion src/lib/datatypes/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct render_tile {
};

typedef struct render_tile render_tile;
dyn_array_def(render_tile);
dyn_array_def(render_tile)

struct tile_set {
struct render_tile_arr tiles;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/nodes/bsdfnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ struct bsdfNode {
};

typedef const struct bsdfNode * bsdf_node_ptr;
dyn_array_def(bsdf_node_ptr);
dyn_array_def(bsdf_node_ptr)

typedef struct cr_shader_node * cr_shader_node_ptr;
dyn_array_def(cr_shader_node_ptr);
dyn_array_def(cr_shader_node_ptr)

struct bsdf_buffer {
struct bsdf_node_ptr_arr bsdfs;
Expand All @@ -45,7 +45,7 @@ struct bsdf_buffer {
void bsdf_buffer_free(struct bsdf_buffer *b);

typedef struct bsdf_buffer bsdf_buffer;
dyn_array_def(bsdf_buffer);
dyn_array_def(bsdf_buffer)

#include "shaders/diffuse.h"
#include "shaders/glass.h"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/nodes/converter/color_ramp.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "color_ramp.h"

typedef struct ramp_element ramp_element;
dyn_array_def(ramp_element);
dyn_array_def(ramp_element)

struct color_ramp_node {
struct colorNode node;
Expand Down
48 changes: 24 additions & 24 deletions src/lib/nodes/converter/vecmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static inline float wrap(float value, float max, float min) {
return (range != 0.0f) ? value - (range * floorf((value - min) / range)) : min;
}

static struct vectorValue eval(const struct vectorNode *node, sampler *sampler, const struct hitRecord *record) {
static union vector_value eval(const struct vectorNode *node, sampler *sampler, const struct hitRecord *record) {
struct vecMathNode *this = (struct vecMathNode *)node;

const struct vector a = this->A->eval(this->A, sampler, record).v;
Expand All @@ -145,56 +145,56 @@ static struct vectorValue eval(const struct vectorNode *node, sampler *sampler,

switch (this->op) {
case VecAdd:
return (struct vectorValue){ .v = vec_add(a, b) };
return (union vector_value){ .v = vec_add(a, b) };
case VecSubtract:
return (struct vectorValue){ .v = vec_sub(a, b) };
return (union vector_value){ .v = vec_sub(a, b) };
case VecMultiply:
return (struct vectorValue){ .v = vec_mul(a, b) };
return (union vector_value){ .v = vec_mul(a, b) };
case VecDivide:
return (struct vectorValue){ .v = { a.x / b.x, a.y / b.y, a.z / b.z } };
return (union vector_value){ .v = { a.x / b.x, a.y / b.y, a.z / b.z } };
case VecCross:
return (struct vectorValue){ .v = vec_cross(a, b) };
return (union vector_value){ .v = vec_cross(a, b) };
case VecReflect:
return (struct vectorValue){ .v = vec_reflect(a, b) };
return (union vector_value){ .v = vec_reflect(a, b) };
case VecRefract:
{
struct vectorValue v = { 0 };
union vector_value v = { 0 };
v.f = vec_refract(a, b, f, &v.v) ? 1.0f : 0.0f;
return v;
}
case VecDot:
return (struct vectorValue){ .f = vec_dot(a, b) };
return (union vector_value){ .f = vec_dot(a, b) };
case VecDistance:
return (struct vectorValue){ .f = vec_distance_to(a, b) };
return (union vector_value){ .f = vec_distance_to(a, b) };
case VecLength:
return (struct vectorValue){ .f = vec_length(a) };
return (union vector_value){ .f = vec_length(a) };
case VecScale:
return (struct vectorValue){ .v = vec_scale(a, f) };
return (union vector_value){ .v = vec_scale(a, f) };
case VecNormalize:
return (struct vectorValue){ .v = vec_normalize(a) };
return (union vector_value){ .v = vec_normalize(a) };
case VecWrap:
return (struct vectorValue){ .v = { wrap(a.x, b.x, c.x), wrap(a.y, b.y, c.y), wrap(a.z, b.z, c.z) } };
return (union vector_value){ .v = { wrap(a.x, b.x, c.x), wrap(a.y, b.y, c.y), wrap(a.z, b.z, c.z) } };
case VecFloor:
return (struct vectorValue){ .v = { .x = floorf(a.x), .y = floorf(a.y), .z = floorf(a.z) } };
return (union vector_value){ .v = { .x = floorf(a.x), .y = floorf(a.y), .z = floorf(a.z) } };
case VecCeil:
return (struct vectorValue){ .v = { .x = ceilf(a.x), .y = ceilf(a.y), .z = ceilf(a.z) } };
return (union vector_value){ .v = { .x = ceilf(a.x), .y = ceilf(a.y), .z = ceilf(a.z) } };
case VecModulo:
return (struct vectorValue){ .v = { .x = fmodf(a.x, b.x), .y = fmodf(a.y, b.y), .z = fmodf(a.z, b.z) } };
return (union vector_value){ .v = { .x = fmodf(a.x, b.x), .y = fmodf(a.y, b.y), .z = fmodf(a.z, b.z) } };
case VecAbs:
return (struct vectorValue){ .v = { .x = fabsf(a.x), .y = fabsf(a.y), .z = fabsf(a.z) } };
return (union vector_value){ .v = { .x = fabsf(a.x), .y = fabsf(a.y), .z = fabsf(a.z) } };
case VecMin:
return (struct vectorValue){ .v = { .x = fminf(a.x, b.x), .y = fminf(a.y, b.y), .z = fminf(a.z, b.z) } };
return (union vector_value){ .v = { .x = fminf(a.x, b.x), .y = fminf(a.y, b.y), .z = fminf(a.z, b.z) } };
case VecMax:
return (struct vectorValue){ .v = { .x = fmaxf(a.x, b.x), .y = fmaxf(a.y, b.y), .z = fmaxf(a.z, b.z) } };
return (union vector_value){ .v = { .x = fmaxf(a.x, b.x), .y = fmaxf(a.y, b.y), .z = fmaxf(a.z, b.z) } };
case VecSin:
return (struct vectorValue){ .v = { .x = sinf(a.x), .y = sinf(a.y), .z = sinf(a.z) } };
return (union vector_value){ .v = { .x = sinf(a.x), .y = sinf(a.y), .z = sinf(a.z) } };
case VecCos:
return (struct vectorValue){ .v = { .x = cosf(a.x), .y = cosf(a.y), .z = cosf(a.z) } };
return (union vector_value){ .v = { .x = cosf(a.x), .y = cosf(a.y), .z = cosf(a.z) } };
case VecTan:
return (struct vectorValue){ .v = { .x = tanf(a.x), .y = tanf(a.y), .z = tanf(a.z) } };
return (union vector_value){ .v = { .x = tanf(a.x), .y = tanf(a.y), .z = tanf(a.z) } };
}
ASSERT_NOT_REACHED();
return (struct vectorValue){ 0 };
return (union vector_value){ 0 };
}

const struct vectorNode *newVecMath(const struct node_storage *s, const struct vectorNode *A, const struct vectorNode *B, const struct vectorNode *C, const struct valueNode *f, const enum cr_vec_op op) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/nodes/converter/vecmix.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ static void dump(const void *node, char *dumpbuf, int bufsize) {
snprintf(dumpbuf, bufsize, "vec_mix { A: %s, B: %s, f: %s }", A, B, f);
}

static struct vectorValue eval(const struct vectorNode *node, sampler *sampler, const struct hitRecord *record) {
static union vector_value eval(const struct vectorNode *node, sampler *sampler, const struct hitRecord *record) {
struct vec_mix *this = (struct vec_mix *)node;

const float lerp = this->f->eval(this->f, sampler, record);

if (getDimension(sampler) > lerp) {
return (struct vectorValue){ .v = this->A->eval(this->A, sampler, record).v };
return (union vector_value){ .v = this->A->eval(this->A, sampler, record).v };
} else {
return (struct vectorValue){ .v = this->B->eval(this->B, sampler, record).v };
return (union vector_value){ .v = this->B->eval(this->B, sampler, record).v };
}
}

Expand Down
Loading

0 comments on commit 44b980c

Please sign in to comment.