FFmpeg
Loading...
Searching...
No Matches
rational.c
Go to the documentation of this file.
1/*
2 * rational numbers
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "libavutil/rational.c"
23#include "libavutil/integer.h"
24#include "libavutil/intfloat.h"
25
27{
28 float f = av_int2float(av_q2intfloat(q));
29 float f2 = av_q2d(q);
30 int fail;
31
32 if (isnan(f) || isnan(f2))
33 fail = !isnan(f) || !isnan(f2);
34 else if (isinf(f) || isinf(f2))
35 fail = f != f2;
36 else
37 fail = fabs(f - f2) > fabs(f)/5000000;
38 if (fail)
39 av_log(NULL, AV_LOG_ERROR, "%d/%d %f %f\n", q.num, q.den, f, f2);
40 return fail;
41}
42
43int main(void)
44{
46 int i,j,k;
47 int ret = 0;
48 static const int64_t numlist[] = {
49 INT64_MIN, INT64_MIN+1, INT64_MAX, INT32_MIN, INT32_MAX, 1,0,-1,
50 123456789, INT32_MAX-1, INT32_MAX+1LL, UINT32_MAX-1, UINT32_MAX, UINT32_MAX+1LL
51 };
52
53 for (a.num = -2; a.num <= 2; a.num++) {
54 for (a.den = -2; a.den <= 2; a.den++) {
55 for (b.num = -2; b.num <= 2; b.num++) {
56 for (b.den = -2; b.den <= 2; b.den++) {
57 int c = av_cmp_q(a,b);
58 double d = av_q2d(a) == av_q2d(b) ?
59 0 : (av_q2d(a) - av_q2d(b));
60 if (d > 0) d = 1;
61 else if (d < 0) d = -1;
62 else if (d != d) d = INT_MIN;
63 if (c != d) {
64 av_log(NULL, AV_LOG_ERROR, "%d/%d %d/%d, %d %f\n", a.num,
65 a.den, b.num, b.den, c,d);
66 ret = 1;
67 }
68 r = av_sub_q(av_add_q(b,a), b);
69 if(b.den && (r.num*a.den != a.num*r.den || !r.num != !a.num || !r.den != !a.den)) {
70 av_log(NULL, AV_LOG_ERROR, "%d/%d ", r.num, r.den);
71 ret = 1;
72 }
73 }
74 }
75 }
76 }
77
78 /* Check overflow behavior and edge cases */
79 static const AVRational unit_mul_q[][3] = {
80 {{INT_MAX, 2}, { 2, 1}, { INT_MAX, 1}},
81 {{INT_MAX, 2}, {-2, 1}, {-INT_MAX, 1}},
82 {{INT_MAX, 2}, { 0, 1}, {0, 1}},
83 {{INT_MIN, 2}, { 2, 1}, {-INT_MAX, 1}}, /* not INT_MIN */
84 {{INT_MIN, 2}, {-2, 1}, { INT_MAX, 1}},
85 {{INT_MIN, 2}, { 0, 1}, {0, 1}},
86 {{INT_MAX >> 8, 1}, {INT_MAX >> 8, 1}, {INT_MAX, 1}},
87 {{1, INT_MAX >> 8}, {1, INT_MAX >> 8}, {0, 1}},
88 {{1, 1}, {0, 0}, {0, 0}},
89 {{0, 1}, {0, 0}, {0, 0}},
90 };
91
92 for (i = 0; i < FF_ARRAY_ELEMS(unit_mul_q); i++) {
93 for (int c = 0; c < 2; c++) { /* test commutativity */
94 AVRational a = unit_mul_q[i][c ? 1 : 0];
95 AVRational b = unit_mul_q[i][c ? 0 : 1];
96 AVRational c = unit_mul_q[i][2];
98 if (r.num != c.num || r.den != c.den) {
99 av_log(NULL, AV_LOG_ERROR, "%d/%d * %d/%d = %d/%d, expected %d/%d\n",
100 a.num, a.den, b.num, b.den, r.num, r.den, c.num, c.den);
101 ret = 1;
102 }
103 }
104 }
105
106 static const AVRational unit_add_q[][3] = {
107 {{INT_MAX, 1}, { 2, 2}, { INT_MAX, 1}},
108 {{INT_MAX, 1}, {-2, 2}, { INT_MAX - 1, 1}},
109 {{INT_MAX, 1}, { 0, 2}, { INT_MAX, 1}},
110 {{INT_MIN, 1}, { 2, 2}, {-INT_MAX, 1}},
111 {{INT_MIN, 1}, {-2, 2}, {-INT_MAX, 1}},
112 {{INT_MIN, 1}, { 0, 2}, {-INT_MAX, 1}},
113 {{INT_MAX - 10, 1}, {20, 1}, { INT_MAX, 1}},
114 {{2, INT_MAX}, {2, INT_MAX}, {4, INT_MAX}},
115 {{1, 1}, {0, 0}, {0, 0}},
116 {{0, 1}, {0, 0}, {0, 0}},
117 };
118
119 for (i = 0; i < FF_ARRAY_ELEMS(unit_add_q); i++) {
120 for (int c = 0; c < 2; c++) { /* test commutativity */
121 AVRational a = unit_add_q[i][c ? 1 : 0];
122 AVRational b = unit_add_q[i][c ? 0 : 1];
123 AVRational c = unit_add_q[i][2];
124 AVRational r = av_add_q(a, b);
125 if (r.num != c.num || r.den != c.den) {
126 av_log(NULL, AV_LOG_ERROR, "%d/%d + %d/%d = %d/%d, expected %d/%d\n",
127 a.num, a.den, b.num, b.den, r.num, r.den, c.num, c.den);
128 ret = 1;
129 }
130 }
131 }
132
133 for (i = 0; i < FF_ARRAY_ELEMS(numlist); i++) {
134 int64_t a = numlist[i];
135
136 for (j = 0; j < FF_ARRAY_ELEMS(numlist); j++) {
137 int64_t b = numlist[j];
138 if (b<=0)
139 continue;
140 for (k = 0; k < FF_ARRAY_ELEMS(numlist); k++) {
141 int64_t c = numlist[k];
142 int64_t res;
143 AVInteger ai;
144
145 if (c<=0)
146 continue;
148
149 ai = av_mul_i(av_int2i(a), av_int2i(b));
150 ai = av_div_i(ai, av_int2i(c));
151
152 if (av_cmp_i(ai, av_int2i(INT64_MAX)) > 0 && res == INT64_MIN)
153 continue;
154 if (av_cmp_i(ai, av_int2i(INT64_MIN)) < 0 && res == INT64_MIN)
155 continue;
156 if (av_cmp_i(ai, av_int2i(res)) == 0)
157 continue;
158
159 // Special exception for INT64_MIN, remove this in case INT64_MIN is handled without off by 1 error
160 if (av_cmp_i(ai, av_int2i(res-1)) == 0 && a == INT64_MIN)
161 continue;
162
163 av_log(NULL, AV_LOG_ERROR, "%"PRId64" * %"PRId64" / %"PRId64" = %"PRId64" or %"PRId64"\n", a,b,c, res, av_i2int(ai));
164 ret = 1;
165 }
166 }
167 }
168
169 for (a.num = 1; a.num <= 10; a.num++) {
170 for (a.den = 1; a.den <= 10; a.den++) {
171 if (av_gcd(a.num, a.den) > 1)
172 continue;
173 for (b.num = 1; b.num <= 10; b.num++) {
174 for (b.den = 1; b.den <= 10; b.den++) {
175 int start;
176 if (av_gcd(b.num, b.den) > 1)
177 continue;
178 if (av_cmp_q(b, a) < 0)
179 continue;
180 for (start = 0; start < 10 ; start++) {
181 int acc= start;
182 int i;
183
184 for (i = 0; i<100; i++) {
185 int exact = start + av_rescale_q(i+1, b, a);
186 acc = av_add_stable(a, acc, b, 1);
187 if (FFABS(acc - exact) > 2) {
188 av_log(NULL, AV_LOG_ERROR, "%d/%d %d/%d, %d %d\n", a.num,
189 a.den, b.num, b.den, acc, exact);
190 return 1;
191 }
192 }
193 }
194 }
195 }
196 }
197 }
198
199 for (int64_t den = 1; den <= INT_MAX; den *= 3) {
200 a.den = den;
201 for (a.num = -1; a.num < (1<<27); a.num += 1 + a.num/100) {
202 if (check_q2intfloat(a))
203 return 1;
204 }
205 }
206
207 static const AVRational q2intfloat_list[] = {
208 {INT_MIN, 1}, {1, INT_MIN}, {INT_MIN, INT_MIN}, {INT_MIN, -1}, {-1, INT_MIN},
209 {INT_MAX, INT_MIN}, {INT_MIN, INT_MAX}, {-3, -7}, {3, -7},
210 {1, 0}, {-1, 0}, {INT_MIN, 0}, {0, 0}, {0, 1}, {0, INT_MIN},
211 };
212
213 for (i = 0; i < FF_ARRAY_ELEMS(q2intfloat_list); i++)
214 ret |= check_q2intfloat(q2intfloat_list[i]);
215
216 return ret;
217}
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float fabs(float a)
#define fail
Definition test.h:479
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
Definition rational.c:93
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition rational.c:80
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition rational.h:89
AVRational av_sub_q(AVRational b, AVRational c)
Subtract one rational from another.
Definition rational.c:101
uint32_t av_q2intfloat(AVRational q)
Convert an AVRational to a IEEE 32-bit float expressed in fixed-point format.
Definition rational.c:154
int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t inc)
Add a value to a timestamp.
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition mathematics.c:58
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition mathematics.c:37
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AV_ROUND_ZERO
Round toward zero.
int a
#define r
Definition input.c:42
#define b
Definition input.c:43
AVInteger av_div_i(AVInteger a, AVInteger b)
Return a/b.
Definition integer.c:143
int av_cmp_i(AVInteger a, AVInteger b)
Return 0 if a==b, 1 if a>b and -1 if a<b.
Definition integer.c:87
AVInteger av_int2i(int64_t a)
Convert the given int64_t to an AVInteger.
Definition integer.c:149
AVInteger av_mul_i(AVInteger a, AVInteger b)
Definition integer.c:66
int64_t av_i2int(AVInteger a)
Convert the given AVInteger to an int64_t.
Definition integer.c:160
arbitrary precision integers
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition intfloat.h:40
rational numbers
#define FF_ARRAY_ELEMS(a)
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
#define av_log(a,...)
int main(void)
Definition rational.c:43
static int check_q2intfloat(AVRational q)
Definition rational.c:26
static double c[64]