blob: 10b70408e09f72e6d4935b218f1c8728c133ff3e [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar0ea50ce2010-05-18 11:58:33 -040010 */
Yaowu Xuf883b422016-08-30 14:01:10 -070011#ifndef AOM_AOM_INTEGER_H_
12#define AOM_AOM_INTEGER_H_
John Koleszar0ea50ce2010-05-18 11:58:33 -040013
14/* get ptrdiff_t, size_t, wchar_t, NULL */
15#include <stddef.h>
16
Jim Bankoski07a67522014-08-12 16:28:08 -070017#if defined(_MSC_VER)
Yaowu Xuf883b422016-08-30 14:01:10 -070018#define AOM_FORCE_INLINE __forceinline
19#define AOM_INLINE __inline
Jim Bankoski07a67522014-08-12 16:28:08 -070020#else
James Zern7dec5152016-10-17 12:39:34 -070021#define AOM_FORCE_INLINE __inline__ __attribute__((always_inline))
Jim Bankoski07a67522014-08-12 16:28:08 -070022// TODO(jbb): Allow a way to force inline off for older compilers.
Yaowu Xuf883b422016-08-30 14:01:10 -070023#define AOM_INLINE inline
Jim Bankoski07a67522014-08-12 16:28:08 -070024#endif
25
Yaowu Xuf883b422016-08-30 14:01:10 -070026#if defined(AOM_EMULATE_INTTYPES)
clang-format83a52072016-08-08 20:22:13 -070027typedef signed char int8_t;
John Koleszar0ea50ce2010-05-18 11:58:33 -040028typedef signed short int16_t;
clang-format83a52072016-08-08 20:22:13 -070029typedef signed int int32_t;
John Koleszar0ea50ce2010-05-18 11:58:33 -040030
clang-format83a52072016-08-08 20:22:13 -070031typedef unsigned char uint8_t;
John Koleszar0ea50ce2010-05-18 11:58:33 -040032typedef unsigned short uint16_t;
clang-format83a52072016-08-08 20:22:13 -070033typedef unsigned int uint32_t;
John Koleszar0ea50ce2010-05-18 11:58:33 -040034
John Koleszar0ea50ce2010-05-18 11:58:33 -040035#ifndef _UINTPTR_T_DEFINED
John Koleszar83b1d902012-11-05 12:37:14 -080036typedef size_t uintptr_t;
John Koleszar0ea50ce2010-05-18 11:58:33 -040037#endif
38
John Koleszar1df03142010-05-20 14:44:18 -040039#else
40
41/* Most platforms have the C99 standard integer types. */
42
James Zernc87f32c2014-09-30 16:06:23 -070043#if defined(__cplusplus)
clang-format83a52072016-08-08 20:22:13 -070044#if !defined(__STDC_FORMAT_MACROS)
45#define __STDC_FORMAT_MACROS
46#endif
47#if !defined(__STDC_LIMIT_MACROS)
48#define __STDC_LIMIT_MACROS
49#endif
James Zernc87f32c2014-09-30 16:06:23 -070050#endif // __cplusplus
51
John Koleszar1df03142010-05-20 14:44:18 -040052#include <stdint.h>
John Koleszar1df03142010-05-20 14:44:18 -040053
John Koleszar0ea50ce2010-05-18 11:58:33 -040054#endif
55
John Koleszar4ead98f2011-05-05 10:53:24 -040056/* VS2010 defines stdint.h, but not inttypes.h */
Tom Finegan7320fdd2014-02-10 15:30:43 -080057#if defined(_MSC_VER) && _MSC_VER < 1800
John Koleszar4ead98f2011-05-05 10:53:24 -040058#define PRId64 "I64d"
59#else
60#include <inttypes.h>
61#endif
62
Yaowu Xu91058972017-11-24 16:26:18 -080063#if !defined(INT8_MAX)
64#define INT8_MAX 127
65#endif
66
67#if !defined(INT32_MAX)
Yaowu Xud74a21e2017-11-24 22:34:43 -080068#define INT32_MAX 2147483647
Yaowu Xu91058972017-11-24 16:26:18 -080069#endif
70
71#if !defined(INT32_MIN)
Yaowu Xue65e12f2017-11-27 07:56:37 -080072#define INT32_MIN (-2147483647 - 1)
Yaowu Xu91058972017-11-24 16:26:18 -080073#endif
74
Sebastien Alaiwaned9e0d02017-07-13 10:44:58 +020075#define NELEMENTS(x) (int)(sizeof(x) / sizeof(x[0]))
76
Tom Finegan10fea492018-01-08 18:07:46 -080077#if defined(__cplusplus)
78extern "C" {
79#endif // __cplusplus
80
81// Returns size of uint32_t when encoded using LEB128.
Tom Finegan9540d592018-02-06 10:04:31 -080082size_t aom_uleb_size_in_bytes(uint64_t value);
Tom Finegan10fea492018-01-08 18:07:46 -080083
Tom Fineganf4129062018-02-08 08:32:42 -080084// Returns -1 upon decode failure, or 0 and decoded LEB128 value via last
85// argument when decode is successful.
86int aom_uleb_decode(const uint8_t *buffer, size_t available, uint64_t *value);
Tom Finegan10fea492018-01-08 18:07:46 -080087
88// Encodes LEB128 integer. Returns 0 when successful, and -1 upon failure.
Tom Finegan9540d592018-02-06 10:04:31 -080089int aom_uleb_encode(uint64_t value, size_t available, uint8_t *coded_value,
Tom Finegan10fea492018-01-08 18:07:46 -080090 size_t *coded_size);
91
92// Encodes LEB128 integer to size specified. Returns 0 when successful, and -1
93// upon failure.
Tom Finegan9540d592018-02-06 10:04:31 -080094int aom_uleb_encode_fixed_size(uint64_t value, size_t available,
Tom Finegan10fea492018-01-08 18:07:46 -080095 size_t pad_to_size, uint8_t *coded_value,
96 size_t *coded_size);
97
98#if defined(__cplusplus)
99} // extern "C"
100#endif // __cplusplus
101
Yaowu Xuf883b422016-08-30 14:01:10 -0700102#endif // AOM_AOM_INTEGER_H_