Generated C leaves out the bounds check of an index such as row * stride + column when a guard bounds it: an earlier if that returns, breaks or continues, an enclosing if or while condition, or the earlier operands of && and ||, until a value they compare changes. Indexes and guard comparisons become polynomials over parameters, invariant locals and slice counts; each loop counter is replaced by its upper bound, and the index is proven below the count or length when every step is exact. Guards that miss the read, values moved after the guard and else-if exits keep their checks. On the AV1 benchmark (fixed binaries, interleaved, one core) 1080p q20 drops from 185.1 to 182.2 ms and 720p 4:4:4 is unchanged, but 1080p 10-bit q30 rises from 201.5 to 212 ms. All of that comes from the one check left out of FindCdefDirection's sample read; the function alone runs faster without it (102 against 105 ns per call), so the loss is a layout effect in the decoder's context.
460 lines
17 KiB
C
460 lines
17 KiB
C
/* Generated by zi2c from compiler_enum.zi. */
|
|
#include "zir_bounds.h"
|
|
#include "zir_string.h"
|
|
#define ZIRAN_COMPILER_ENUM_H_PRIVATE 1
|
|
#include "compiler_enum.h"
|
|
#undef ZIRAN_COMPILER_ENUM_H_PRIVATE
|
|
#ifndef ZIRAN_HELPER_ZiDivS64
|
|
#define ZIRAN_HELPER_ZiDivS64
|
|
static inline int64_t ZiDivS64(int64_t a, int64_t b, int64_t min) {
|
|
if(!b) abort();
|
|
if(b == -1) return a == min ? a : -a;
|
|
return a / b;
|
|
}
|
|
#endif
|
|
|
|
#ifndef ZIRAN_HELPER_ZiRemS64
|
|
#define ZIRAN_HELPER_ZiRemS64
|
|
static inline int64_t ZiRemS64(int64_t a, int64_t b) {
|
|
if(!b) abort();
|
|
return b == -1 ? 0 : a % b;
|
|
}
|
|
#endif
|
|
|
|
#ifndef ZIRAN_HELPER_ZiDivU64
|
|
#define ZIRAN_HELPER_ZiDivU64
|
|
static inline uint64_t ZiDivU64(uint64_t a, uint64_t b) {
|
|
if(!b) abort();
|
|
return a / b;
|
|
}
|
|
#endif
|
|
|
|
#ifndef ZIRAN_HELPER_ZiShl64
|
|
#define ZIRAN_HELPER_ZiShl64
|
|
static inline uint64_t ZiShl64(uint64_t a, uint64_t s, int w) {
|
|
if(s >= (uint64_t)w) abort();
|
|
return (uint64_t)(a << s);
|
|
}
|
|
#endif
|
|
|
|
#ifndef ZIRAN_HELPER_ZiShrS64
|
|
#define ZIRAN_HELPER_ZiShrS64
|
|
static inline int64_t ZiShrS64(int64_t a, uint64_t s, int w) {
|
|
if(s >= (uint64_t)w) abort();
|
|
return a < 0 ? ~(~a >> s) : a >> s;
|
|
}
|
|
#endif
|
|
static void compiler_enum_Space(Cursor* cursor);
|
|
static bool compiler_enum_IdentifierStart(uint8_t byte);
|
|
static int64_t compiler_enum_Digit(uint8_t byte);
|
|
static int64_t compiler_enum_Number(Cursor* cursor, bool negative);
|
|
static int64_t compiler_enum_Term(Cursor* cursor, Slice entries);
|
|
static int64_t compiler_enum_Product(Cursor* cursor, Slice entries);
|
|
static int64_t compiler_enum_Sum(Cursor* cursor, Slice entries);
|
|
static int64_t compiler_enum_Expression(Cursor* cursor, Slice entries);
|
|
|
|
static void
|
|
compiler_enum_Space(Cursor* cursor)
|
|
{
|
|
int64_t value_0 = compiler_text_SkipSpace(cursor->source, cursor->at, false);
|
|
cursor->at = value_0;
|
|
}
|
|
|
|
static bool
|
|
compiler_enum_IdentifierStart(uint8_t byte)
|
|
{
|
|
return byte == 95 || (byte >= 97 && byte <= 122) || (byte >= 65 && byte <= 90);
|
|
}
|
|
|
|
static int64_t
|
|
compiler_enum_Digit(uint8_t byte)
|
|
{
|
|
if (byte >= 48 && byte <= 57) {
|
|
return (int64_t)(((uint8_t)((uint32_t)byte - 48u)));
|
|
}
|
|
if (byte >= 97 && byte <= 102) {
|
|
return (int64_t)((uint64_t)(((uint8_t)((uint32_t)byte - 97u))) + UINT64_C(10));
|
|
}
|
|
if (byte >= 65 && byte <= 70) {
|
|
return (int64_t)((uint64_t)(((uint8_t)((uint32_t)byte - 65u))) + UINT64_C(10));
|
|
}
|
|
return -1LL;
|
|
}
|
|
|
|
static int64_t
|
|
compiler_enum_Number(Cursor* cursor, bool negative)
|
|
{
|
|
uint64_t base = 10ULL;
|
|
if ((uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at) == 48) {
|
|
base = 8ULL;
|
|
int64_t value_0 = (int64_t)((uint64_t)(cursor->at) + UINT64_C(1));
|
|
int64_t value_1 = (int64_t)(cursor->source).length;
|
|
bool value_2 = value_0 < value_1;
|
|
if (value_2) {
|
|
bool value_3 = (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, ((int64_t)((uint64_t)(cursor->at) + UINT64_C(1)))) == 120;
|
|
bool value_4 = value_3 || (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, ((int64_t)((uint64_t)(cursor->at) + UINT64_C(1)))) == 88;
|
|
value_2 = value_4;
|
|
}
|
|
if (value_2) {
|
|
base = 16ULL;
|
|
cursor->at = (int64_t)((uint64_t)(cursor->at) + UINT64_C(2));
|
|
}
|
|
}
|
|
int64_t begin = cursor->at;
|
|
uint64_t limit = 9223372036854775807ULL;
|
|
if (negative) {
|
|
limit = (uint64_t)((uint64_t)limit + UINT64_C(1));
|
|
}
|
|
uint64_t magnitude = 0ULL;
|
|
while (true) {
|
|
int64_t value_5 = cursor->at;
|
|
int64_t value_6 = (int64_t)(cursor->source).length;
|
|
if (!(value_5 < value_6)) { break; }
|
|
int64_t digit = compiler_enum_Digit((uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at));
|
|
if (digit < 0LL || (uint64_t)(digit) >= base) {
|
|
break;
|
|
}
|
|
uint64_t value_7 = ZiDivU64(((uint64_t)((uint64_t)limit - (uint64_t)(digit))), base);
|
|
if (magnitude > value_7) {
|
|
cursor->valid = false;
|
|
return 0LL;
|
|
}
|
|
magnitude = (uint64_t)(((uint64_t)magnitude * (uint64_t)base) + (uint64_t)(digit));
|
|
cursor->at = (int64_t)((uint64_t)(cursor->at) + UINT64_C(1));
|
|
}
|
|
int64_t value_8 = cursor->at;
|
|
if (value_8 == begin) {
|
|
cursor->valid = false;
|
|
return 0LL;
|
|
}
|
|
if (negative) {
|
|
return (int64_t)(((uint64_t)(UINT64_C(0) - (uint64_t)magnitude)));
|
|
}
|
|
return (int64_t)(magnitude);
|
|
}
|
|
|
|
static int64_t
|
|
compiler_enum_Term(Cursor* cursor, Slice entries)
|
|
{
|
|
compiler_enum_Space(cursor);
|
|
bool negative = false;
|
|
int64_t value_0 = cursor->at;
|
|
int64_t value_1 = (int64_t)(cursor->source).length;
|
|
if (value_0 < value_1 && ((uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at) == 43 || (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at) == 45)) {
|
|
negative = (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at) == 45;
|
|
cursor->at = (int64_t)((uint64_t)(cursor->at) + UINT64_C(1));
|
|
}
|
|
compiler_enum_Space(cursor);
|
|
int64_t value_2 = cursor->at;
|
|
int64_t value_3 = (int64_t)(cursor->source).length;
|
|
if (value_2 == value_3) {
|
|
cursor->valid = false;
|
|
return 0LL;
|
|
}
|
|
uint8_t first = (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at);
|
|
if (first >= 48 && first <= 57) {
|
|
return compiler_enum_Number(cursor, negative);
|
|
}
|
|
bool value_4 = compiler_enum_IdentifierStart(first);
|
|
if (!value_4) {
|
|
cursor->valid = false;
|
|
return 0LL;
|
|
}
|
|
int64_t begin = cursor->at;
|
|
while (true) {
|
|
int64_t value_5 = cursor->at;
|
|
int64_t value_6 = (int64_t)(cursor->source).length;
|
|
bool value_7 = value_5 < value_6;
|
|
if (!(value_7 && compiler_text_IdentifierByte((uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at)))) { break; }
|
|
cursor->at = (int64_t)((uint64_t)(cursor->at) + UINT64_C(1));
|
|
}
|
|
String value_8 = cursor->source;
|
|
String name = StringRange(value_8, (int64_t)begin, (int64_t)cursor->at);
|
|
for (int64_t it_index = 0LL; it_index < (int64_t)(entries).length; it_index++) {
|
|
Entry entry = ((Entry *)entries.data)[it_index];
|
|
if (StringEqual(entry.name, name)) {
|
|
if (!negative) {
|
|
return entry.value;
|
|
}
|
|
if (entry.value == (-INT64_C(9223372036854775807)-1)) {
|
|
cursor->valid = false;
|
|
return 0LL;
|
|
}
|
|
return (int64_t)(UINT64_C(0) - (uint64_t)(entry.value));
|
|
}
|
|
}
|
|
cursor->valid = false;
|
|
return 0LL;
|
|
}
|
|
|
|
static int64_t
|
|
compiler_enum_Product(Cursor* cursor, Slice entries)
|
|
{
|
|
int64_t value = compiler_enum_Term(cursor, entries);
|
|
while (cursor->valid) {
|
|
compiler_enum_Space(cursor);
|
|
int64_t value_0 = cursor->at;
|
|
int64_t value_1 = (int64_t)(cursor->source).length;
|
|
if (value_0 == value_1) {
|
|
return value;
|
|
}
|
|
uint8_t op = (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at);
|
|
if (op != 42 && op != 47 && op != 37) {
|
|
return value;
|
|
}
|
|
cursor->at = (int64_t)((uint64_t)(cursor->at) + UINT64_C(1));
|
|
int64_t right = compiler_enum_Term(cursor, entries);
|
|
if (!cursor->valid) {
|
|
return 0LL;
|
|
}
|
|
if (op == 42) {
|
|
bool value_2 = value > 0LL && right > 0LL;
|
|
if (value_2) {
|
|
int64_t value_3 = ZiDivS64(9223372036854775807LL, right, INT64_MIN);
|
|
value_2 = (value > value_3);
|
|
}
|
|
bool value_4 = value_2;
|
|
if (!value_4) {
|
|
bool value_5 = value > 0LL && right < 0LL;
|
|
if (value_5) {
|
|
int64_t value_6 = ZiDivS64((-INT64_C(9223372036854775807)-1), value, INT64_MIN);
|
|
value_5 = (right < value_6);
|
|
}
|
|
value_4 = value_5;
|
|
}
|
|
bool value_7 = value_4;
|
|
if (!value_7) {
|
|
bool value_8 = value < 0LL && right > 0LL;
|
|
if (value_8) {
|
|
int64_t value_9 = ZiDivS64((-INT64_C(9223372036854775807)-1), right, INT64_MIN);
|
|
value_8 = (value < value_9);
|
|
}
|
|
value_7 = value_8;
|
|
}
|
|
bool value_10 = value_7;
|
|
if (!value_10) {
|
|
bool value_11 = value < 0LL && right < 0LL;
|
|
if (value_11) {
|
|
int64_t value_12 = ZiDivS64(9223372036854775807LL, value, INT64_MIN);
|
|
value_11 = (right < value_12);
|
|
}
|
|
value_10 = value_11;
|
|
}
|
|
if (value_10) {
|
|
cursor->valid = false;
|
|
return 0LL;
|
|
}
|
|
value = (int64_t)((uint64_t)value * (uint64_t)right);
|
|
} else {
|
|
if (right == 0LL || (value == (-INT64_C(9223372036854775807)-1) && right == -1LL)) {
|
|
cursor->valid = false;
|
|
return 0LL;
|
|
}
|
|
if (op == 47) {
|
|
value = ZiDivS64(value, right, INT64_MIN);
|
|
} else {
|
|
value = ZiRemS64(value, right);
|
|
}
|
|
}
|
|
}
|
|
return 0LL;
|
|
}
|
|
|
|
static int64_t
|
|
compiler_enum_Sum(Cursor* cursor, Slice entries)
|
|
{
|
|
int64_t value = compiler_enum_Product(cursor, entries);
|
|
while (cursor->valid) {
|
|
compiler_enum_Space(cursor);
|
|
int64_t value_0 = cursor->at;
|
|
int64_t value_1 = (int64_t)(cursor->source).length;
|
|
if (value_0 == value_1) {
|
|
return value;
|
|
}
|
|
uint8_t op = (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at);
|
|
if (op != 43 && op != 45) {
|
|
return value;
|
|
}
|
|
cursor->at = (int64_t)((uint64_t)(cursor->at) + UINT64_C(1));
|
|
int64_t right = compiler_enum_Product(cursor, entries);
|
|
if (!cursor->valid) {
|
|
return 0LL;
|
|
}
|
|
bool value_2 = op == 43 && ((right > 0LL && value > (int64_t)(UINT64_C(9223372036854775807) - (uint64_t)right)) || (right < 0LL && value < (int64_t)((uint64_t)((-INT64_C(9223372036854775807)-1)) - (uint64_t)right)));
|
|
if (value_2 || (op == 45 && ((right < 0LL && value > (int64_t)(UINT64_C(9223372036854775807) + (uint64_t)right)) || (right > 0LL && value < (int64_t)((uint64_t)((-INT64_C(9223372036854775807)-1)) + (uint64_t)right))))) {
|
|
cursor->valid = false;
|
|
return 0LL;
|
|
}
|
|
if (op == 43) {
|
|
value = (int64_t)((uint64_t)value + (uint64_t)right);
|
|
} else {
|
|
value = (int64_t)((uint64_t)value - (uint64_t)right);
|
|
}
|
|
}
|
|
return 0LL;
|
|
}
|
|
|
|
static int64_t
|
|
compiler_enum_Expression(Cursor* cursor, Slice entries)
|
|
{
|
|
int64_t value = compiler_enum_Sum(cursor, entries);
|
|
while (cursor->valid) {
|
|
compiler_enum_Space(cursor);
|
|
int64_t value_0 = (int64_t)((uint64_t)(cursor->at) + UINT64_C(1));
|
|
int64_t value_1 = (int64_t)(cursor->source).length;
|
|
bool value_2 = value_0 >= value_1;
|
|
if (!value_2) {
|
|
String value_3 = cursor->source;
|
|
bool value_4 = !StringEqual(StringRange(value_3, (int64_t)cursor->at, (int64_t)((int64_t)((uint64_t)(cursor->at) + UINT64_C(2)))), StringLiteral("<<"));
|
|
value_2 = value_4;
|
|
}
|
|
if (value_2) {
|
|
break;
|
|
}
|
|
cursor->at = (int64_t)((uint64_t)(cursor->at) + UINT64_C(2));
|
|
int64_t amount = compiler_enum_Sum(cursor, entries);
|
|
bool value_5 = !cursor->valid || amount < 0LL || amount >= 63LL || value < 0LL;
|
|
if (!value_5) {
|
|
int64_t value_6 = ZiShrS64(9223372036854775807LL, (uint64_t)(amount), 64);
|
|
value_5 = (value > value_6);
|
|
}
|
|
if (value_5) {
|
|
cursor->valid = false;
|
|
return 0LL;
|
|
}
|
|
value = (int64_t)ZiShl64(value, (uint64_t)(amount), 64);
|
|
}
|
|
compiler_enum_Space(cursor);
|
|
int64_t value_7 = cursor->at;
|
|
int64_t value_8 = (int64_t)(cursor->source).length;
|
|
if (value_7 != value_8) {
|
|
cursor->valid = false;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
bool
|
|
compiler_enum_FitsBacking(String backing, int64_t value)
|
|
{
|
|
if (StringEqual(backing, StringLiteral("s64"))) {
|
|
return true;
|
|
}
|
|
if (StringEqual(backing, StringLiteral("u64"))) {
|
|
return value >= 0LL;
|
|
}
|
|
if (StringEqual(backing, StringLiteral("s8"))) {
|
|
return value >= -128LL && value <= 127LL;
|
|
}
|
|
if (StringEqual(backing, StringLiteral("s16"))) {
|
|
return value >= -32768LL && value <= 32767LL;
|
|
}
|
|
if (StringEqual(backing, StringLiteral("s32"))) {
|
|
return value >= -2147483648LL && value <= 2147483647LL;
|
|
}
|
|
if (StringEqual(backing, StringLiteral("u8"))) {
|
|
return value >= 0LL && value <= 255LL;
|
|
}
|
|
if (StringEqual(backing, StringLiteral("u16"))) {
|
|
return value >= 0LL && value <= 65535LL;
|
|
}
|
|
if (StringEqual(backing, StringLiteral("u32"))) {
|
|
return value >= 0LL && value <= 4294967295LL;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
Evaluation
|
|
compiler_enum_MemberValue(String body, String backing, String wanted, Slice entries, int64_t name_limit)
|
|
{
|
|
Evaluation result = {0};
|
|
int64_t at = 0LL;
|
|
int64_t next = 0LL;
|
|
bool next_valid = true;
|
|
while (at < (int64_t)(body).length) {
|
|
while (true) {
|
|
bool value_0 = at < (int64_t)(body).length;
|
|
bool value_1 = value_0;
|
|
if (value_1) {
|
|
bool value_2 = (uint8_t)ZIRAN_INDEX(body.data, body.length, at) == 44;
|
|
bool value_3 = value_2 || compiler_text_SpaceByte((uint8_t)ZIRAN_INDEX(body.data, body.length, at));
|
|
value_1 = value_3;
|
|
}
|
|
if (!value_1) { break; }
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
if (at == (int64_t)(body).length) {
|
|
break;
|
|
}
|
|
int64_t end = at;
|
|
while (end < (int64_t)(body).length && (uint8_t)ZIRAN_INDEX(body.data, body.length, end) != 44 && (uint8_t)ZIRAN_INDEX(body.data, body.length, end) != 10) {
|
|
end = (int64_t)((uint64_t)end + UINT64_C(1));
|
|
}
|
|
int64_t begin = at;
|
|
bool value_4 = compiler_enum_IdentifierStart((uint8_t)ZIRAN_INDEX(body.data, body.length, at));
|
|
if (!value_4) {
|
|
return (Evaluation){0};
|
|
}
|
|
while (true) {
|
|
bool value_5 = at < end;
|
|
if (!(value_5 && compiler_text_IdentifierByte((uint8_t)ZIRAN_INDEX(body.data, body.length, at)))) { break; }
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
String name = StringRange(body, (int64_t)begin, (int64_t)at);
|
|
if ((int64_t)(name).length >= name_limit || result.count >= (int64_t)(entries).length) {
|
|
return (Evaluation){0};
|
|
}
|
|
{
|
|
Slice value_6 = entries;
|
|
Slice value_7 = SliceRange(value_6, (int64_t)0LL, (int64_t)result.count, sizeof(Entry));
|
|
Slice loop_view_26 = SliceRange(value_7, (int64_t)0, (int64_t)value_7.length, sizeof(Entry));
|
|
int64_t loop_count_26 = (int64_t)(loop_view_26).length;
|
|
int64_t loop_cursor_26 = 0LL;
|
|
while (loop_cursor_26 < loop_count_26) {
|
|
int64_t loop_index_26 = loop_cursor_26;
|
|
Entry entry = ((Entry *)loop_view_26.data)[loop_index_26];
|
|
if (StringEqual(entry.name, name)) {
|
|
return (Evaluation){0};
|
|
}
|
|
loop_cursor_26 = (int64_t)((uint64_t)loop_cursor_26 + UINT64_C(1));
|
|
}
|
|
}
|
|
Cursor cursor = {.source = StringRange(body, (int64_t)at, (int64_t)end), .at = 0LL, .valid = true};
|
|
compiler_enum_Space(&(cursor));
|
|
int64_t value = next;
|
|
if (cursor.at < (int64_t)(cursor.source).length) {
|
|
if ((uint8_t)ZIRAN_INDEX(cursor.source.data, cursor.source.length, cursor.at) != 61) {
|
|
return (Evaluation){0};
|
|
}
|
|
cursor.at = (int64_t)((uint64_t)(cursor.at) + UINT64_C(1));
|
|
Slice value_8 = entries;
|
|
value = compiler_enum_Expression(&(cursor), SliceRange(value_8, (int64_t)0LL, (int64_t)result.count, sizeof(Entry)));
|
|
if (!cursor.valid) {
|
|
return (Evaluation){0};
|
|
}
|
|
} else if (!next_valid) {
|
|
return (Evaluation){0};
|
|
}
|
|
bool value_9 = compiler_enum_FitsBacking(backing, value);
|
|
if (!value_9) {
|
|
return (Evaluation){0};
|
|
}
|
|
((Entry *)entries.data)[SliceIndex(entries, (int64_t)result.count)] = (Entry){.name = name, .value = value};
|
|
if (StringEqual(name, wanted)) {
|
|
result.found = true;
|
|
result.value = value;
|
|
}
|
|
result.count = (int64_t)((uint64_t)(result.count) + UINT64_C(1));
|
|
next_valid = value != 9223372036854775807LL;
|
|
next = value;
|
|
if (next_valid) {
|
|
next = (int64_t)((uint64_t)next + UINT64_C(1));
|
|
}
|
|
at = end;
|
|
if (at < (int64_t)(body).length) {
|
|
at = (int64_t)((uint64_t)at + UINT64_C(1));
|
|
}
|
|
}
|
|
result.valid = result.count > 0LL;
|
|
return result;
|
|
}
|