~kdsch/bl

113632caadfc1c23cc11d3f8b08889e216025133 — Karl Schultheisz 2 years ago 0542a50
lib: avoid propagating types needlessly

Just because a libc function returns unsigned long doesn't mean the
entire codebase has to use unsigned long. The numbers fit into 13 bits;
typecasting is OK.
2 files changed, 24 insertions(+), 24 deletions(-)

M lib.c
M lib_test.c
M lib.c => lib.c +16 -16
@@ 14,16 14,16 @@
/* magic numbers at which an increase in brightness
 * has been found to actually make a difference.
 * There are probably more, but these suffice. */
static const unsigned long table[] = {
static const unsigned table[] = {
	0, 1, 42, 71, 100, 130, 159, 188, 218, 247, 276
};

static const struct {
	unsigned long num, den;
	unsigned num, den;
} rate = {6, 5};

static int
index(unsigned long x)
index(unsigned x)
{
	for (int i = 0; (unsigned)i < len(table) - 1; i++) {
		if (table[i] <= x && x < table[i + 1]) {


@@ 34,10 34,10 @@ index(unsigned long x)
	return -1;
}

static unsigned long
inc(unsigned long x)
static unsigned
inc(unsigned x)
{
	unsigned long y = rate.num * x / rate.den;
	unsigned y = rate.num * x / rate.den;
	int j = index(y);

	if (-1 == j) {


@@ 58,20 58,20 @@ inc(unsigned long x)
	return y;
}

static unsigned long
lim(unsigned long x, unsigned long max)
static unsigned
lim(unsigned x, unsigned max)
{
	return x < max ? x : max;
}

static unsigned long
dec(unsigned long x)
static unsigned
dec(unsigned x)
{ 
	if (!x) {
		return 0;
	}

	unsigned long y = rate.den * x / rate.num;
	unsigned y = rate.den * x / rate.num;
	int j = index(y);

	if ((x && index(x) != j) || j == -1) {


@@ 88,7 88,7 @@ die(const char *s)
	exit(1);
}

static unsigned long
static unsigned
get(int fd)
{
	char buf[5] = {0};


@@ 98,16 98,16 @@ get(int fd)
		die("read");
	}

	return strtoul(buf, NULL, 10);
	return (unsigned)strtoul(buf, NULL, 10);
}

static void
put(int fd, unsigned long x)
put(int fd, unsigned x)
{
	dprintf(fd, "%lu\n", x);
}

static unsigned long
static unsigned
path_get(const char *pathname)
{
	int fd = open(pathname, O_RDONLY, 0);


@@ 116,7 116,7 @@ path_get(const char *pathname)
		die("path_get: open");
	}

	unsigned long x = get(fd);
	unsigned x = get(fd);
	close(fd);
	return x;
}

M lib_test.c => lib_test.c +8 -8
@@ 4,7 4,7 @@ int
test_index()
{
	struct {
		unsigned long x;
		unsigned x;
		int i;
	} c, cases[] = {
		{ .x = 0, .i = 0 },


@@ 61,7 61,7 @@ int
test_inc()
{
	struct {
		unsigned long x, y;
		unsigned x, y;
	} c, cases[] = {
		{ .x = 300, .y = 360 },
		{ .x = 200, .y = 240 },


@@ 77,9 77,9 @@ test_inc()
	int fail = 0;
	for (unsigned i = 0; i < len(cases); i++) {
		c = cases[i];
		unsigned long y = inc(c.x);
		unsigned y = inc(c.x);
		if (y != c.y) {
			printf("%s case %d: expected %lu, got %lu\n", __func__, i, c.y, y);
			printf("%s case %d: expected %u, got %u\n", __func__, i, c.y, y);
			fail++;
		}
	}


@@ 90,7 90,7 @@ int
test_dec()
{
	struct {
		unsigned long x, y;
		unsigned x, y;
	} c, cases[] = {
		{ .x = 491, .y = 5 * 491 / 6 },
		{ .x = 300, .y = 5 * 300 / 6 },


@@ 107,13 107,13 @@ test_dec()
	int fail = 0;
	for (unsigned i = 0; i < len(cases); i++) {
		c = cases[i];
		unsigned long y = dec(c.x);
		unsigned y = dec(c.x);
		if (y != c.y) {
			printf("%s case %d: expected %lu, got %lu\n", __func__, i, c.y, y);
			printf("%s case %d: expected %u, got %u\n", __func__, i, c.y, y);
			fail++;
		}
		if (c.x && index(c.x) != -1 && index(y) == index(c.x)) {
			printf("%s case %d: index(%lu) == index(dec(%lu))\n", __func__, i, y, c.x);
			printf("%s case %d: index(%u) == index(dec(%u))\n", __func__, i, y, c.x);
			fail++;
		}
	}