*/
typedef uint32_t sfloat_t;
-typedef union {
+union sfloat_cast_t {
qcfloat_t f;
sfloat_t s;
-} sfloat_cast_t;
+};
/* Exception flags */
-typedef enum {
+enum sfloat_exceptionflags_t {
SFLOAT_NOEXCEPT = 0,
SFLOAT_INVALID = 1,
SFLOAT_DIVBYZERO = 4,
SFLOAT_OVERFLOW = 8,
SFLOAT_UNDERFLOW = 16,
SFLOAT_INEXACT = 32
-} sfloat_exceptionflags_t;
+};
/* Rounding modes */
-typedef enum {
+enum sfloat_roundingmode_t {
SFLOAT_ROUND_NEAREST_EVEN,
SFLOAT_ROUND_DOWN,
SFLOAT_ROUND_UP,
SFLOAT_ROUND_TO_ZERO
-} sfloat_roundingmode_t;
+};
/* Underflow tininess-detection mode */
-typedef enum {
+enum sfloat_tdetect_t {
SFLOAT_TAFTER,
SFLOAT_TBEFORE
-} sfloat_tdetect_t;
+};
-typedef struct {
- sfloat_roundingmode_t roundingmode;
+struct sfloat_state_t {
+ sfloat_roundingmode_t roundingmode;
sfloat_exceptionflags_t exceptionflags;
- sfloat_tdetect_t tiny;
-} sfloat_state_t;
+ sfloat_tdetect_t tiny;
+};
/* Counts the number of leading zero bits before the most-significand one bit. */
#ifdef _MSC_VER
*
* TODO: gcc/clang hinting for autovectorization
*/
-typedef enum {
+enum vec3_comp_t {
VEC_COMP_X = 1 << 0,
VEC_COMP_Y = 1 << 1,
VEC_COMP_Z = 1 << 2
-} vec3_comp_t;
+};
-typedef struct {
+struct vec3_soft_t {
sfloat_cast_t x;
sfloat_cast_t y;
sfloat_cast_t z;
-} vec3_soft_t;
+};
-typedef struct {
- vec3_comp_t faults;
+struct vec3_soft_state_t {
+ vec3_comp_t faults;
sfloat_state_t state[3];
-} vec3_soft_state_t;
+};
static GMQCC_INLINE vec3_soft_t vec3_soft_convert(vec3_t vec) {
vec3_soft_t soft;
sfloat_neg(&s[1], v[1].s);
sfloat_neg(&s[2], v[2].s);
- sfloat_check(ctx, &s[0], NULL);
- sfloat_check(ctx, &s[1], NULL);
- sfloat_check(ctx, &s[2], NULL);
+ sfloat_check(ctx, &s[0], nullptr);
+ sfloat_check(ctx, &s[1], nullptr);
+ sfloat_check(ctx, &s[2], nullptr);
end:
out.x = -a.x;
r[3] = sfloat_add(&s[3], r[0], r[1]);
r[4] = sfloat_add(&s[4], r[3], r[2]);
- sfloat_check(ctx, &s[0], NULL);
- sfloat_check(ctx, &s[1], NULL);
- sfloat_check(ctx, &s[2], NULL);
- sfloat_check(ctx, &s[3], NULL);
- sfloat_check(ctx, &s[4], NULL);
+ sfloat_check(ctx, &s[0], nullptr);
+ sfloat_check(ctx, &s[1], nullptr);
+ sfloat_check(ctx, &s[2], nullptr);
+ sfloat_check(ctx, &s[3], nullptr);
+ sfloat_check(ctx, &s[4], nullptr);
end:
return (a.x * b.x + a.y * b.y + a.z * b.z);
r[7] = sfloat_sub(&s[7], r[2], r[3]);
r[8] = sfloat_sub(&s[8], r[4], r[5]);
- sfloat_check(ctx, &s[0], NULL);
- sfloat_check(ctx, &s[1], NULL);
- sfloat_check(ctx, &s[2], NULL);
- sfloat_check(ctx, &s[3], NULL);
- sfloat_check(ctx, &s[4], NULL);
- sfloat_check(ctx, &s[5], NULL);
+ sfloat_check(ctx, &s[0], nullptr);
+ sfloat_check(ctx, &s[1], nullptr);
+ sfloat_check(ctx, &s[2], nullptr);
+ sfloat_check(ctx, &s[3], nullptr);
+ sfloat_check(ctx, &s[4], nullptr);
+ sfloat_check(ctx, &s[5], nullptr);
sfloat_check(ctx, &s[6], "x");
sfloat_check(ctx, &s[7], "y");
sfloat_check(ctx, &s[8], "z");
lex_ctx_t ctx;
if (fold->parser->lex)
return parser_ctx(fold->parser);
-
memset(&ctx, 0, sizeof(ctx));
return ctx;
}
#define fold_immvalue_string(E) ((E)->constval.vstring)
fold_t *fold_init(parser_t *parser) {
- fold_t *fold = (fold_t*)mem_a(sizeof(fold_t));
- fold->parser = parser;
- fold->imm_float = NULL;
- fold->imm_vector = NULL;
- fold->imm_string = NULL;
+ fold_t *fold = new fold_t;
+ fold->parser = parser;
fold->imm_string_untranslate = util_htnew(FOLD_STRING_UNTRANSLATE_HTSIZE);
fold->imm_string_dotranslate = util_htnew(FOLD_STRING_DOTRANSLATE_HTSIZE);
* prime the tables with common constant values at constant
* locations.
*/
- (void)fold_constgen_float (fold, 0.0f, false);
- (void)fold_constgen_float (fold, 1.0f, false);
- (void)fold_constgen_float (fold, -1.0f, false);
- (void)fold_constgen_float (fold, 2.0f, false);
+ (void)fold_constgen_float(fold, 0.0f, false);
+ (void)fold_constgen_float(fold, 1.0f, false);
+ (void)fold_constgen_float(fold, -1.0f, false);
+ (void)fold_constgen_float(fold, 2.0f, false);
(void)fold_constgen_vector(fold, vec3_create(0.0f, 0.0f, 0.0f));
(void)fold_constgen_vector(fold, vec3_create(-1.0f, -1.0f, -1.0f));
}
bool fold_generate(fold_t *fold, ir_builder *ir) {
- /* generate globals for immediate folded values */
- size_t i;
+ // generate globals for immediate folded values
ast_value *cur;
-
- for (i = 0; i < vec_size(fold->imm_float); ++i)
- if (!ast_global_codegen ((cur = fold->imm_float[i]), ir, false)) goto err;
- for (i = 0; i < vec_size(fold->imm_vector); ++i)
- if (!ast_global_codegen((cur = fold->imm_vector[i]), ir, false)) goto err;
- for (i = 0; i < vec_size(fold->imm_string); ++i)
- if (!ast_global_codegen((cur = fold->imm_string[i]), ir, false)) goto err;
-
+ for (auto &it : fold->imm_float)
+ if (!ast_global_codegen((cur = it), ir, false)) goto err;
+ for (auto &it : fold->imm_vector)
+ if (!ast_global_codegen((cur = it), ir, false)) goto err;
+ for (auto &it : fold->imm_string)
+ if (!ast_global_codegen((cur = it), ir, false)) goto err;
return true;
-
err:
con_out("failed to generate global %s\n", cur->name);
ir_builder_delete(ir);
}
void fold_cleanup(fold_t *fold) {
- size_t i;
-
- for (i = 0; i < vec_size(fold->imm_float); ++i) ast_delete(fold->imm_float[i]);
- for (i = 0; i < vec_size(fold->imm_vector); ++i) ast_delete(fold->imm_vector[i]);
- for (i = 0; i < vec_size(fold->imm_string); ++i) ast_delete(fold->imm_string[i]);
-
- vec_free(fold->imm_float);
- vec_free(fold->imm_vector);
- vec_free(fold->imm_string);
+ for (auto &it : fold->imm_float) ast_delete(it);
+ for (auto &it : fold->imm_vector) ast_delete(it);
+ for (auto &it : fold->imm_string) ast_delete(it);
util_htdel(fold->imm_string_untranslate);
util_htdel(fold->imm_string_dotranslate);
- mem_d(fold);
+ delete fold;
}
ast_expression *fold_constgen_float(fold_t *fold, qcfloat_t value, bool inexact) {
- ast_value *out = NULL;
- size_t i;
-
- for (i = 0; i < vec_size(fold->imm_float); i++) {
- if (!memcmp(&fold->imm_float[i]->constval.vfloat, &value, sizeof(qcfloat_t)))
- return (ast_expression*)fold->imm_float[i];
- }
-
- out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_FLOAT);
- out->cvq = CV_CONST;
- out->hasvalue = true;
- out->inexact = inexact;
+ for (auto &it : fold->imm_float)
+ if (!memcmp(&it->constval.vfloat, &value, sizeof(qcfloat_t)))
+ return (ast_expression*)it;
+
+ ast_value *out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_FLOAT);
+ out->cvq = CV_CONST;
+ out->hasvalue = true;
+ out->inexact = inexact;
out->constval.vfloat = value;
- vec_push(fold->imm_float, out);
+ fold->imm_float.push_back(out);
return (ast_expression*)out;
}
ast_expression *fold_constgen_vector(fold_t *fold, vec3_t value) {
- ast_value *out;
- size_t i;
+ for (auto &it : fold->imm_vector)
+ if (vec3_cmp(it->constval.vvec, value))
+ return (ast_expression*)it;
- for (i = 0; i < vec_size(fold->imm_vector); i++) {
- if (vec3_cmp(fold->imm_vector[i]->constval.vvec, value))
- return (ast_expression*)fold->imm_vector[i];
- }
-
- out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_VECTOR);
- out->cvq = CV_CONST;
- out->hasvalue = true;
+ ast_value *out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_VECTOR);
+ out->cvq = CV_CONST;
+ out->hasvalue = true;
out->constval.vvec = value;
- vec_push(fold->imm_vector, out);
+ fold->imm_vector.push_back(out);
return (ast_expression*)out;
}
ast_expression *fold_constgen_string(fold_t *fold, const char *str, bool translate) {
hash_table_t *table = (translate) ? fold->imm_string_untranslate : fold->imm_string_dotranslate;
- ast_value *out = NULL;
- size_t hash = util_hthash(table, str);
+ ast_value *out = nullptr;
+ size_t hash = util_hthash(table, str);
if ((out = (ast_value*)util_htgeth(table, str, hash)))
return (ast_expression*)out;
if (translate) {
char name[32];
util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(fold->parser->translated++));
- out = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING);
+ out = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING);
out->expression.flags |= AST_FLAG_INCLUDE_DEF; /* def needs to be included for translatables */
- } else
- out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_STRING);
+ } else {
+ out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_STRING);
+ }
- out->cvq = CV_CONST;
- out->hasvalue = true;
- out->isimm = true;
+ out->cvq = CV_CONST;
+ out->hasvalue = true;
+ out->isimm = true;
out->constval.vstring = parser_strdup(str);
- vec_push(fold->imm_string, out);
+ fold->imm_string.push_back(out);
util_htseth(table, str, hash, out);
return (ast_expression*)out;
if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS))
goto inexact_possible;
- sfloat_check(fold_ctx(fold), &s, NULL);
+ sfloat_check(fold_ctx(fold), &s, nullptr);
inexact_possible:
return s.exceptionflags & SFLOAT_INEXACT;
if (!y && !z) {
ast_expression *out;
++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
- out = (ast_expression*)ast_member_new(fold_ctx(fold), (ast_expression*)sel, set[0]-'x', NULL);
+ out = (ast_expression*)ast_member_new(fold_ctx(fold), (ast_expression*)sel, set[0]-'x', nullptr);
out->node.keep = false;
((ast_member*)out)->rvalue = true;
if (x != -1.0f)
return (ast_expression*)ast_binary_new(fold_ctx(fold), INSTR_MUL_F, fold_constgen_float(fold, x, false), out);
}
- return NULL;
+ return nullptr;
}
if (isfloat(a)) {
if (fold_can_1(a)) {
/* Negation can produce inexact as well */
- bool inexact = fold_check_except_float(&sfloat_neg, fold, a, NULL);
+ bool inexact = fold_check_except_float(&sfloat_neg, fold, a, nullptr);
return fold_constgen_float(fold, -fold_immvalue_float(a), inexact);
}
} else if (isvector(a)) {
if (fold_can_1(a))
return fold_constgen_vector(fold, vec3_neg(fold_ctx(fold), fold_immvalue_vector(a)));
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_not(fold_t *fold, ast_value *a) {
return fold_constgen_float(fold, !fold_immvalue_string(a) || !*fold_immvalue_string(a), false);
}
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_add(fold_t *fold, ast_value *a, ast_value *b) {
fold_immvalue_vector(a),
fold_immvalue_vector(b)));
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_sub(fold_t *fold, ast_value *a, ast_value *b) {
fold_immvalue_vector(a),
fold_immvalue_vector(b)));
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_mul(fold_t *fold, ast_value *a, ast_value *b) {
}
}
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_div(fold_t *fold, ast_value *a, ast_value *b) {
);
}
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_mod(fold_t *fold, ast_value *a, ast_value *b) {
return (fold_can_2(a, b))
? fold_constgen_float(fold, fmod(fold_immvalue_float(a), fold_immvalue_float(b)), false)
- : NULL;
+ : nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_bor(fold_t *fold, ast_value *a, ast_value *b) {
return fold_constgen_vector(fold, vec3_orvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
}
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_band(fold_t *fold, ast_value *a, ast_value *b) {
return fold_constgen_vector(fold, vec3_andvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
}
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_xor(fold_t *fold, ast_value *a, ast_value *b) {
return fold_constgen_vector(fold, vec3_xorvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
}
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_lshift(fold_t *fold, ast_value *a, ast_value *b) {
if (fold_can_2(a, b) && isfloats(a, b))
return fold_constgen_float(fold, (qcfloat_t)floorf(fold_immvalue_float(a) * powf(2.0f, fold_immvalue_float(b))), false);
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_rshift(fold_t *fold, ast_value *a, ast_value *b) {
if (fold_can_2(a, b) && isfloats(a, b))
return fold_constgen_float(fold, (qcfloat_t)floorf(fold_immvalue_float(a) / powf(2.0f, fold_immvalue_float(b))), false);
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_andor(fold_t *fold, ast_value *a, ast_value *b, float expr) {
);
}
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_tern(fold_t *fold, ast_value *a, ast_value *b, ast_value *c) {
? (ast_expression*)b
: (ast_expression*)c;
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_exp(fold_t *fold, ast_value *a, ast_value *b) {
if (fold_can_2(a, b))
return fold_constgen_float(fold, (qcfloat_t)powf(fold_immvalue_float(a), fold_immvalue_float(b)), false);
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_lteqgt(fold_t *fold, ast_value *a, ast_value *b) {
if (fold_immvalue_float(a) == fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[0];
if (fold_immvalue_float(a) > fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[1];
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_ltgt(fold_t *fold, ast_value *a, ast_value *b, bool lt) {
return (lt) ? (ast_expression*)fold->imm_float[!!(fold_immvalue_float(a) < fold_immvalue_float(b))]
: (ast_expression*)fold->imm_float[!!(fold_immvalue_float(a) > fold_immvalue_float(b))];
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_cmp(fold_t *fold, ast_value *a, ast_value *b, bool ne) {
return (ast_expression*)fold->imm_float[!(ne ? vec3_cmp(la, lb) : !vec3_cmp(la, lb))];
}
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_bnot(fold_t *fold, ast_value *a) {
return fold_constgen_vector(fold, vec3_not(fold_immvalue_vector(a)));
}
}
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_cross(fold_t *fold, ast_value *a, ast_value *b) {
return fold_constgen_vector(fold, vec3_cross(fold_ctx(fold),
fold_immvalue_vector(a),
fold_immvalue_vector(b)));
- return NULL;
+ return nullptr;
}
static GMQCC_INLINE ast_expression *fold_op_length(fold_t *fold, ast_value *a) {
if (fold_can_1(a) && isstring(a))
return fold_constgen_float(fold, strlen(fold_immvalue_string(a)), false);
if (isarray(a))
- return fold_constgen_float(fold, vec_size(a->initlist), false);
- return NULL;
+ return fold_constgen_float(fold, a->initlist.size(), false);
+ return nullptr;
}
ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **opexprs) {
ast_value *a = (ast_value*)opexprs[0];
ast_value *b = (ast_value*)opexprs[1];
ast_value *c = (ast_value*)opexprs[2];
- ast_expression *e = NULL;
+ ast_expression *e = nullptr;
/* can a fold operation be applied to this operator usage? */
if (!info->folds)
- return NULL;
+ return nullptr;
switch(info->operands) {
- case 3: if(!c) return NULL;
- case 2: if(!b) return NULL;
+ case 3: if(!c) return nullptr;
+ case 2: if(!b) return nullptr;
case 1:
if(!a) {
compile_error(fold_ctx(fold), "internal error: fold_op no operands to fold\n");
- return NULL;
+ return nullptr;
}
}
}
#undef fold_op_case
compile_error(fold_ctx(fold), "internal error: attempted to constant-fold for unsupported operator");
- return NULL;
+ return nullptr;
}
/*
ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **arg) {
- ast_expression *ret = NULL;
+ ast_expression *ret = nullptr;
ast_value *a = (ast_value*)arg[0];
ast_value *b = (ast_value*)arg[1];
/*#define fold_can_2(X,Y) (fold_can_1(X) && fold_can_1(Y))*/
static ast_expression *fold_superfluous(ast_expression *left, ast_expression *right, int op) {
- ast_expression *swapped = NULL; /* using this as bool */
+ ast_expression *swapped = nullptr; /* using this as bool */
ast_value *load;
if (!ast_istype(right, ast_value) || !fold_can_1((load = (ast_value*)right))) {
}
if (!ast_istype(right, ast_value) || !fold_can_1((load = (ast_value*)right)))
- return NULL;
+ return nullptr;
switch (op) {
case INSTR_DIV_F:
if (swapped)
- return NULL;
+ return nullptr;
case INSTR_MUL_F:
if (fold_immvalue_float(load) == 1.0f) {
++opts_optimizationcount[OPTIM_PEEPHOLE];
case INSTR_SUB_F:
if (swapped)
- return NULL;
+ return nullptr;
case INSTR_ADD_F:
if (fold_immvalue_float(load) == 0.0f) {
++opts_optimizationcount[OPTIM_PEEPHOLE];
case INSTR_SUB_V:
if (swapped)
- return NULL;
+ return nullptr;
case INSTR_ADD_V:
if (vec3_cmp(fold_immvalue_vector(load), vec3_create(0, 0, 0))) {
++opts_optimizationcount[OPTIM_PEEPHOLE];
break;
}
- return NULL;
+ return nullptr;
}
ast_expression *fold_binary(lex_ctx_t ctx, int op, ast_expression *left, ast_expression *right) {
bool istrue = (fold_immvalue_float(condval) != 0.0f && branch->on_true);
bool isfalse = (fold_immvalue_float(condval) == 0.0f && branch->on_false);
ast_expression *path = (istrue) ? branch->on_true :
- (isfalse) ? branch->on_false : NULL;
+ (isfalse) ? branch->on_false : nullptr;
if (!path) {
/*
* no path to take implies that the evaluation is if(0) and there