use unsigned int for compiler optimization

This commit is contained in:
2025-09-24 15:57:34 +02:00
parent 8cc23ce9e6
commit e65cf7e37c
10 changed files with 58 additions and 76 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ on:
branches: ["master"]
env:
GCC_ARGS: src/*.c src/*.h
GCC_ARGS: src/*.c src/*.h -Ofast
TARGET: margen
TEST_ARGS: ""
+1 -1
View File
@@ -9,7 +9,7 @@ clean:
build:
@mkdir -p build
gcc -Wall -Wextra src/*.c src/*.h -o build/$(TARGET) -g
gcc -Wall -Wextra -Og -g src/*.c src/*.h -o build/$(TARGET)
.PHONY: install
install: build
+1
View File
@@ -6,5 +6,6 @@ AC_CHECK_HEADERS([stdlib.h])
AC_CHECK_HEADERS([stdbool.h])
AC_CHECK_HEADERS([string.h])
AC_CHECK_HEADERS([time.h])
AC_CHECK_HEADERS([limits.h])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
+23 -33
View File
@@ -1,3 +1,4 @@
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -81,26 +82,15 @@ static bool is_number(char *value) {
return true;
}
static unsigned char parse_char(char *arg, char *value) {
static unsigned int parse_uint(char *arg, char *value) {
if (!is_number(value)) {
invalid_value(arg, value);
}
unsigned long long tmp_value = (unsigned long long)atoll(value);
if (tmp_value >= 256) {
if (tmp_value >= UINT_MAX) {
invalid_value(arg, value);
}
return (unsigned char)tmp_value;
}
static unsigned short parse_ushort(char *arg, char *value) {
if (!is_number(value)) {
invalid_value(arg, value);
}
unsigned long long tmp_value = (unsigned long long)atoll(value);
if (tmp_value >= 65536) {
invalid_value(arg, value);
}
return (unsigned short)tmp_value;
return (unsigned int)tmp_value;
}
static unsigned long parse_ulong(char *arg, char *value) {
@@ -110,14 +100,14 @@ static unsigned long parse_ulong(char *arg, char *value) {
return (unsigned long)atoll(value);
}
static void parse_color(char *arg, char *value, unsigned char color[3]) {
static void parse_color(char *arg, char *value, unsigned int color[3]) {
char *tmp;
tmp = strtok(value, ",");
color[0] = parse_char(arg, tmp);
color[0] = parse_uint(arg, tmp);
tmp = strtok(NULL, ",");
color[1] = parse_char(arg, tmp);
color[1] = parse_uint(arg, tmp);
tmp = strtok(NULL, ",");
color[2] = parse_char(arg, tmp);
color[2] = parse_uint(arg, tmp);
}
Parameters args_parse(int argc, char **argv) {
@@ -152,7 +142,7 @@ Parameters args_parse(int argc, char **argv) {
puts(PACKAGE " " VERSION);
exit(0);
} else if (is_arg(arg, "-w") || is_arg(arg, "--width")) {
params.width = parse_ushort(arg, value);
params.width = parse_uint(arg, value);
if (params.width == 0) {
invalid_value(arg, value);
}
@@ -160,7 +150,7 @@ Parameters args_parse(int argc, char **argv) {
params.height = params.width;
}
} else if (is_arg(arg, "-h") || is_arg(arg, "--height")) {
params.height = parse_ushort(arg, value);
params.height = parse_uint(arg, value);
if (params.height == 0) {
invalid_value(arg, value);
}
@@ -172,13 +162,13 @@ Parameters args_parse(int argc, char **argv) {
} else if (is_arg(arg, "-o") || is_arg(arg, "--output")) {
params.file_path = value;
} else if (is_arg(arg, "-p") || is_arg(arg, "--pixel")) {
params.size = parse_ushort(arg, value);
params.size = parse_uint(arg, value);
if (params.size == 0) {
invalid_value(arg, value);
}
size_set = true;
} else if (is_arg(arg, "-s") || is_arg(arg, "--slope")) {
params.slope = parse_char(arg, value);
params.slope = parse_uint(arg, value);
slope_set = true;
} else if (is_arg(arg, "-c") || is_arg(arg, "--color")) {
parse_color(arg, value, params.start);
@@ -187,9 +177,9 @@ Parameters args_parse(int argc, char **argv) {
parse_color(arg, value, params.var);
var_set = true;
} else if (is_arg(arg, "-vr") || is_arg(arg, "--var-range")) {
var_range = parse_char(arg, value);
var_range = parse_uint(arg, value);
} else if (is_arg(arg, "-r") || is_arg(arg, "--rotation")) {
params.rotation = parse_char(arg, value) % 4;
params.rotation = parse_uint(arg, value) % 4;
rot_set = true;
} else if (is_arg(arg, "-m") || is_arg(arg, "--monochrome")) {
params.monochrome = true;
@@ -206,27 +196,27 @@ Parameters args_parse(int argc, char **argv) {
rand_seed(params.seed);
if (!size_set) {
params.size = rand_ushort(6) + 6;
params.size = rand_uint(6) + 6;
}
if (!slope_set) {
params.slope = rand_uchar(50) + 100;
params.slope = rand_uint(50) + 100;
}
if (!start_set) {
params.start[0] = rand_uchar(256);
params.start[1] = rand_uchar(256);
params.start[2] = rand_uchar(256);
params.start[0] = rand_uint(256);
params.start[1] = rand_uint(256);
params.start[2] = rand_uint(256);
}
if (!var_set) {
params.var[0] = rand_uchar(var_range + 1);
params.var[1] = rand_uchar(var_range + 1);
params.var[2] = rand_uchar(var_range + 1);
params.var[0] = rand_uint(var_range + 1);
params.var[1] = rand_uint(var_range + 1);
params.var[2] = rand_uint(var_range + 1);
}
if (!rot_set) {
params.rotation = rand_uchar(4);
params.rotation = rand_uint(4);
}
return params;
+15 -19
View File
@@ -30,11 +30,10 @@ static void write_nul(unsigned char *buffer, unsigned int offset,
}
}
static unsigned char *bmp_header(unsigned short width, unsigned short height,
unsigned char color_depth) {
static unsigned char *bmp_header(unsigned int width, unsigned int height,
unsigned int color_depth) {
unsigned char *output = (unsigned char *)malloc(HEADER_SIZE);
unsigned int data_length = ((unsigned int)width) * ((unsigned int)height) *
((unsigned int)color_depth);
unsigned int data_length = width * height * color_depth;
// 0x00(2) BM
write_str(output, 0x00, 0x02, (unsigned char *)"BM");
// 0x02(4) file size
@@ -69,41 +68,38 @@ static unsigned char *bmp_header(unsigned short width, unsigned short height,
return output;
}
static unsigned int bmp_data_line_length(unsigned short width,
unsigned char color_depth) {
unsigned int data_length =
((unsigned int)width) * ((unsigned int)color_depth);
static unsigned int bmp_data_line_length(unsigned int width,
unsigned int color_depth) {
unsigned int data_length = width * color_depth;
unsigned int line_offset = data_length % 4;
unsigned short line_padding = line_offset > 0 ? 4 - line_offset : 0;
unsigned int line_padding = line_offset > 0 ? 4 - line_offset : 0;
return data_length + line_padding;
}
static void bmp_data_line(unsigned char *buffer, unsigned short width,
unsigned char color_depth, unsigned char *data) {
unsigned int data_length =
((unsigned int)width) * ((unsigned int)color_depth);
static void bmp_data_line(unsigned char *buffer, unsigned int width,
unsigned int color_depth, unsigned char *data) {
unsigned int data_length = width * color_depth;
unsigned int line_offset = data_length % 4;
unsigned short line_padding = line_offset > 0 ? 4 - line_offset : 0;
unsigned int line_padding = line_offset > 0 ? 4 - line_offset : 0;
write_str(buffer, 0, data_length, data);
if (line_padding > 0) {
write_nul(buffer, width * color_depth, line_padding);
}
}
void bmp_generate(unsigned short width, unsigned short height,
unsigned char color_depth, bool descending, char *file_path,
void bmp_generate(unsigned int width, unsigned int height,
unsigned int color_depth, bool descending, char *file_path,
line_fn generate_line) {
FILE *fptr;
fptr = fopen(file_path, "w");
unsigned char *header = bmp_header(width, height, color_depth);
fwrite(header, HEADER_SIZE, 1, fptr);
free(header);
unsigned int data_length =
((unsigned int)width) * ((unsigned int)color_depth);
unsigned int data_length = width * color_depth;
unsigned int line_length = bmp_data_line_length(width, color_depth);
unsigned char *data_buffer = (unsigned char *)malloc(data_length);
unsigned char *line_buffer = (unsigned char *)malloc(line_length);
unsigned short y;
unsigned int y;
for (y = 0; y < height; y++) {
generate_line(y, data_buffer, data_length);
bmp_data_line(line_buffer, width, color_depth, data_buffer);
+2 -2
View File
@@ -3,8 +3,8 @@
#ifndef BMP_H
#define BMP_H
void bmp_generate(unsigned short width, unsigned short height,
unsigned char color_depth, bool descending, char *file_path,
void bmp_generate(unsigned int width, unsigned int height,
unsigned int color_depth, bool descending, char *file_path,
line_fn generate_line);
#endif
+4 -4
View File
@@ -19,9 +19,9 @@ static unsigned char *current_line;
static unsigned char generate_pixel(unsigned char depth,
unsigned char top_pixel,
unsigned char left_pixel) {
short k = rand_uchar(global_params.var[depth] + 1);
short v = (rand_uchar(2) == 0 ? k : -k) + (left_pixel)*slope +
(top_pixel) * (1.0 - slope);
int k = rand_uint(global_params.var[depth] + 1);
int v = (rand_uint(2) == 0 ? k : -k) + (left_pixel)*slope +
(top_pixel) * (1.0 - slope);
return (unsigned char)(v < 0 ? 0 : (v > 255 ? (unsigned char)255 : v));
}
@@ -41,7 +41,7 @@ static void generate_line() {
}
}
static void generate_bmp_line(unsigned short y, unsigned char *data_buffer,
static void generate_bmp_line(unsigned int y, unsigned char *data_buffer,
unsigned int len) {
unsigned int i;
unsigned int x;
+2 -6
View File
@@ -18,10 +18,6 @@ void rand_seed(unsigned long long seed) {
(void)rand();
}
unsigned char rand_uchar(const unsigned int max) {
return max == 0 ? 0 : (unsigned char)(rand() % max);
}
unsigned short rand_ushort(const unsigned int max) {
return max == 0 ? 0 : (unsigned short)(rand() % max);
unsigned int rand_uint(const unsigned int max) {
return max == 0 ? 0 : (unsigned int)(rand() % max);
}
+1 -2
View File
@@ -2,7 +2,6 @@
#define RAND_H
void rand_seed(unsigned long long seed);
unsigned char rand_uchar(unsigned int max);
unsigned short rand_ushort(unsigned int max);
unsigned int rand_uint(unsigned int max);
#endif
+8 -8
View File
@@ -7,17 +7,17 @@ typedef struct Parameters {
bool quiet;
bool monochrome;
unsigned long seed;
unsigned short width;
unsigned short height;
unsigned int width;
unsigned int height;
char *file_path;
unsigned short size;
unsigned char slope;
unsigned char start[3];
unsigned char var[3];
unsigned char rotation;
unsigned int size;
unsigned int slope;
unsigned int start[3];
unsigned int var[3];
unsigned int rotation;
} Parameters;
typedef void line_fn(unsigned short y, unsigned char *data_buffer,
typedef void line_fn(unsigned int y, unsigned char *data_buffer,
unsigned int len);
#endif