X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=fold.c;h=884e039969f569e983f87a37ba3387b5893f8155;hp=662601bc0ab61a8c91478f6d38cc46e5a8c8c4a9;hb=960cb7034af7240036a969a59f21e28e63a86df2;hpb=fa5ad1212eb2cac046ff66dffb472cec9b024b9f diff --git a/fold.c b/fold.c index 662601b..884e039 100644 --- a/fold.c +++ b/fold.c @@ -42,7 +42,6 @@ #define isvector(X) (((ast_expression*)(X))->vtype == TYPE_VECTOR) #define isstring(X) (((ast_expression*)(X))->vtype == TYPE_STRING) #define isfloats(X,Y) (isfloat (X) && isfloat (Y)) -#define isvectors(X,Y) (isvector (X) && isvector(Y)) /* * Implementation of basic vector math for vec3_t, for trivial constant @@ -76,17 +75,17 @@ static GMQCC_INLINE vec3_t vec3_neg(vec3_t a) { static GMQCC_INLINE vec3_t vec3_xor(vec3_t a, vec3_t b) { vec3_t out; - out.x = (qcfloat_t)((qcint_t)a.x ^ (qcint_t)b.x); - out.y = (qcfloat_t)((qcint_t)a.y ^ (qcint_t)b.y); - out.z = (qcfloat_t)((qcint_t)a.z ^ (qcint_t)b.z); + out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b.x)); + out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b.y)); + out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b.z)); return out; } static GMQCC_INLINE vec3_t vec3_xorvf(vec3_t a, qcfloat_t b) { vec3_t out; - out.x = (qcfloat_t)((qcint_t)a.x ^ (qcint_t)b); - out.y = (qcfloat_t)((qcint_t)a.y ^ (qcint_t)b); - out.z = (qcfloat_t)((qcint_t)a.z ^ (qcint_t)b); + out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b)); + out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b)); + out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b)); return out; } @@ -125,7 +124,8 @@ static GMQCC_INLINE bool vec3_pbool(vec3_t a) { } static GMQCC_INLINE bool fold_can_1(const ast_value *val) { - return (ast_istype((ast_expression*)val, ast_value) && val->hasvalue && val->cvq == CV_CONST && ((ast_expression*)val)->vtype != TYPE_FUNCTION); + return (ast_istype(((ast_expression*)(val)), ast_value) && val->hasvalue && (val->cvq == CV_CONST) && + ((ast_expression*)(val))->vtype != TYPE_FUNCTION); } static GMQCC_INLINE bool fold_can_2(const ast_value *v1, const ast_value *v2) { @@ -150,7 +150,7 @@ static GMQCC_INLINE bool fold_immediate_true(fold_t *fold, ast_value *v) { case TYPE_VECTOR: if (OPTS_FLAG(CORRECT_LOGIC)) return vec3_pbool(v->constval.vvec); - return !!v->constval.vvec.x; + return !!(v->constval.vvec.x); case TYPE_STRING: if (!v->constval.vstring) return false; @@ -323,7 +323,7 @@ static GMQCC_INLINE ast_expression *fold_op_mul_vec(fold_t *fold, vec3_t vec, as out = (ast_expression*)ast_member_new(fold_ctx(fold), (ast_expression*)sel, set[0]-'x', NULL); out->node.keep = false; ((ast_member*)out)->rvalue = true; - if (!x != -1) + if (x != -1) return (ast_expression*)ast_binary_new(fold_ctx(fold), INSTR_MUL_F, fold_constgen_float(fold, x), out); } return NULL; @@ -383,13 +383,17 @@ static GMQCC_INLINE ast_expression *fold_op_sub(fold_t *fold, ast_value *a, ast_ static GMQCC_INLINE ast_expression *fold_op_mul(fold_t *fold, ast_value *a, ast_value *b) { if (isfloat(a)) { - if (isfloat(b) && fold_can_2(a, b)) - return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(b), fold_immvalue_float(a))); - else if (fold_can_2(a, b)) - return fold_constgen_float(fold, fold_immvalue_float(a) * fold_immvalue_float(b)); + if (isvector(b)) { + if (fold_can_2(a, b)) + return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(b), fold_immvalue_float(a))); + } else { + if (fold_can_2(a, b)) + return fold_constgen_float(fold, fold_immvalue_float(a) * fold_immvalue_float(b)); + } } else if (isvector(a)) { - if (isfloat(b) && fold_can_2(a, b)) { - return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), fold_immvalue_float(b))); + if (isfloat(b)) { + if (fold_can_2(a, b)) + return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), fold_immvalue_float(b))); } else { if (fold_can_2(a, b)) { return fold_constgen_float(fold, vec3_mulvv(fold_immvalue_vector(a), fold_immvalue_vector(b))); @@ -416,8 +420,21 @@ static GMQCC_INLINE ast_expression *fold_op_div(fold_t *fold, ast_value *a, ast_ } else if (isvector(a)) { if (fold_can_2(a, b)) return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), 1.0f / fold_immvalue_float(b))); - else if (fold_can_1(b)) - return fold_constgen_float (fold, 1.0f / fold_immvalue_float(b)); + else { + return (ast_expression*)ast_binary_new( + fold_ctx(fold), + INSTR_MUL_VF, + (ast_expression*)a, + (fold_can_1(b)) + ? (ast_expression*)fold_constgen_float(fold, 1.0f / fold_immvalue_float(b)) + : (ast_expression*)ast_binary_new( + fold_ctx(fold), + INSTR_DIV_F, + (ast_expression*)fold->imm_float[1], + (ast_expression*)b + ) + ); + } } return NULL; } @@ -478,8 +495,8 @@ static GMQCC_INLINE ast_expression *fold_op_andor(fold_t *fold, ast_value *a, as fold, ((or) ? (fold_immediate_true(fold, a) || fold_immediate_true(fold, b)) : (fold_immediate_true(fold, a) && fold_immediate_true(fold, b))) - ? 1.0f - : 0.0f + ? 1 + : 0 ); } } @@ -547,8 +564,8 @@ ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **op } switch(info->id) { - case opid2('-', 'P'): return fold_op_neg (fold, a); - case opid2('!', 'P'): return fold_op_not (fold, a); + case opid2('-','P'): return fold_op_neg (fold, a); + case opid2('!','P'): return fold_op_not (fold, a); case opid1('+'): return fold_op_add (fold, a, b); case opid1('-'): return fold_op_sub (fold, a, b); case opid1('*'): return fold_op_mul (fold, a, b); @@ -568,5 +585,6 @@ ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **op case opid2('=','='): return fold_op_cmp (fold, a, b, false); case opid2('~','P'): return fold_op_bnot (fold, a); } + compile_error(fold_ctx(fold), "internal error: attempted to constant for unsupported operator"); return NULL; }