FFmpeg
int128.h
Go to the documentation of this file.
1 /*
2  * 128-bit integers
3  * Copyright (c) 2026 Niklas Haas
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  * 128-bit integers, falling back to integer.h if necessary
25  * @author Niklas Haas
26  */
27 
28 #ifndef AVUTIL_INT128_H
29 #define AVUTIL_INT128_H
30 
31 #include <stdint.h>
32 #include <stddef.h>
33 #include <limits.h>
34 
35 #include "config.h"
36 #include "integer.h"
37 
38 #if HAVE_INT128
39 #define AV_INT128_NATIVE 1
40 typedef unsigned __int128 av_uint128;
41 typedef __int128 av_int128;
42 #elif defined(BITINT_MAXWIDTH) && BITINT_MAXWIDTH >= 128
43 #define AV_INT128_NATIVE 1
44 typedef unsigned _BitInt(128) av_uint128;
45 typedef _BitInt(128) av_int128;
46 #elif AV_INTEGER_SIZE >= 8
47 #define AV_INT128_NATIVE 0
50 #else
51 #error "128-bit integer type not available"
52 #endif
53 
54 #if AV_INT128_NATIVE
55 # define av_add128(a, b) ((a) + (b))
56 # define av_sub128(a, b) ((a) - (b))
57 # define av_mul128(a, b) ((a) * (b))
58 # define av_div128(a, b) ((a) / (b))
59 # define av_cmp128(a, b) ((a) < (b) ? -1 : (a) > (b) ? 1 : 0)
60 # define av_min128(a, b) ((a) > (b) ? (b) : (a))
61 # define av_max128(a, b) ((a) > (b) ? (a) : (b))
62 # define av_eq128(a, b) ((a) == (b))
63 # define av_mod128(a, b) ((a) % (b))
64 # define av_shr128(a, b) ((a) >> (b))
65 # define av_to128u(a) ((av_uint128) (a))
66 # define av_to128i(a) ((av_int128) (a))
67 # define av_from128i(a) ((int64_t) (a))
68 # define av_from128u(a) ((uint64_t) (a))
69 # define av_test128(a) (!!(a))
70 #else
71 # define av_add128(a, b) av_add_i(a, b)
72 # define av_sub128(a, b) av_sub_i(a, b)
73 # define av_mul128(a, b) av_mul_i(a, b)
74 # define av_div128(a, b) av_div_i(a, b)
75 # define av_cmp128(a, b) av_cmp_i(a, b)
76 # define av_min128(a, b) (av_cmp_i(a, b) > 0 ? (b) : (a))
77 # define av_max128(a, b) (av_cmp_i(a, b) > 0 ? (a) : (b))
78 # define av_eq128(a, b) (av_cmp_i(a, b) == 0)
79 # define av_mod128(a, b) av_mod_i(NULL, a, b)
80 # define av_shr128(a, b) av_shr_i(a, b)
81 # define av_to128i(a) av_int2i(a)
82 # define av_from128i(a) av_i2int(a)
83 # define av_from128u(a) ((uint64_t) av_i2int(a))
84 # define av_test128(a) (!av_eq128(a, av_to128u(0)))
85 
87 {
88  return (AVInteger) {{ a, a >> 16, a >> 32, a >> 48 }};
89 }
90 #endif
91 
92 #endif /* AVUTIL_INT128_H */
av_to128u
static av_always_inline av_uint128 av_to128u(uint64_t a)
Definition: int128.h:86
av_int128
AVInteger av_int128
Definition: int128.h:49
av_always_inline
#define av_always_inline
Definition: attributes.h:76
limits.h
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
AVInteger
Definition: integer.h:36
av_uint128
AVInteger av_uint128
Definition: int128.h:48
integer.h