Coverage Report

Created: 2025-08-28 07:12

/src/ffmpeg/libavutil/integer.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * arbitrary precision integers
3
 * Copyright (c) 2004 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
/**
23
 * @file
24
 * arbitrary precision integers
25
 * @author Michael Niedermayer <michaelni@gmx.at>
26
 */
27
28
#include <string.h>
29
30
#include "integer.h"
31
#include "avassert.h"
32
#include "intmath.h"
33
34
static const AVInteger zero_i;
35
36
67
AVInteger av_add_i(AVInteger a, AVInteger b){
37
67
    int i, carry=0;
38
39
603
    for(i=0; i<AV_INTEGER_SIZE; i++){
40
536
        carry= (carry>>16) + a.v[i] + b.v[i];
41
536
        a.v[i]= carry;
42
536
    }
43
67
    return a;
44
67
}
45
46
826
AVInteger av_sub_i(AVInteger a, AVInteger b){
47
826
    int i, carry=0;
48
49
7.43k
    for(i=0; i<AV_INTEGER_SIZE; i++){
50
6.60k
        carry= (carry>>16) + a.v[i] - b.v[i];
51
6.60k
        a.v[i]= carry;
52
6.60k
    }
53
826
    return a;
54
826
}
55
56
414
int av_log2_i(AVInteger a){
57
414
    int i;
58
59
2.82k
    for(i=AV_INTEGER_SIZE-1; i>=0; i--){
60
2.82k
        if(a.v[i])
61
414
            return av_log2_16bit(a.v[i]) + 16*i;
62
2.82k
    }
63
0
    return -1;
64
414
}
65
66
134
AVInteger av_mul_i(AVInteger a, AVInteger b){
67
134
    AVInteger out;
68
134
    int i, j;
69
134
    int na= (av_log2_i(a)+16) >> 4;
70
134
    int nb= (av_log2_i(b)+16) >> 4;
71
72
134
    memset(&out, 0, sizeof(out));
73
74
436
    for(i=0; i<na; i++){
75
302
        unsigned int carry=0;
76
77
302
        if(a.v[i])
78
1.02k
            for(j=i; j<AV_INTEGER_SIZE && j-i<=nb; j++){
79
723
                carry= (carry>>16) + out.v[j] + a.v[i]*(unsigned)b.v[j-i];
80
723
                out.v[j]= carry;
81
723
            }
82
302
    }
83
84
134
    return out;
85
134
}
86
87
1.60k
int av_cmp_i(AVInteger a, AVInteger b){
88
1.60k
    int i;
89
1.60k
    int v= (int16_t)a.v[AV_INTEGER_SIZE-1] - (int16_t)b.v[AV_INTEGER_SIZE-1];
90
1.60k
    if(v) return (v>>16)|1;
91
92
8.65k
    for(i=AV_INTEGER_SIZE-2; i>=0; i--){
93
8.65k
        int v= a.v[i] - b.v[i];
94
8.65k
        if(v) return (v>>16)|1;
95
8.65k
    }
96
3
    return 0;
97
1.59k
}
98
99
3.20k
AVInteger av_shr_i(AVInteger a, int s){
100
3.20k
    AVInteger out;
101
3.20k
    int i;
102
103
28.8k
    for(i=0; i<AV_INTEGER_SIZE; i++){
104
25.6k
        unsigned int index= i + (s>>4);
105
25.6k
        unsigned int v=0;
106
25.6k
        if (index + 1 < AV_INTEGER_SIZE) v  = a.v[index + 1] * (1U << 16);
107
25.6k
        if (index     < AV_INTEGER_SIZE) v |= a.v[index];
108
25.6k
        out.v[i]= v >> (s&15);
109
25.6k
    }
110
3.20k
    return out;
111
3.20k
}
112
113
73
AVInteger av_mod_i(AVInteger *quot, AVInteger a, AVInteger b){
114
73
    int i= av_log2_i(a) - av_log2_i(b);
115
73
    AVInteger quot_temp;
116
73
    if(!quot) quot = &quot_temp;
117
118
73
    if ((int16_t)a.v[AV_INTEGER_SIZE-1] < 0) {
119
6
        a = av_mod_i(quot, av_sub_i(zero_i, a), b);
120
6
        *quot = av_sub_i(zero_i, *quot);
121
6
        return av_sub_i(zero_i, a);
122
6
    }
123
124
67
    av_assert2((int16_t)a.v[AV_INTEGER_SIZE-1] >= 0 && (int16_t)b.v[AV_INTEGER_SIZE-1] >= 0);
125
67
    av_assert2(av_log2_i(b)>=0);
126
127
67
    if(i > 0)
128
62
        b= av_shr_i(b, -i);
129
130
67
    memset(quot, 0, sizeof(AVInteger));
131
132
1.60k
    while(i-- >= 0){
133
1.53k
        *quot= av_shr_i(*quot, -1);
134
1.53k
        if(av_cmp_i(a, b) >= 0){
135
808
            a= av_sub_i(a, b);
136
808
            quot->v[0] += 1;
137
808
        }
138
1.53k
        b= av_shr_i(b, 1);
139
1.53k
    }
140
67
    return a;
141
73
}
142
143
67
AVInteger av_div_i(AVInteger a, AVInteger b){
144
67
    AVInteger quot;
145
67
    av_mod_i(&quot, a, b);
146
67
    return quot;
147
67
}
148
149
335
AVInteger av_int2i(int64_t a){
150
335
    AVInteger out;
151
335
    int i;
152
153
3.01k
    for(i=0; i<AV_INTEGER_SIZE; i++){
154
2.68k
        out.v[i]= a;
155
2.68k
        a>>=16;
156
2.68k
    }
157
335
    return out;
158
335
}
159
160
67
int64_t av_i2int(AVInteger a){
161
67
    uint64_t out = a.v[3];
162
163
268
    for (int i = 2; i >= 0; i--)
164
201
        out = (out << 16) | a.v[i];
165
67
    return out;
166
67
}