summaryrefslogtreecommitdiffstats
path: root/third_party/aom/av1/encoder/sorting_network.h
blob: 54f4c19dcdd6c39a3e024c977eacb36f116deb13 (plain)
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
/*
 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

/*! \file
 * This file contains several utility functions used to sort small arrays with
 * sorting networks.
 *
 * Sorting network is a (potentially branch-less) way to quickly sort small
 * arrays with known size. For more details, consult
 * (https://en.wikipedia.org/wiki/Sorting_network).
 */
#ifndef AOM_AV1_ENCODER_SORTING_NETWORK_H_
#define AOM_AV1_ENCODER_SORTING_NETWORK_H_

#include "aom/aom_integer.h"

#define SWAP(i, j)                                   \
  do {                                               \
    const float maxf = (k[i] >= k[j]) ? k[i] : k[j]; \
    const float minf = (k[i] >= k[j]) ? k[j] : k[i]; \
    const int maxi = (k[i] >= k[j]) ? v[i] : v[j];   \
    const int mini = (k[i] >= k[j]) ? v[j] : v[i];   \
    k[i] = maxf;                                     \
    k[j] = minf;                                     \
    v[i] = maxi;                                     \
    v[j] = mini;                                     \
  } while (0)

/*!\brief Sorts two size-16 arrays of keys and values in descending order of
 * keys.
 *
 * \param[in,out]    k          An length-16 array of float serves as the keys.
 * \param[in,out]    v          An length-16 array of int32 serves as the
 *                              value.
 */
static AOM_INLINE void av1_sort_fi32_16(float k[], int32_t v[]) {
  SWAP(0, 1);
  SWAP(2, 3);
  SWAP(4, 5);
  SWAP(6, 7);
  SWAP(8, 9);
  SWAP(10, 11);
  SWAP(12, 13);
  SWAP(14, 15);
  SWAP(0, 2);
  SWAP(1, 3);
  SWAP(4, 6);
  SWAP(5, 7);
  SWAP(8, 10);
  SWAP(9, 11);
  SWAP(12, 14);
  SWAP(13, 15);
  SWAP(1, 2);
  SWAP(5, 6);
  SWAP(0, 4);
  SWAP(3, 7);
  SWAP(9, 10);
  SWAP(13, 14);
  SWAP(8, 12);
  SWAP(11, 15);
  SWAP(1, 5);
  SWAP(2, 6);
  SWAP(9, 13);
  SWAP(10, 14);
  SWAP(0, 8);
  SWAP(7, 15);
  SWAP(1, 4);
  SWAP(3, 6);
  SWAP(9, 12);
  SWAP(11, 14);
  SWAP(2, 4);
  SWAP(3, 5);
  SWAP(10, 12);
  SWAP(11, 13);
  SWAP(1, 9);
  SWAP(6, 14);
  SWAP(3, 4);
  SWAP(11, 12);
  SWAP(1, 8);
  SWAP(2, 10);
  SWAP(5, 13);
  SWAP(7, 14);
  SWAP(3, 11);
  SWAP(2, 8);
  SWAP(4, 12);
  SWAP(7, 13);
  SWAP(3, 10);
  SWAP(5, 12);
  SWAP(3, 9);
  SWAP(6, 12);
  SWAP(3, 8);
  SWAP(7, 12);
  SWAP(5, 9);
  SWAP(6, 10);
  SWAP(4, 8);
  SWAP(7, 11);
  SWAP(5, 8);
  SWAP(7, 10);
  SWAP(6, 8);
  SWAP(7, 9);
  SWAP(7, 8);
}

/*!\brief Sorts two size-8 arrays of keys and values in descending order of
 * keys.
 *
 * \param[in,out]    k          An length-8 array of float serves as the keys.
 * \param[in,out]    v          An length-8 array of int32 serves as the values.
 */
static AOM_INLINE void av1_sort_fi32_8(float k[], int32_t v[]) {
  SWAP(0, 1);
  SWAP(2, 3);
  SWAP(4, 5);
  SWAP(6, 7);
  SWAP(0, 2);
  SWAP(1, 3);
  SWAP(4, 6);
  SWAP(5, 7);
  SWAP(1, 2);
  SWAP(5, 6);
  SWAP(0, 4);
  SWAP(3, 7);
  SWAP(1, 5);
  SWAP(2, 6);
  SWAP(1, 4);
  SWAP(3, 6);
  SWAP(2, 4);
  SWAP(3, 5);
  SWAP(3, 4);
}
#undef SWAP
#endif  // AOM_AV1_ENCODER_SORTING_NETWORK_H_