aboutsummaryrefslogtreecommitdiffstats
path: root/src/vu-arithm.c
blob: fd0ff6ad49820ef32114866aa15c585218717dfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* See LICENSE file for copyright and license details. */
#include "stream.h"
#include "util.h"

#include <fcntl.h>
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

USAGE("right-hand-stream")

/* Because the syntax for a function returning a function pointer is disgusting. */
typedef void (*process_func)(struct stream *left, struct stream *right, size_t n);

#define LIST_OPERATORS\
	X(add, *lh += rh)\
	X(sub, *lh -= rh)\
	X(mul, *lh *= rh)\
	X(div, *lh /= rh)\
	X(exp, *lh = pow(*lh, rh))\
	X(log, *lh = log(*lh) / log(rh))\
	X(min, *lh = *lh < rh ? *lh : rh)\
	X(max, *lh = *lh > rh ? *lh : rh)

#define X(NAME, ALGO)\
	static void\
	process_lf_##NAME(struct stream *left, struct stream *right, size_t n)\
	{\
		size_t i;\
		double *lh, rh;\
		for (i = 0; i < n; i += 4 * sizeof(double)) {\
			lh = ((double *)(left->buf + i)) + 0, rh = ((double *)(right->buf + i))[0];\
			ALGO;\
			lh = ((double *)(left->buf + i)) + 1, rh = ((double *)(right->buf + i))[1];\
			ALGO;\
			lh = ((double *)(left->buf + i)) + 2, rh = ((double *)(right->buf + i))[2];\
			ALGO;\
			lh = ((double *)(left->buf + i)) + 3, rh = ((double *)(right->buf + i))[3];\
			ALGO;\
		}\
	}
LIST_OPERATORS
#undef X

static process_func
get_lf_process(const char *operation)
{
#define X(NAME, ALGO)\
	if (!strcmp(operation, #NAME)) return process_lf_##NAME;
LIST_OPERATORS
#undef X
	eprintf("algorithm not recognised: %s\n", operation);
	return NULL;
}

int
main(int argc, char *argv[])
{
	struct stream left;
	struct stream right;
	size_t n;
	process_func process = NULL;

	ENOFLAGS(argc != 2);

	left.file = "<stdin>";
	left.fd = STDIN_FILENO;
	einit_stream(&left);

	right.file = argv[1];
	right.fd = eopen(right.file, O_RDONLY);
	einit_stream(&right);

	echeck_compat(&left, &right);

	if (!strcmp(left.pixfmt, "xyza"))
		process = get_lf_process(argv[0]);
	else
		eprintf("pixel format %s is not supported, try xyza\n", left.pixfmt);

	for (;;) {
		if (left.ptr < sizeof(left.buf) && !eread_stream(&left, SIZE_MAX)) {
			close(left.fd);
			left.fd = -1;
			break;
		}
		if (right.ptr < sizeof(right.buf) && !eread_stream(&right, SIZE_MAX)) {
			close(right.fd);
			right.fd = -1;
			break;
		}

		n = left.ptr < right.ptr ? left.ptr : right.ptr;
		n -= n % left.pixel_size;
		left.ptr -= n;
		right.ptr -= n;

		process(&left, &right, n);

		ewriteall(STDOUT_FILENO, left.buf, n, "<stdout>");
		if ((n & 3) || left.ptr != right.ptr) {
			memmove(left.buf,  left.buf  + n, left.ptr);
			memmove(right.buf, right.buf + n, right.ptr);
		}
	}

	if (right.fd >= 0)
		close(right.fd);

	ewriteall(STDOUT_FILENO, left.buf, left.ptr, "<stdout>");

	if (left.fd >= 0) {
		for (;;) {
			left.ptr = 0;
			if (!eread_stream(&left, SIZE_MAX)) {
				close(left.fd);
				left.fd = -1;
				break;
			}
			ewriteall(STDOUT_FILENO, left.buf, left.ptr, "<stdout>");
		}
	}

	return 0;
}