File: binops.ct

package info (click to toggle)
tela 1.28-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 6,596 kB
  • ctags: 5,519
  • sloc: ansic: 14,013; cpp: 13,376; lex: 1,651; fortran: 1,048; yacc: 834; sh: 715; makefile: 464
file content (146 lines) | stat: -rw-r--r-- 4,023 bytes parent folder | download
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * This file is part of tela the Tensor Language.
 * Copyright (c) 1994-1996 Pekka Janhunen
 */

/*
	binops.ct
	Binary AND, OR, XOR, NOT  as band, bor, bxor, bnot
	Preprocess with ctpp.
	C-tela code is C++ equipped with []=f() style function definition.
*/

[u] = band(x,y)
/* band(x,y) is the binary AND.
   The arguments must be integer arrays or scalars.
   If they are arrays, they must be of the same dimensions.
   See also: bor, bxor, bnot.
   Error codes:
   -1: First argument not integer
   -2: Second argument not integer
   -3: Arrays are of incompatible dimensions
*/
{
	const Tkind xk = x.kind();
	const Tkind yk = y.kind();
	if (xk!=Kint && xk!=KIntArray) return -1;
	if (yk!=Kint && yk!=KIntArray) return -2;
	Tint i;
	if (xk == Kint && yk == Kint) {
		u = x.IntValue() & y.IntValue();
	} else if (xk == Kint && yk == KIntArray) {
		const Tint N = y.length();
		u.ireserv(y.dims());
		VECTORIZED for (i=0; i<N; i++)
			u.IntPtr()[i] = x.IntValue() & y.IntPtr()[i];
	} else if (xk == KIntArray && yk == Kint) {
		const Tint N = x.length();
		u.ireserv(x.dims());
		VECTORIZED for (i=0; i<N; i++)
			u.IntPtr()[i] = x.IntPtr()[i] & y.IntValue();
	} else {
		if (x.dims() != y.dims()) return -3;
		const Tint N = x.length();
		u.ireserv(x.dims());
		VECTORIZED for (i=0; i<N; i++)
			u.IntPtr()[i] = x.IntPtr()[i] & y.IntPtr()[i];
	}
	return 0;
}

[u] = bor(x,y)
/* bor(x,y) is the binary OR.
   The arguments must be integer arrays or scalars.
   If they are arrays, they must be of the same dimensions.
   See also: band, bxor, bnot.
   Error codes:
   -1: First argument not integer
   -2: Second argument not integer
   -3: Arrays are of incompatible dimensions
*/
{
	const Tkind xk = x.kind();
	const Tkind yk = y.kind();
	if (xk!=Kint && xk!=KIntArray) return -1;
	if (yk!=Kint && yk!=KIntArray) return -2;
	Tint i;
	if (xk == Kint && yk == Kint) {
		u = x.IntValue() | y.IntValue();
	} else if (xk == Kint && yk == KIntArray) {
		const Tint N = y.length();
		u.ireserv(y.dims());
		VECTORIZED for (i=0; i<N; i++)
			u.IntPtr()[i] = x.IntValue() | y.IntPtr()[i];
	} else if (xk == KIntArray && yk == Kint) {
		const Tint N = x.length();
		u.ireserv(x.dims());
		VECTORIZED for (i=0; i<N; i++)
			u.IntPtr()[i] = x.IntPtr()[i] | y.IntValue();
	} else {
		if (x.dims() != y.dims()) return -3;
		const Tint N = x.length();
		u.ireserv(x.dims());
		VECTORIZED for (i=0; i<N; i++)
			u.IntPtr()[i] = x.IntPtr()[i] | y.IntPtr()[i];
	}
	return 0;
}

[u] = bxor(x,y)
/* bxor(x,y) is the binary XOR (exclusive OR).
   The arguments must be integer arrays or scalars.
   If they are arrays, they must be of the same dimensions.
   See also: band, bor, bnot.
   Error codes:
   -1: First argument not integer
   -2: Second argument not integer
   -3: Arrays are of incompatible dimensions
*/
{
	const Tkind xk = x.kind();
	const Tkind yk = y.kind();
	if (xk!=Kint && xk!=KIntArray) return -1;
	if (yk!=Kint && yk!=KIntArray) return -2;
	Tint i;
	if (xk == Kint && yk == Kint) {
		u = x.IntValue() ^ y.IntValue();
	} else if (xk == Kint && yk == KIntArray) {
		const Tint N = y.length();
		u.ireserv(y.dims());
		VECTORIZED for (i=0; i<N; i++)
			u.IntPtr()[i] = x.IntValue() ^ y.IntPtr()[i];
	} else if (xk == KIntArray && yk == Kint) {
		const Tint N = x.length();
		u.ireserv(x.dims());
		VECTORIZED for (i=0; i<N; i++)
			u.IntPtr()[i] = x.IntPtr()[i] ^ y.IntValue();
	} else {
		if (x.dims() != y.dims()) return -3;
		const Tint N = x.length();
		u.ireserv(x.dims());
		VECTORIZED for (i=0; i<N; i++)
			u.IntPtr()[i] = x.IntPtr()[i] ^ y.IntPtr()[i];
	}
	return 0;
}

[u] = bnot(x)
/* bnot(x) is the binary NOT operation.
   The argument must be an integer scalar or array.
   See also: band, bor, bxor.
   Error codes:
   -1: Argument not integer
*/
{
	if (x.kind()!=Kint && x.kind()!=KIntArray) return -1;
	if (x.kind()==Kint) {
		u = ~x.IntValue();
	} else {
		const Tint N = x.length();
		Tint i;
		u.ireserv(x.dims());
		VECTORIZED for (i=0; i<N; i++)
			u.IntPtr()[i] = ~x.IntPtr()[i];
	}
	return 0;
}