Bug Summary

File:dev/pci/drm/amd/display/include/fixed31_32.h
Warning:line 215, column 24
The result of the left shift is undefined because the left operand is negative

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name custom_float.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -ffreestanding -mcmodel=kernel -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse2 -target-feature -sse -target-feature -3dnow -target-feature -mmx -target-feature +save-args -target-feature +retpoline-external-thunk -disable-red-zone -no-implicit-float -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -nostdsysteminc -nobuiltininc -resource-dir /usr/local/llvm16/lib/clang/16 -I /usr/src/sys -I /usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -I /usr/src/sys/arch -I /usr/src/sys/dev/pci/drm/include -I /usr/src/sys/dev/pci/drm/include/uapi -I /usr/src/sys/dev/pci/drm/amd/include/asic_reg -I /usr/src/sys/dev/pci/drm/amd/include -I /usr/src/sys/dev/pci/drm/amd/amdgpu -I /usr/src/sys/dev/pci/drm/amd/display -I /usr/src/sys/dev/pci/drm/amd/display/include -I /usr/src/sys/dev/pci/drm/amd/display/dc -I /usr/src/sys/dev/pci/drm/amd/display/amdgpu_dm -I /usr/src/sys/dev/pci/drm/amd/pm/inc -I /usr/src/sys/dev/pci/drm/amd/pm/legacy-dpm -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu11 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu12 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu13 -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/inc -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/hwmgr -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/smumgr -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/inc/pmfw_if -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc/hw -I /usr/src/sys/dev/pci/drm/amd/display/dc/clk_mgr -I /usr/src/sys/dev/pci/drm/amd/display/modules/inc -I /usr/src/sys/dev/pci/drm/amd/display/modules/hdcp -I /usr/src/sys/dev/pci/drm/amd/display/dmub/inc -I /usr/src/sys/dev/pci/drm/i915 -D DDB -D DIAGNOSTIC -D KTRACE -D ACCOUNTING -D KMEMSTATS -D PTRACE -D POOL_DEBUG -D CRYPTO -D SYSVMSG -D SYSVSEM -D SYSVSHM -D UVM_SWAP_ENCRYPT -D FFS -D FFS2 -D FFS_SOFTUPDATES -D UFS_DIRHASH -D QUOTA -D EXT2FS -D MFS -D NFSCLIENT -D NFSSERVER -D CD9660 -D UDF -D MSDOSFS -D FIFO -D FUSE -D SOCKET_SPLICE -D TCP_ECN -D TCP_SIGNATURE -D INET6 -D IPSEC -D PPP_BSDCOMP -D PPP_DEFLATE -D PIPEX -D MROUTING -D MPLS -D BOOT_CONFIG -D USER_PCICONF -D APERTURE -D MTRR -D NTFS -D SUSPEND -D HIBERNATE -D PCIVERBOSE -D USBVERBOSE -D WSDISPLAY_COMPAT_USL -D WSDISPLAY_COMPAT_RAWKBD -D WSDISPLAY_DEFAULTSCREENS=6 -D X86EMU -D ONEWIREVERBOSE -D MULTIPROCESSOR -D MAXUSERS=80 -D _KERNEL -O2 -Wno-pointer-sign -Wno-address-of-packed-member -Wno-constant-conversion -Wno-unused-but-set-variable -Wno-gnu-folding-constant -fdebug-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /home/ben/Projects/scan/2024-01-11-110808-61670-1 -x c /usr/src/sys/dev/pci/drm/amd/display/dc/dml/calcs/custom_float.c

/usr/src/sys/dev/pci/drm/amd/display/dc/dml/calcs/custom_float.c

