⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 constfold.c

📁 cg编译器
💻 C
📖 第 1 页 / 共 2 页
字号:
        case GE_VS_OP:
            cmpfn = runtime_ops[base]->op_ge;
            op_offset = fexpr->bin.op - GE_OP;
            goto normal_cmpop;
        case EQ_OP:
        case EQ_V_OP:
        case EQ_SV_OP:
        case EQ_VS_OP:
            cmpfn = runtime_ops[base]->op_eq;
            op_offset = fexpr->bin.op - EQ_OP;
            goto normal_cmpop;
        case NE_OP:
        case NE_V_OP:
        case NE_SV_OP:
        case NE_VS_OP:
            cmpfn = runtime_ops[base]->op_ne;
            op_offset = fexpr->bin.op - NE_OP;
            goto normal_cmpop;
        case AND_OP:
        case AND_V_OP:
        case AND_SV_OP:
        case AND_VS_OP:
            DB (printf("fold AND");)
            binfn = runtime_ops[base]->op_and;
            op_offset = fexpr->bin.op - AND_OP;
            goto normal_binop;
        case XOR_OP:
        case XOR_V_OP:
        case XOR_SV_OP:
        case XOR_VS_OP:
            DB (printf("fold XOR");)
            binfn = runtime_ops[base]->op_xor;
            op_offset = fexpr->bin.op - XOR_OP;
            goto normal_binop;
        case OR_OP:
        case OR_V_OP:
        case OR_SV_OP:
        case OR_VS_OP:
            DB (printf("fold OR");)
            binfn = runtime_ops[base]->op_or;
            op_offset = fexpr->bin.op - OR_OP;
            goto normal_binop;
        case BAND_OP:
        case BAND_V_OP:
        case BAND_SV_OP:
        case BAND_VS_OP:
            DB (printf("fold BAND");)
            binfn = runtime_ops[base]->op_band;
            op_offset = fexpr->bin.op - BAND_OP;
            goto normal_binop;
        case BOR_OP:
        case BOR_V_OP:
        case BOR_SV_OP:
        case BOR_VS_OP:
            DB (printf("fold BOR");)
            binfn = runtime_ops[base]->op_bor;
            op_offset = fexpr->bin.op - BOR_OP;
            goto normal_binop;
        case ASSIGN_OP:
        case ASSIGN_V_OP:
        case ASSIGN_GEN_OP:
        case ASSIGN_MASKED_KV_OP:
        default:
            break;
        }
        break;
    case TRINARY_N:
        switch(fexpr->tri.op) {
        case COND_OP:
        case COND_V_OP:
        case COND_SV_OP:
        case COND_GEN_OP:
        case ASSIGN_COND_OP:
        case ASSIGN_COND_V_OP:
        case ASSIGN_COND_SV_OP:
        case ASSIGN_COND_GEN_OP:
        default:
            break;
        }
        break;
    default:
        break;
    }
    return rv;
} // ConstantFoldNode

static void int_neg(scalar_constant *r, const scalar_constant *a)
{
    r->i = - a->i;
}
static void int_not(scalar_constant *r, const scalar_constant *a)
{
    r->i = ~ a->i;
}
static void int_add(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->i = a->i + b->i;
}
static void int_sub(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->i = a->i - b->i;
}
static void int_mul(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->i = a->i * b->i;
}
static void int_div(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->i = a->i / b->i;
}
static void int_mod(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->i = a->i % b->i;
}
static void int_and(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->i = a->i & b->i;
}
static void int_or(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->i = a->i | b->i;
}
static void int_xor(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->i = a->i ^ b->i;
}
static void int_shr(scalar_constant *r, const scalar_constant *a, int b)
{
    r->i = a->i >> b;
}
static void int_shl(scalar_constant *r, const scalar_constant *a, int b)
{
    r->i = a->i << b;
}
static int int_lt(const scalar_constant *a, const scalar_constant *b)
{
    return a->i < b->i;
}
static int int_gt(const scalar_constant *a, const scalar_constant *b)
{
    return a->i > b->i;
}
static int int_le(const scalar_constant *a, const scalar_constant *b)
{
    return a->i <= b->i;
}
static int int_ge(const scalar_constant *a, const scalar_constant *b)
{
    return a->i >= b->i;
}
static int int_eq(const scalar_constant *a, const scalar_constant *b)
{
    return a->i == b->i;
}
static int int_ne(const scalar_constant *a, const scalar_constant *b)
{
    return a->i != b->i;
}

