File: morph_hwy.cpp

package info (click to toggle)
vips 8.18.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 53,240 kB
  • sloc: ansic: 172,611; cpp: 12,257; python: 5,077; sh: 773; perl: 40; makefile: 25; javascript: 6
file content (197 lines) | stat: -rw-r--r-- 4,635 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* 24/08/22 kleisauke
 * 	- initial implementation
 * 20/08/23 kleisauke
 * 	- speed-up implementation
 */

/*

	This file is part of VIPS.

	VIPS is free software; you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
	02110-1301  USA

 */

/*

	These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>

#include <cstdio>
#include <cstdlib>
#include <cmath>

#include <vips/vips.h>
#include <vips/vector.h>
#include <vips/debug.h>
#include <vips/internal.h>

#include "pmorphology.h"

#ifdef HAVE_HWY

#undef HWY_TARGET_INCLUDE
#define HWY_TARGET_INCLUDE "libvips/morphology/morph_hwy.cpp"
#include <hwy/foreach_target.h>
#include <hwy/highway.h>

namespace HWY_NAMESPACE {

using namespace hwy::HWY_NAMESPACE;

using DU8 = ScalableTag<uint8_t>;
constexpr DU8 du8;

// Compat for Highway versions < 1.3.0
#ifndef HWY_LANES_CONSTEXPR
#define HWY_LANES_CONSTEXPR
#endif

HWY_ATTR void
vips_dilate_uchar_hwy(VipsRegion *out_region, VipsRegion *ir, VipsRect *r,
	int32_t sz, int32_t nn128, const int32_t *HWY_RESTRICT offsets,
	const uint8_t *HWY_RESTRICT coeff)
{
	int32_t bo = VIPS_RECT_BOTTOM(r);

	HWY_LANES_CONSTEXPR int32_t N = Lanes(du8);
	const auto zero = Zero(du8);
	const auto one = Set(du8, 255);

	for (int32_t y = r->top; y < bo; ++y) {
		VipsPel *HWY_RESTRICT p = VIPS_REGION_ADDR(ir, r->left, y);
		VipsPel *HWY_RESTRICT q = VIPS_REGION_ADDR(out_region, r->left, y);

		/* Main loop: unrolled.
		 */
		int32_t x = 0;
		for (; x + N <= sz; x += N) {
			auto sum = zero;

			for (int32_t i = 0; i < nn128; ++i) {
				auto mmk = Set(du8, coeff[i]);

				/* Load with an offset.
				 */
				auto pix = LoadU(du8, p + offsets[i]);

				pix = IfThenElse(Ne(mmk, one), AndNot(pix, one), pix);
				sum = Or(sum, pix);
			}

			StoreU(sum, du8, q + x);
			p += N;
		}

		/* `ne` was not a multiple of the vector length `N`;
		 * proceed one by one.
		 */
		for (; x < sz; ++x) {
			int32_t sum = 0;

			for (int32_t i = 0; i < nn128; ++i) 
				sum |= !coeff[i] ? ~p[offsets[i]] : p[offsets[i]];

			q[x] = sum;
			p += 1;
		}
	}
}

HWY_ATTR void
vips_erode_uchar_hwy(VipsRegion *out_region, VipsRegion *ir, VipsRect *r,
	int32_t sz, int32_t nn128, const int32_t *HWY_RESTRICT offsets,
	const uint8_t *HWY_RESTRICT coeff)
{
	int32_t bo = VIPS_RECT_BOTTOM(r);

	HWY_LANES_CONSTEXPR int32_t N = Lanes(du8);
	const auto one = Set(du8, 255);

	for (int32_t y = r->top; y < bo; ++y) {
		VipsPel *HWY_RESTRICT p = VIPS_REGION_ADDR(ir, r->left, y);
		VipsPel *HWY_RESTRICT q = VIPS_REGION_ADDR(out_region, r->left, y);

		/* Main loop: unrolled.
		 */
		int32_t x = 0;
		for (; x + N <= sz; x += N) {
			auto sum = one;

			for (int32_t i = 0; i < nn128; ++i) {
				auto mmk = Set(du8, coeff[i]);

				/* Load with an offset.
				 */
				auto pix = LoadU(du8, p + offsets[i]);

				pix = IfThenElse(Ne(mmk, one), AndNot(pix, one), pix);
				sum = And(sum, pix);
			}

			StoreU(sum, du8, q + x);
			p += N;
		}

		/* `ne` was not a multiple of the vector length `N`;
		 * proceed one by one.
		 */
		for (; x < sz; ++x) {
			int32_t sum = 255;

			for (int32_t i = 0; i < nn128; ++i)
				sum &= !coeff[i] ? ~p[offsets[i]] : p[offsets[i]];

			q[x] = sum;
			p += 1;
		}
	}
}

} /*namespace HWY_NAMESPACE*/

#if HWY_ONCE
HWY_EXPORT(vips_dilate_uchar_hwy);
HWY_EXPORT(vips_erode_uchar_hwy);

void
vips_dilate_uchar_hwy(VipsRegion *out_region, VipsRegion *ir, VipsRect *r,
	int sz, int nn128, int *restrict offsets, guint8 *restrict coeff)
{
	/* clang-format off */
	HWY_DYNAMIC_DISPATCH(vips_dilate_uchar_hwy)(out_region, ir, r, sz,
		nn128, offsets, coeff);
	/* clang-format on */
}

void
vips_erode_uchar_hwy(VipsRegion *out_region, VipsRegion *ir, VipsRect *r,
	int sz, int nn128, int *restrict offsets, guint8 *restrict coeff)
{
	/* clang-format off */
	HWY_DYNAMIC_DISPATCH(vips_erode_uchar_hwy)(out_region, ir, r, sz,
		nn128, offsets, coeff);
	/* clang-format on */
}
#endif /*HWY_ONCE*/

#endif /*HAVE_HWY*/