aboutsummaryrefslogtreecommitdiffstats
path: root/libavutil/riscv/intmath.h
blob: a09248f9030011a43c69802b7ca13ecf6ebb531b (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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright © 2022-2024 Rémi Denis-Courmont.
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg 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.1 of the License, or (at your option) any later version.
 *
 * FFmpeg 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 FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef AVUTIL_RISCV_INTMATH_H
#define AVUTIL_RISCV_INTMATH_H

#include <stdint.h>
#include <math.h>

#include "config.h"
#include "libavutil/attributes.h"
#include "libavutil/riscv/cpu.h"

/*
 * The compiler is forced to sign-extend the result anyhow, so it is faster to
 * compute it explicitly and use it.
 */
#define av_clip_int8 av_clip_int8_rvi
static av_always_inline av_const int8_t av_clip_int8_rvi(int a)
{
    union { uint8_t u; int8_t s; } u = { .u = a };

    if (a != u.s)
        a = ((a >> 31) ^ 0x7F);
    return a;
}

#define av_clip_int16 av_clip_int16_rvi
static av_always_inline av_const int16_t av_clip_int16_rvi(int a)
{
    union { uint16_t u; int16_t s; } u = { .u = a };

    if (a != u.s)
        a = ((a >> 31) ^ 0x7FFF);
    return a;
}

#define av_clipl_int32 av_clipl_int32_rvi
static av_always_inline av_const int32_t av_clipl_int32_rvi(int64_t a)
{
    union { uint32_t u; int32_t s; } u = { .u = a };

    if (a != u.s)
        a = ((a >> 63) ^ 0x7FFFFFFF);
    return a;
}

#define av_clip_intp2 av_clip_intp2_rvi
static av_always_inline av_const int av_clip_intp2_rvi(int a, int p)
{
    const int shift = 31 - p;
    int b = ((int)(((unsigned)a) << shift)) >> shift;

    if (a != b)
        b = (a >> 31) ^ ((1 << p) - 1);
    return b;
}

#if defined (__riscv_f) || defined (__riscv_zfinx)
#define av_clipf av_clipf_rvf
static av_always_inline av_const float av_clipf_rvf(float a, float min,
                                                    float max)
{
    return fminf(fmaxf(a, min), max);
}
#endif

#if defined (__riscv_d) || defined (__riscv_zdinx)
#define av_clipd av_clipd_rvd
static av_always_inline av_const double av_clipd_rvd(double a, double min,
                                                     double max)
{
    return fmin(fmax(a, min), max);
}
#endif

#if defined (__GNUC__) || defined (__clang__)
static inline av_const int ff_ctz_rv(int x)
{
#if HAVE_RV && !defined(__riscv_zbb)
    if (!__builtin_constant_p(x) &&
        __builtin_expect(ff_rv_zbb_support(), true)) {
        int y;

        __asm__ (
            ".option push\n"
            ".option arch, +zbb\n"
#if __riscv_xlen >= 64
            "ctzw    %0, %1\n"
#else
            "ctz     %0, %1\n"
#endif
            ".option pop" : "=r" (y) : "r" (x));
        if (y > 32)
            __builtin_unreachable();
        return y;
    }
#endif
    return __builtin_ctz(x);
}
#define ff_ctz ff_ctz_rv

static inline av_const int ff_ctzll_rv(long long x)
{
#if HAVE_RV && !defined(__riscv_zbb) && __riscv_xlen == 64
    if (!__builtin_constant_p(x) &&
        __builtin_expect(ff_rv_zbb_support(), true)) {
        int y;

        __asm__ (
            ".option push\n"
            ".option arch, +zbb\n"
            "ctz     %0, %1\n"
            ".option pop" : "=r" (y) : "r" (x));
        if (y > 64)
            __builtin_unreachable();
        return y;
    }
#endif
    return __builtin_ctzll(x);
}
#define ff_ctzll ff_ctzll_rv

static inline av_const int ff_clz_rv(int x)
{
#if HAVE_RV && !defined(__riscv_zbb)
    if (!__builtin_constant_p(x) &&
        __builtin_expect(ff_rv_zbb_support(), true)) {
        int y;

        __asm__ (
            ".option push\n"
            ".option arch, +zbb\n"
#if __riscv_xlen >= 64
            "clzw    %0, %1\n"
#else
            "clz     %0, %1\n"
#endif
            ".option pop" : "=r" (y) : "r" (x));
        if (y > 32)
            __builtin_unreachable();
        return y;
    }
#endif
    return __builtin_clz(x);
}
#define ff_clz ff_clz_rv

#if __riscv_xlen == 64
static inline av_const int ff_clzll_rv(long long x)
{
#if HAVE_RV && !defined(__riscv_zbb)
    if (!__builtin_constant_p(x) &&
        __builtin_expect(ff_rv_zbb_support(), true)) {
        int y;

        __asm__ (
            ".option push\n"
            ".option arch, +zbb\n"
            "clz     %0, %1\n"
            ".option pop" : "=r" (y) : "r" (x));
        if (y > 64)
            __builtin_unreachable();
        return y;
    }
#endif
    return __builtin_clzll(x);
}
#define ff_clz ff_clz_rv
#endif

static inline av_const int ff_log2_rv(unsigned int x)
{
    return 31 - ff_clz_rv(x | 1);
}
#define ff_log2 ff_log2_rv
#define ff_log2_16bit ff_log2_rv

static inline av_const int av_popcount_rv(unsigned int x)
{
#if HAVE_RV && !defined(__riscv_zbb)
    if (!__builtin_constant_p(x) &&
        __builtin_expect(ff_rv_zbb_support(), true)) {
        int y;

        __asm__ (
            ".option push\n"
            ".option arch, +zbb\n"
#if __riscv_xlen >= 64
            "cpopw   %0, %1\n"
#else
            "cpop    %0, %1\n"
#endif
            ".option pop" : "=r" (y) : "r" (x));
        if (y > 32)
            __builtin_unreachable();
        return y;
    }
#endif
    return __builtin_popcount(x);
}
#define av_popcount av_popcount_rv

static inline av_const int av_popcount64_rv(uint64_t x)
{
#if HAVE_RV && !defined(__riscv_zbb) && __riscv_xlen >= 64
    if (!__builtin_constant_p(x) &&
        __builtin_expect(ff_rv_zbb_support(), true)) {
        int y;

        __asm__ (
            ".option push\n"
            ".option arch, +zbb\n"
            "cpop    %0, %1\n"
            ".option pop" : "=r" (y) : "r" (x));
        if (y > 64)
            __builtin_unreachable();
        return y;
    }
#endif
    return __builtin_popcountl(x);
}
#define av_popcount64 av_popcount64_rv

static inline av_const int av_parity_rv(unsigned int x)
{
#if HAVE_RV && !defined(__riscv_zbb)
    if (!__builtin_constant_p(x) &&
        __builtin_expect(ff_rv_zbb_support(), true)) {
        int y;

        __asm__ (
            ".option push\n"
            ".option arch, +zbb\n"
#if __riscv_xlen >= 64
            "cpopw   %0, %1\n"
#else
            "cpop    %0, %1\n"
#endif
            ".option pop" : "=r" (y) : "r" (x));
        return y & 1;
    }
#endif
    return __builtin_parity(x);
}
#define av_parity av_parity_rv
#endif

#endif /* AVUTIL_RISCV_INTMATH_H */