static void float_neg(scalar_constant *r, const scalar_constant *a)
{
    r->f = - a->f;
}
static void float_add(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = a->f + b->f;
}
static void float_sub(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = a->f - b->f;
}
static void float_mul(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = a->f * b->f;
}
static void float_div(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = a->f / b->f;
}
static int float_lt(const scalar_constant *a, const scalar_constant *b)
{
    return a->f < b->f;
}
static int float_gt(const scalar_constant *a, const scalar_constant *b)
{
    return a->f > b->f;
}
static int float_le(const scalar_constant *a, const scalar_constant *b)
{
    return a->f <= b->f;
}
static int float_ge(const scalar_constant *a, const scalar_constant *b)
{
    return a->f >= b->f;
}
static int float_eq(const scalar_constant *a, const scalar_constant *b)
{
    return a->f == b->f;
}
static int float_ne(const scalar_constant *a, const scalar_constant *b)
{
    return a->f != b->f;
}

static void bool_not(scalar_constant *r, const scalar_constant *a)
{
    r->i = !a->i;
}
static void bool_and(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->i = a->i && b->i;
}
static void bool_or(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->i = a->i || b->i;
}

static void int2float(scalar_constant *r, const scalar_constant *a)
{
    r->f = (float)a->i;
}
static void float2int(scalar_constant *r, const scalar_constant *a)
{
    r->i = (int)a->f;
}
static void copy(scalar_constant *r, const scalar_constant *a)
{
    *r = *a;
}
static void int2bool(scalar_constant *r, const scalar_constant *a)
{
    r->i = (a->i != 0);
}
static void bool2int(scalar_constant *r, const scalar_constant *a)
{
    r->i = a->i ? 1 : 0;
}
static void float2bool(scalar_constant *r, const scalar_constant *a)
{
    r->i = (a->f != 0);
}
static void bool2float(scalar_constant *r, const scalar_constant *a)
{
    r->f = a->i ? 1.0f : 0.0f;
}

static operations int_ops = {
    ICONST_OP,
    int_neg, int_not, 0,
    int_add, int_sub, int_mul, int_div, int_mod,
    int_and, int_or, int_xor, 0, 0,
    int_shr, int_shl,
    int_lt, int_gt, int_le, int_ge, int_eq, int_ne,
    { 0, 0, int2float, copy, 0, int2float, copy, int2bool, 0, 0, 0, 0, 0, 0, 0, 0, },
    { 0, 0, float2int, copy, 0, float2int, copy, bool2int, 0, 0, 0, 0, 0, 0, 0, 0, },
};

static operations float_ops = {
    FCONST_OP,
    float_neg, 0, 0,
    float_add, float_sub, float_mul, float_div, 0,
    0, 0, 0, 0, 0,
    0, 0,
    float_lt, float_gt, float_le, float_ge, float_eq, float_ne,
    { 0, 0, copy, float2int, 0, copy, float2int, float2bool, 0, 0, 0, 0, 0, 0, 0, 0, },
    { 0, 0, copy, int2float, 0, copy, int2float, bool2float, 0, 0, 0, 0, 0, 0, 0, 0, },
};

static operations bool_ops = {
    BCONST_OP,
    0, 0, bool_not,
    0, 0, 0, 0, 0,
    0, 0, 0, bool_and, bool_or,
    0, 0,
    0, 0, 0, 0, int_eq, int_ne,
    { 0, 0, bool2float, bool2int, 0, bool2float, bool2int, copy, 0, 0, 0, 0, 0, 0, 0, 0, },
    { 0, 0, float2bool, int2bool, 0, float2bool, int2bool, copy, 0, 0, 0, 0, 0, 0, 0, 0, },
};

operations *runtime_ops[TYPE_BASE_LAST_USER+1] = {
    0,          /* No type */
    0,          /* Undefined */
    &float_ops, /* cfloat */
    &int_ops,   /* cint */
    0,          /* void */
    &float_ops, /* float */
    &int_ops,   /* int */
    &bool_ops,  /* boolean */
    /* profile defined types: these will be set up by the profile */
    0, 0, 0, 0, 0, 0, 0, 0,
};


// round a value to half (S5.10, bias=14) precision
float round_half(double v) {
    int exp;
    double mant = frexp(v, &exp);
    int rndm = (int)(mant * 2048 + 0.5);
    if (exp > 17) {
        // overflow -- build the appropriately signed infinity
        v = ldexp(mant, 500) * 2;
    } else if (exp < -23) {
        // full underflow -- build appropraitely signed zero
        v = ldexp(mant, -500);
    } else {
        if (exp < -13) {
            // underflow -- round more to show denorm
            rndm >>= -(exp - 13);
            rndm <<= -(exp - 13);
        }
        v = ldexp(rndm/2048.0, exp);
    }
    return (float)v;
}

