1 /*
2 * Copyright © 2018, VideoLAN and dav1d authors
3 * Copyright © 2018, Two Orioles, LLC
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef DAV1D_SRC_CTX_H
29 #define DAV1D_SRC_CTX_H
30
31 #include <stdint.h>
32
33 #include "common/attributes.h"
34 #include "common/intops.h"
35
36 union alias64 { uint64_t u64; uint8_t u8[8]; } ATTR_ALIAS;
37 union alias32 { uint32_t u32; uint8_t u8[4]; } ATTR_ALIAS;
38 union alias16 { uint16_t u16; uint8_t u8[2]; } ATTR_ALIAS;
39 union alias8 { uint8_t u8; } ATTR_ALIAS;
40
41 typedef void (*dav1d_memset_pow2_fn)(void *ptr, int value);
42 EXTERN const dav1d_memset_pow2_fn dav1d_memset_pow2[6];
43
dav1d_memset_likely_pow2(void * const ptr,const int value,const int n)44 static inline void dav1d_memset_likely_pow2(void *const ptr, const int value, const int n) {
45 assert(n >= 1 && n <= 32);
46 if ((n&(n-1)) == 0) {
47 dav1d_memset_pow2[ulog2(n)](ptr, value);
48 } else {
49 memset(ptr, value, n);
50 }
51 }
52
53 // For smaller sizes use multiplication to broadcast bytes. memset misbehaves on the smaller sizes.
54 // For the larger sizes, we want to use memset to get access to vector operations.
55 #define set_ctx1(var, off, val) \
56 ((union alias8 *) &(var)[off])->u8 = (val) * 0x01
57 #define set_ctx2(var, off, val) \
58 ((union alias16 *) &(var)[off])->u16 = (val) * 0x0101
59 #define set_ctx4(var, off, val) \
60 ((union alias32 *) &(var)[off])->u32 = (val) * 0x01010101U
61 #define set_ctx8(var, off, val) \
62 ((union alias64 *) &(var)[off])->u64 = (val) * 0x0101010101010101ULL
63 #define set_ctx16(var, off, val) do { \
64 memset(&(var)[off], val, 16); \
65 } while (0)
66 #define set_ctx32(var, off, val) do { \
67 memset(&(var)[off], val, 32); \
68 } while (0)
69 #define case_set(var) \
70 switch (var) { \
71 case 0: set_ctx(set_ctx1); break; \
72 case 1: set_ctx(set_ctx2); break; \
73 case 2: set_ctx(set_ctx4); break; \
74 case 3: set_ctx(set_ctx8); break; \
75 case 4: set_ctx(set_ctx16); break; \
76 case 5: set_ctx(set_ctx32); break; \
77 default: assert(0); \
78 }
79 #define case_set_upto16(var) \
80 switch (var) { \
81 case 0: set_ctx(set_ctx1); break; \
82 case 1: set_ctx(set_ctx2); break; \
83 case 2: set_ctx(set_ctx4); break; \
84 case 3: set_ctx(set_ctx8); break; \
85 case 4: set_ctx(set_ctx16); break; \
86 default: assert(0); \
87 }
88
89 #endif /* DAV1D_SRC_CTX_H */
90