1/*
2 * Copyright 2017 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: AMD
23 *
24 */
25#include "dm_services.h"
26#include "custom_float.h"
27
28
29static bool_Bool build_custom_float(
30 struct fixed31_32 value,
31 const struct custom_float_format *format,
32 bool_Bool *negative,
33 uint32_t *mantissa,
34 uint32_t *exponenta)
35{
36 uint32_t exp_offset = (1 << (format->exponenta_bits - 1)) - 1;
37
38 const struct fixed31_32 mantissa_constant_plus_max_fraction =
39 dc_fixpt_from_fraction(
40 (1LL << (format->mantissa_bits + 1)) - 1,
41 1LL << format->mantissa_bits);
42
43 struct fixed31_32 mantiss;
44
45 if (dc_fixpt_eq(
2
Taking false branch
46 value,
47 dc_fixpt_zero)) {
48 *negative = false0;
49 *mantissa = 0;
50 *exponenta = 0;
51 return true1;
52 }
53
54 if (dc_fixpt_lt(
3
Taking true branch
55 value,
56 dc_fixpt_zero)) {
57 *negative = format->sign;
58 value = dc_fixpt_neg(value);
59 } else {
60 *negative = false0;
61 }
62
63 if (dc_fixpt_lt(
4
Taking true branch
64 value,
65 dc_fixpt_one)) {
66 uint32_t i = 1;
67
68 do {
69 value = dc_fixpt_shl(value, 1);
5
Value assigned to 'arg.value'
6
Calling 'dc_fixpt_shl'
70 ++i;
71 } while (dc_fixpt_lt(
72 value,
73 dc_fixpt_one));
74
75 --i;
76
77 if (exp_offset <= i) {
78 *mantissa = 0;
79 *exponenta = 0;
80 return true1;
81 }
82
83 *exponenta = exp_offset - i;
84 } else if (dc_fixpt_le(
85 mantissa_constant_plus_max_fraction,
86 value)) {
87 uint32_t i = 1;
88
89 do {
90 value = dc_fixpt_shr(value, 1);
91 ++i;
92 } while (dc_fixpt_lt(
93 mantissa_constant_plus_max_fraction,
94 value));
95
96 *exponenta = exp_offset + i - 1;
97 } else {
98 *exponenta = exp_offset;
99 }
100
101 mantiss = dc_fixpt_sub(
102 value,
103 dc_fixpt_one);
104
105 if (dc_fixpt_lt(
106 mantiss,
107 dc_fixpt_zero) ||
108 dc_fixpt_lt(
109 dc_fixpt_one,
110 mantiss))
111 mantiss = dc_fixpt_zero;
112 else
113 mantiss = dc_fixpt_shl(
114 mantiss,
115 format->mantissa_bits);
116
117 *mantissa = dc_fixpt_floor(mantiss);
118
119 return true1;
120}
121
122static bool_Bool setup_custom_float(
123 const struct custom_float_format *format,
124 bool_Bool negative,
125 uint32_t mantissa,
126 uint32_t exponenta,
127 uint32_t *result)
128{
129 uint32_t i = 0;
130 uint32_t j = 0;
131
132 uint32_t value = 0;
133
134 /* verification code:
135 * once calculation is ok we can remove it
136 */
137
138 const uint32_t mantissa_mask =
139 (1 << (format->mantissa_bits + 1)) - 1;
140
141 const uint32_t exponenta_mask =
142 (1 << (format->exponenta_bits + 1)) - 1;
143
144 if (mantissa & ~mantissa_mask) {
145 BREAK_TO_DEBUGGER()do { ___drm_dbg(((void *)0), DRM_UT_DRIVER, "%s():%d\n", __func__
, 145); do {} while (0); } while (0)
;
146 mantissa = mantissa_mask;
147 }
148
149 if (exponenta & ~exponenta_mask) {
150 BREAK_TO_DEBUGGER()do { ___drm_dbg(((void *)0), DRM_UT_DRIVER, "%s():%d\n", __func__
, 150); do {} while (0); } while (0)
;
151 exponenta = exponenta_mask;
152 }
153
154 /* end of verification code */
155
156 while (i < format->mantissa_bits) {
157 uint32_t mask = 1 << i;
158
159 if (mantissa & mask)
160 value |= mask;
161
162 ++i;
163 }
164
165 while (j < format->exponenta_bits) {
166 uint32_t mask = 1 << j;
167
168 if (exponenta & mask)
169 value |= mask << i;
170
171 ++j;
172 }
173
174 if (negative && format->sign)
175 value |= 1 << (i + j);
176
177 *result = value;
178
179 return true1;
180}
181
182bool_Bool convert_to_custom_float_format(
183 struct fixed31_32 value,
184 const struct custom_float_format *format,
185 uint32_t *result)
186{
187 uint32_t mantissa;
188 uint32_t exponenta;
189 bool_Bool negative;
190
191 return build_custom_float(
1
Calling 'build_custom_float'
192 value, format, &negative, &mantissa, &exponenta) &&
193 setup_custom_float(
194 format, negative, mantissa, exponenta, result);
195}
196
197