// round/clamp to fixed (signed 2.10) precision
#define FIXED_MAX (1.9990234375)
#define FIXED_MIN (-2.0)
float round_fixed(double v) {
    int tmp;
    if (v > FIXED_MAX) return FIXED_MAX;
    if (v < FIXED_MIN) return FIXED_MIN;
    tmp = (int)(v*1024 + 0.5);
    return (float)(tmp/1024.0);
}

static void half_neg(scalar_constant *r, const scalar_constant *a)
{
    r->f = round_half(- a->f);
}
static void half_add(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = round_half(a->f + b->f);
}
static void half_sub(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = round_half(a->f - b->f);
}
static void half_mul(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = round_half(a->f * b->f);
}
static void half_div(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = round_half(a->f / b->f);
}
#define half_lt float_lt
#define half_gt float_gt
#define half_le float_le
#define half_ge float_ge
#define half_eq float_eq
#define half_ne float_ne

static void fixed_neg(scalar_constant *r, const scalar_constant *a)
{
    r->f = round_fixed(- a->f);
}
static void fixed_add(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = round_fixed(a->f + b->f);
}
static void fixed_sub(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = round_fixed(a->f - b->f);
}
static void fixed_mul(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = round_fixed(a->f * b->f);
}
static void fixed_div(scalar_constant *r, const scalar_constant *a, const scalar_constant *b)
{
    r->f = round_fixed(a->f / b->f);
}
#define fixed_lt float_lt
#define fixed_gt float_gt
#define fixed_le float_le
#define fixed_ge float_ge
#define fixed_eq float_eq
#define fixed_ne float_ne


#define half2int float2int
#define half2float copy
#define half2bool float2bool
static void int2half(scalar_constant *r, const scalar_constant *a)
{
    r->f = round_half((float)a->i);
}
static void float2half(scalar_constant *r, const scalar_constant *a)
{
    r->f = round_half(a->f);
}
#define bool2half bool2float
#define fixed2int float2int
#define fixed2float copy
#define fixed2bool float2bool
static void int2fixed(scalar_constant *r, const scalar_constant *a)
{
    r->f = round_fixed((float)a->i);
}
static void float2fixed(scalar_constant *r, const scalar_constant *a)
{
    r->f = round_fixed(a->f);
}
#define bool2fixed bool2float
#define half2fixed float2fixed
#define fixed2half copy

static operations half_ops = {
    HCONST_OP,
    half_neg, 0, 0,
    half_add, half_sub, half_mul, half_div, 0,
    0, 0, 0, 0, 0,
    0, 0,
    half_lt, half_gt, half_le, half_ge, half_eq, half_ne,
    { 0, 0, half2float, half2int, 0, half2float, half2int, half2bool, 0, 0, 0, 0, 0, 0, 0, 0, },
    { 0, 0, float2half, int2half, 0, float2half, int2half, bool2half, 0, 0, 0, 0, 0, 0, 0, 0, },
};

static operations fixed_ops = {
    XCONST_OP,
    fixed_neg, 0, 0,
    fixed_add, fixed_sub, fixed_mul, fixed_div, 0,
    0, 0, 0, 0, 0,
    0, 0,
    fixed_lt, fixed_gt, fixed_le, fixed_ge, fixed_eq, fixed_ne,
    { 0, 0, fixed2float, fixed2int, 0, fixed2float, fixed2int, fixed2bool, 0, 0, 0, 0, 0, 0, 0, 0, },
    { 0, 0, float2fixed, int2fixed, 0, float2fixed, int2fixed, bool2fixed, 0, 0, 0, 0, 0, 0, 0, 0, },
};

void HAL_SetupHalfFixedTypes(int half, int fixed) {
    if (half) {
        runtime_ops[half] = &half_ops;
        half_ops.cvtTo[half] = copy;
        half_ops.cvtFrom[half] = copy;
    }
    if (fixed) {
        runtime_ops[fixed] = &fixed_ops;
        fixed_ops.cvtTo[fixed] = copy;
        fixed_ops.cvtFrom[fixed] = copy;
    }
    if (half && fixed) {
        half_ops.cvtTo[fixed] = half2fixed;
        half_ops.cvtFrom[fixed] = fixed2half;
        fixed_ops.cvtTo[half] = fixed2half;
        fixed_ops.cvtFrom[half] = half2fixed;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -