xref: /qemu/tests/tcg/hexagon/hvx_histogram.c (revision 49278c1b)
1*49278c1bSTaylor Simpson /*
2*49278c1bSTaylor Simpson  *  Copyright(c) 2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
3*49278c1bSTaylor Simpson  *
4*49278c1bSTaylor Simpson  *  This program is free software; you can redistribute it and/or modify
5*49278c1bSTaylor Simpson  *  it under the terms of the GNU General Public License as published by
6*49278c1bSTaylor Simpson  *  the Free Software Foundation; either version 2 of the License, or
7*49278c1bSTaylor Simpson  *  (at your option) any later version.
8*49278c1bSTaylor Simpson  *
9*49278c1bSTaylor Simpson  *  This program is distributed in the hope that it will be useful,
10*49278c1bSTaylor Simpson  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*49278c1bSTaylor Simpson  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*49278c1bSTaylor Simpson  *  GNU General Public License for more details.
13*49278c1bSTaylor Simpson  *
14*49278c1bSTaylor Simpson  *  You should have received a copy of the GNU General Public License
15*49278c1bSTaylor Simpson  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16*49278c1bSTaylor Simpson  */
17*49278c1bSTaylor Simpson 
18*49278c1bSTaylor Simpson #include <stdio.h>
19*49278c1bSTaylor Simpson #include <stdint.h>
20*49278c1bSTaylor Simpson #include <string.h>
21*49278c1bSTaylor Simpson #include "hvx_histogram_row.h"
22*49278c1bSTaylor Simpson 
23*49278c1bSTaylor Simpson const int vector_len = 128;
24*49278c1bSTaylor Simpson const int width = 275;
25*49278c1bSTaylor Simpson const int height = 20;
26*49278c1bSTaylor Simpson const int stride = (width + vector_len - 1) & -vector_len;
27*49278c1bSTaylor Simpson 
28*49278c1bSTaylor Simpson int err;
29*49278c1bSTaylor Simpson 
30*49278c1bSTaylor Simpson static uint8_t input[height][stride] __attribute__((aligned(128))) = {
31*49278c1bSTaylor Simpson #include "hvx_histogram_input.h"
32*49278c1bSTaylor Simpson };
33*49278c1bSTaylor Simpson 
34*49278c1bSTaylor Simpson static int result[256] __attribute__((aligned(128)));
35*49278c1bSTaylor Simpson static int expect[256] __attribute__((aligned(128)));
36*49278c1bSTaylor Simpson 
check(void)37*49278c1bSTaylor Simpson static void check(void)
38*49278c1bSTaylor Simpson {
39*49278c1bSTaylor Simpson     for (int i = 0; i < 256; i++) {
40*49278c1bSTaylor Simpson         int res = result[i];
41*49278c1bSTaylor Simpson         int exp = expect[i];
42*49278c1bSTaylor Simpson         if (res != exp) {
43*49278c1bSTaylor Simpson             printf("ERROR at %3d: 0x%04x != 0x%04x\n",
44*49278c1bSTaylor Simpson                    i, res, exp);
45*49278c1bSTaylor Simpson             err++;
46*49278c1bSTaylor Simpson         }
47*49278c1bSTaylor Simpson     }
48*49278c1bSTaylor Simpson }
49*49278c1bSTaylor Simpson 
ref_histogram(uint8_t * src,int stride,int width,int height,int * hist)50*49278c1bSTaylor Simpson static void ref_histogram(uint8_t *src, int stride, int width, int height,
51*49278c1bSTaylor Simpson                           int *hist)
52*49278c1bSTaylor Simpson {
53*49278c1bSTaylor Simpson     for (int i = 0; i < 256; i++) {
54*49278c1bSTaylor Simpson         hist[i] = 0;
55*49278c1bSTaylor Simpson     }
56*49278c1bSTaylor Simpson 
57*49278c1bSTaylor Simpson     for (int i = 0; i < height; i++) {
58*49278c1bSTaylor Simpson         for (int j = 0; j < width; j++) {
59*49278c1bSTaylor Simpson             hist[src[i * stride + j]]++;
60*49278c1bSTaylor Simpson         }
61*49278c1bSTaylor Simpson     }
62*49278c1bSTaylor Simpson }
63*49278c1bSTaylor Simpson 
hvx_histogram(uint8_t * src,int stride,int width,int height,int * hist)64*49278c1bSTaylor Simpson static void hvx_histogram(uint8_t *src, int stride, int width, int height,
65*49278c1bSTaylor Simpson                           int *hist)
66*49278c1bSTaylor Simpson {
67*49278c1bSTaylor Simpson     int n = 8192 / width;
68*49278c1bSTaylor Simpson 
69*49278c1bSTaylor Simpson     for (int i = 0; i < 256; i++) {
70*49278c1bSTaylor Simpson         hist[i] = 0;
71*49278c1bSTaylor Simpson     }
72*49278c1bSTaylor Simpson 
73*49278c1bSTaylor Simpson     for (int i = 0; i < height; i += n) {
74*49278c1bSTaylor Simpson         int k = height - i > n ? n : height - i;
75*49278c1bSTaylor Simpson         hvx_histogram_row(src, stride, width, k, hist);
76*49278c1bSTaylor Simpson         src += n * stride;
77*49278c1bSTaylor Simpson     }
78*49278c1bSTaylor Simpson }
79*49278c1bSTaylor Simpson 
main()80*49278c1bSTaylor Simpson int main()
81*49278c1bSTaylor Simpson {
82*49278c1bSTaylor Simpson     ref_histogram(&input[0][0], stride, width, height, expect);
83*49278c1bSTaylor Simpson     hvx_histogram(&input[0][0], stride, width, height, result);
84*49278c1bSTaylor Simpson     check();
85*49278c1bSTaylor Simpson 
86*49278c1bSTaylor Simpson     puts(err ? "FAIL" : "PASS");
87*49278c1bSTaylor Simpson     return err ? 1 : 0;
88*49278c1bSTaylor Simpson }
89