/usr/src/sys/dev/pci/drm/amd/display/include/fixed31_32.h

1/*
2 * Copyright 2012-15 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: AMD
23 *
24 */
25
26#ifndef __DAL_FIXED31_32_H__
27#define __DAL_FIXED31_32_H__
28
29#ifndef LLONG_MAX0x7fffffffffffffffLL
30#define LLONG_MAX0x7fffffffffffffffLL 9223372036854775807ll
31#endif
32#ifndef LLONG_MIN(-0x7fffffffffffffffLL-1)
33#define LLONG_MIN(-0x7fffffffffffffffLL-1) (-LLONG_MAX0x7fffffffffffffffLL - 1ll)
34#endif
35
36#define FIXED31_32_BITS_PER_FRACTIONAL_PART32 32
37#ifndef LLONG_MIN(-0x7fffffffffffffffLL-1)
38#define LLONG_MIN(-0x7fffffffffffffffLL-1) (1LL<<63)
39#endif
40#ifndef LLONG_MAX0x7fffffffffffffffLL
41#define LLONG_MAX0x7fffffffffffffffLL (-1LL>>1)
42#endif
43
44/*
45 * @brief
46 * Arithmetic operations on real numbers
47 * represented as fixed-point numbers.
48 * There are: 1 bit for sign,
49 * 31 bit for integer part,
50 * 32 bits for fractional part.
51 *
52 * @note
53 * Currently, overflows and underflows are asserted;
54 * no special result returned.
55 */
56
57struct fixed31_32 {
58 long long value;
59};
60
61
62/*
63 * @brief
64 * Useful constants
65 */
66
67static const struct fixed31_32 dc_fixpt_zero = { 0 };
68static const struct fixed31_32 dc_fixpt_epsilon = { 1LL };
69static const struct fixed31_32 dc_fixpt_half = { 0x80000000LL };
70static const struct fixed31_32 dc_fixpt_one = { 0x100000000LL };
71
72/*
73 * @brief
74 * Initialization routines
75 */
76
77/*
78 * @brief
79 * result = numerator / denominator
80 */
81struct fixed31_32 dc_fixpt_from_fraction(long long numerator, long long denominator);
82
83/*
84 * @brief
85 * result = arg
86 */
87static inline struct fixed31_32 dc_fixpt_from_int(int arg)
88{
89 struct fixed31_32 res;
90
91 res.value = (long long) arg << FIXED31_32_BITS_PER_FRACTIONAL_PART32;
92
93 return res;
94}
95
96/*
97 * @brief
98 * Unary operators
99 */
100
101/*
102 * @brief
103 * result = -arg
104 */
105static inline struct fixed31_32 dc_fixpt_neg(struct fixed31_32 arg)
106{
107 struct fixed31_32 res;
108
109 res.value = -arg.value;
110
111 return res;
112}
113
114/*
115 * @brief
116 * result = abs(arg) := (arg >= 0) ? arg : -arg
117 */
118static inline struct fixed31_32 dc_fixpt_abs(struct fixed31_32 arg)
119{
120 if (arg.value < 0)
121 return dc_fixpt_neg(arg);
122 else
123 return arg;
124}
125
126/*
127 * @brief
128 * Binary relational operators
129 */
130
131/*
132 * @brief
133 * result = arg1 < arg2
134 */
135static inline bool_Bool dc_fixpt_lt(struct fixed31_32 arg1, struct fixed31_32 arg2)
136{
137 return arg1.value < arg2.value;
138}
139
140/*
141 * @brief
142 * result = arg1 <= arg2
143 */
144static inline bool_Bool dc_fixpt_le(struct fixed31_32 arg1, struct fixed31_32 arg2)
145{
146 return arg1.value <= arg2.value;
147}
148
149/*
150 * @brief
151 * result = arg1 == arg2
152 */
153static inline bool_Bool dc_fixpt_eq(struct fixed31_32 arg1, struct fixed31_32 arg2)
154{
155 return arg1.value == arg2.value;
156}
157
158/*
159 * @brief
160 * result = min(arg1, arg2) := (arg1 <= arg2) ? arg1 : arg2
161 */
162static inline struct fixed31_32 dc_fixpt_min(struct fixed31_32 arg1, struct fixed31_32 arg2)
163{
164 if (arg1.value <= arg2.value)
165 return arg1;
166 else
167 return arg2;
168}
169
170/*
171 * @brief
172 * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1
173 */
174static inline struct fixed31_32 dc_fixpt_max(struct fixed31_32 arg1, struct fixed31_32 arg2)
175{
176 if (arg1.value <= arg2.value)
177 return arg2;
178 else
179 return arg1;
180}
181
182/*
183 * @brief
184 * | min_value, when arg <= min_value
185 * result = | arg, when min_value < arg < max_value
186 * | max_value, when arg >= max_value
187 */
188static inline struct fixed31_32 dc_fixpt_clamp(
189 struct fixed31_32 arg,
190 struct fixed31_32 min_value,
191 struct fixed31_32 max_value)
192{
193 if (dc_fixpt_le(arg, min_value))
194 return min_value;
195 else if (dc_fixpt_le(max_value, arg))
196 return max_value;
197 else
198 return arg;
199}
200
201/*
202 * @brief
203 * Binary shift operators
204 */
205
206/*
207 * @brief
208 * result = arg << shift
209 */
210static inline struct fixed31_32 dc_fixpt_shl(struct fixed31_32 arg, unsigned char shift)
211{
212 ASSERT(((arg.value >= 0) && (arg.value <= LLONG_MAX >> shift)) ||do { if (({ static int __warned; int __ret = !!(!(((arg.value
>= 0) && (arg.value <= 0x7fffffffffffffffLL >>
shift)) || ((arg.value < 0) && (arg.value >= ~
(0x7fffffffffffffffLL >> shift))))); if (__ret &&
!__warned) { printf("WARNING %s failed at %s:%d\n", "!(((arg.value >= 0) && (arg.value <= 0x7fffffffffffffffLL >> shift)) || ((arg.value < 0) && (arg.value >= ~(0x7fffffffffffffffLL >> shift))))"
, "/usr/src/sys/dev/pci/drm/amd/display/include/fixed31_32.h"
, 213); __warned = 1; } __builtin_expect(!!(__ret), 0); })) do
{} while (0); } while (0)
7
Assuming field 'value' is < 0
8
Taking true branch
9
Taking true branch
10
Loop condition is false. Exiting loop
11
Loop condition is false. Exiting loop
213 ((arg.value < 0) && (arg.value >= ~(LLONG_MAX >> shift))))do { if (({ static int __warned; int __ret = !!(!(((arg.value
>= 0) && (arg.value <= 0x7fffffffffffffffLL >>
shift)) || ((arg.value < 0) && (arg.value >= ~
(0x7fffffffffffffffLL >> shift))))); if (__ret &&
!__warned) { printf("WARNING %s failed at %s:%d\n", "!(((arg.value >= 0) && (arg.value <= 0x7fffffffffffffffLL >> shift)) || ((arg.value < 0) && (arg.value >= ~(0x7fffffffffffffffLL >> shift))))"
, "/usr/src/sys/dev/pci/drm/amd/display/include/fixed31_32.h"
, 213); __warned = 1; } __builtin_expect(!!(__ret), 0); })) do
{} while (0); } while (0)
;
214
215 arg.value = arg.value << shift;
12
The result of the left shift is undefined because the left operand is negative
216
217 return arg;
218}
219
220/*
221 * @brief
222 * result = arg >> shift
223 */
224static inline struct fixed31_32 dc_fixpt_shr(struct fixed31_32 arg, unsigned char shift)
225{
226 bool_Bool negative = arg.value < 0;
227
228 if (negative)
229 arg.value = -arg.value;
230 arg.value = arg.value >> shift;
231 if (negative)
232 arg.value = -arg.value;
233 return arg;
234}
235
236/*
237 * @brief
238 * Binary additive operators
239 */
240
241/*
242 * @brief
243 * result = arg1 + arg2
244 */
245static inline struct fixed31_32 dc_fixpt_add(struct fixed31_32 arg1, struct fixed31_32 arg2)
246{
247 struct fixed31_32 res;
248
249 ASSERT(((arg1.value >= 0) && (LLONG_MAX - arg1.value >= arg2.value)) ||do { if (({ static int __warned; int __ret = !!(!(((arg1.value
>= 0) && (0x7fffffffffffffffLL - arg1.value >=
arg2.value)) || ((arg1.value < 0) && ((-0x7fffffffffffffffLL
-1) - arg1.value <= arg2.value)))); if (__ret && !
__warned) { printf("WARNING %s failed at %s:%d\n", "!(((arg1.value >= 0) && (0x7fffffffffffffffLL - arg1.value >= arg2.value)) || ((arg1.value < 0) && ((-0x7fffffffffffffffLL-1) - arg1.value <= arg2.value)))"
, "/usr/src/sys/dev/pci/drm/amd/display/include/fixed31_32.h"
, 250); __warned = 1; } __builtin_expect(!!(__ret), 0); })) do
{} while (0); } while (0)
250 ((arg1.value < 0) && (LLONG_MIN - arg1.value <= arg2.value)))do { if (({ static int __warned; int __ret = !!(!(((arg1.value
>= 0) && (0x7fffffffffffffffLL - arg1.value >=
arg2.value)) || ((arg1.value < 0) && ((-0x7fffffffffffffffLL
-1) - arg1.value <= arg2.value)))); if (__ret && !
__warned) { printf("WARNING %s failed at %s:%d\n", "!(((arg1.value >= 0) && (0x7fffffffffffffffLL - arg1.value >= arg2.value)) || ((arg1.value < 0) && ((-0x7fffffffffffffffLL-1) - arg1.value <= arg2.value)))"
, "/usr/src/sys/dev/pci/drm/amd/display/include/fixed31_32.h"
, 250); __warned = 1; } __builtin_expect(!!(__ret), 0); })) do
{} while (0); } while (0)
;
251
252 res.value = arg1.value + arg2.value;
253
254 return res;
255}
256
257/*
258 * @brief
259 * result = arg1 + arg2
260 */
261static inline struct fixed31_32 dc_fixpt_add_int(struct fixed31_32 arg1, int arg2)
262{
263 return dc_fixpt_add(arg1, dc_fixpt_from_int(arg2));
264}
265
266/*
267 * @brief
268 * result = arg1 - arg2
269 */
270static inline struct fixed31_32 dc_fixpt_sub(struct fixed31_32 arg1, struct fixed31_32 arg2)
271{
272 struct fixed31_32 res;
273
274 ASSERT(((arg2.value >= 0) && (LLONG_MIN + arg2.value <= arg1.value)) ||do { if (({ static int __warned; int __ret = !!(!(((arg2.value
>= 0) && ((-0x7fffffffffffffffLL-1) + arg2.value <=
arg1.value)) || ((arg2.value < 0) && (0x7fffffffffffffffLL
+ arg2.value >= arg1.value)))); if (__ret && !__warned
) { printf("WARNING %s failed at %s:%d\n", "!(((arg2.value >= 0) && ((-0x7fffffffffffffffLL-1) + arg2.value <= arg1.value)) || ((arg2.value < 0) && (0x7fffffffffffffffLL + arg2.value >= arg1.value)))"
, "/usr/src/sys/dev/pci/drm/amd/display/include/fixed31_32.h"
, 275); __warned = 1; } __builtin_expect(!!(__ret), 0); })) do
{} while (0); } while (0)
275 ((arg2.value < 0) && (LLONG_MAX + arg2.value >= arg1.value)))do { if (({ static int __warned; int __ret = !!(!(((arg2.value
>= 0) && ((-0x7fffffffffffffffLL-1) + arg2.value <=
arg1.value)) || ((arg2.value < 0) && (0x7fffffffffffffffLL
+ arg2.value >= arg1.value)))); if (__ret && !__warned
) { printf("WARNING %s failed at %s:%d\n", "!(((arg2.value >= 0) && ((-0x7fffffffffffffffLL-1) + arg2.value <= arg1.value)) || ((arg2.value < 0) && (0x7fffffffffffffffLL + arg2.value >= arg1.value)))"
, "/usr/src/sys/dev/pci/drm/amd/display/include/fixed31_32.h"
, 275); __warned = 1; } __builtin_expect(!!(__ret), 0); })) do
{} while (0); } while (0)
;
276
277 res.value = arg1.value - arg2.value;
278
279 return res;
280}
281
282/*
283 * @brief
284 * result = arg1 - arg2
285 */
286static inline struct fixed31_32 dc_fixpt_sub_int(struct fixed31_32 arg1, int arg2)
287{
288 return dc_fixpt_sub(arg1, dc_fixpt_from_int(arg2));
289}
290
291
292/*
293 * @brief
294 * Binary multiplicative operators
295 */
296
297/*
298 * @brief
299 * result = arg1 * arg2
300 */
301struct fixed31_32 dc_fixpt_mul(struct fixed31_32 arg1, struct fixed31_32 arg2);
302
303
304/*
305 * @brief
306 * result = arg1 * arg2
307 */
308static inline struct fixed31_32 dc_fixpt_mul_int(struct fixed31_32 arg1, int arg2)
309{
310 return dc_fixpt_mul(arg1, dc_fixpt_from_int(arg2));
311}
312
313/*
314 * @brief
315 * result = square(arg) := arg * arg
316 */
317struct fixed31_32 dc_fixpt_sqr(struct fixed31_32 arg);
318
319/*
320 * @brief
321 * result = arg1 / arg2
322 */
323static inline struct fixed31_32 dc_fixpt_div_int(struct fixed31_32 arg1, long long arg2)
324{
325 return dc_fixpt_from_fraction(arg1.value, dc_fixpt_from_int((int)arg2).value);
326}
327
328/*
329 * @brief
330 * result = arg1 / arg2
331 */
332static inline struct fixed31_32 dc_fixpt_div(struct fixed31_32 arg1, struct fixed31_32 arg2)
333{
334 return dc_fixpt_from_fraction(arg1.value, arg2.value);
335}
336
337/*
338 * @brief
339 * Reciprocal function
340 */
341
342/*
343 * @brief
344 * result = reciprocal(arg) := 1 / arg
345 *
346 * @note
347 * No special actions taken in case argument is zero.
348 */
349struct fixed31_32 dc_fixpt_recip(struct fixed31_32 arg);
350
351/*
352 * @brief
353 * Trigonometric functions
354 */
355
356/*
357 * @brief
358 * result = sinc(arg) := sin(arg) / arg
359 *
360 * @note
361 * Argument specified in radians,
362 * internally it's normalized to [-2pi...2pi] range.
363 */
364struct fixed31_32 dc_fixpt_sinc(struct fixed31_32 arg);
365
366/*
367 * @brief
368 * result = sin(arg)
369 *
370 * @note
371 * Argument specified in radians,
372 * internally it's normalized to [-2pi...2pi] range.
373 */
374struct fixed31_32 dc_fixpt_sin(struct fixed31_32 arg);
375
376/*
377 * @brief
378 * result = cos(arg)
379 *
380 * @note
381 * Argument specified in radians
382 * and should be in [-2pi...2pi] range -
383 * passing arguments outside that range
384 * will cause incorrect result!
385 */
386struct fixed31_32 dc_fixpt_cos(struct fixed31_32 arg);
387
388/*
389 * @brief
390 * Transcendent functions
391 */
392
393/*
394 * @brief
395 * result = exp(arg)
396 *
397 * @note
398 * Currently, function is verified for abs(arg) <= 1.
399 */
400struct fixed31_32 dc_fixpt_exp(struct fixed31_32 arg);
401
402/*
403 * @brief
404 * result = log(arg)
405 *
406 * @note
407 * Currently, abs(arg) should be less than 1.
408 * No normalization is done.
409 * Currently, no special actions taken
410 * in case of invalid argument(s). Take care!
411 */
412struct fixed31_32 dc_fixpt_log(struct fixed31_32 arg);
413
414/*
415 * @brief
416 * Power function
417 */
418
419/*
420 * @brief
421 * result = pow(arg1, arg2)
422 *
423 * @note
424 * Currently, abs(arg1) should be less than 1. Take care!
425 */
426static inline struct fixed31_32 dc_fixpt_pow(struct fixed31_32 arg1, struct fixed31_32 arg2)
427{
428 if (arg1.value == 0)
429 return arg2.value == 0 ? dc_fixpt_one : dc_fixpt_zero;
430
431 return dc_fixpt_exp(
432 dc_fixpt_mul(
433 dc_fixpt_log(arg1),
434 arg2));
435}
436
437/*
438 * @brief
439 * Rounding functions
440 */
441
442/*
443 * @brief
444 * result = floor(arg) := greatest integer lower than or equal to arg
445 */
446static inline int dc_fixpt_floor(struct fixed31_32 arg)
447{
448 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
449
450 if (arg.value >= 0)
451 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART32);
452 else
453 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART32);
454}
455
456/*
457 * @brief
458 * result = round(arg) := integer nearest to arg
459 */
460static inline int dc_fixpt_round(struct fixed31_32 arg)
461{
462 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
463
464 const long long summand = dc_fixpt_half.value;
465
466 ASSERT(LLONG_MAX - (long long)arg_value >= summand)do { if (({ static int __warned; int __ret = !!(!(0x7fffffffffffffffLL
- (long long)arg_value >= summand)); if (__ret &&
!__warned) { printf("WARNING %s failed at %s:%d\n", "!(0x7fffffffffffffffLL - (long long)arg_value >= summand)"
, "/usr/src/sys/dev/pci/drm/amd/display/include/fixed31_32.h"
, 466); __warned = 1; } __builtin_expect(!!(__ret), 0); })) do
{} while (0); } while (0)
;
467
468 arg_value += summand;
469
470 if (arg.value >= 0)
471 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART32);
472 else
473 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART32);
474}
475
476/*
477 * @brief
478 * result = ceil(arg) := lowest integer greater than or equal to arg
479 */
480static inline int dc_fixpt_ceil(struct fixed31_32 arg)
481{
482 unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
483
484 const long long summand = dc_fixpt_one.value -
485 dc_fixpt_epsilon.value;
486
487 ASSERT(LLONG_MAX - (long long)arg_value >= summand)do { if (({ static int __warned; int __ret = !!(!(0x7fffffffffffffffLL
- (long long)arg_value >= summand)); if (__ret &&
!__warned) { printf("WARNING %s failed at %s:%d\n", "!(0x7fffffffffffffffLL - (long long)arg_value >= summand)"
, "/usr/src/sys/dev/pci/drm/amd/display/include/fixed31_32.h"
, 487); __warned = 1; } __builtin_expect(!!(__ret), 0); })) do
{} while (0); } while (0)
;
488
489 arg_value += summand;
490
491 if (arg.value >= 0)
492 return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART32);
493 else
494 return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART32);
495}
496
497/* the following two function are used in scaler hw programming to convert fixed
498 * point value to format 2 bits from integer part and 19 bits from fractional
499 * part. The same applies for u0d19, 0 bits from integer part and 19 bits from
500 * fractional
501 */
502
503unsigned int dc_fixpt_u4d19(struct fixed31_32 arg);
504
505unsigned int dc_fixpt_u3d19(struct fixed31_32 arg);
506
507unsigned int dc_fixpt_u2d19(struct fixed31_32 arg);
508
509unsigned int dc_fixpt_u0d19(struct fixed31_32 arg);
510
511unsigned int dc_fixpt_clamp_u0d14(struct fixed31_32 arg);
512
513unsigned int dc_fixpt_clamp_u0d10(struct fixed31_32 arg);
514
515int dc_fixpt_s4d19(struct fixed31_32 arg);
516
517static inline struct fixed31_32 dc_fixpt_truncate(struct fixed31_32 arg, unsigned int frac_bits)
518{
519 bool_Bool negative = arg.value < 0;
520
521 if (frac_bits >= FIXED31_32_BITS_PER_FRACTIONAL_PART32) {
522 ASSERT(frac_bits == FIXED31_32_BITS_PER_FRACTIONAL_PART)do { if (({ static int __warned; int __ret = !!(!(frac_bits ==
32)); if (__ret && !__warned) { printf("WARNING %s failed at %s:%d\n"
, "!(frac_bits == 32)", "/usr/src/sys/dev/pci/drm/amd/display/include/fixed31_32.h"
, 522); __warned = 1; } __builtin_expect(!!(__ret), 0); })) do
{} while (0); } while (0)
;
523 return arg;
524 }
525
526 if (negative)
527 arg.value = -arg.value;
528 arg.value &= (~0LL) << (FIXED31_32_BITS_PER_FRACTIONAL_PART32 - frac_bits);
529 if (negative)
530 arg.value = -arg.value;
531 return arg;
532}
533
534#endif