static void gen_farith (DisasContext *ctx, uint32_t op1,

                        int ft, int fs, int fd, int cc)

{

    const char *opn = "farith";

    const char *condnames[] = {

            "c.f",

            "c.un",

            "c.eq",

            "c.ueq",

            "c.olt",

            "c.ult",

            "c.ole",

            "c.ule",

            "c.sf",

            "c.ngle",

            "c.seq",

            "c.ngl",

            "c.lt",

            "c.nge",

            "c.le",

            "c.ngt",

    };

    const char *condnames_abs[] = {

            "cabs.f",

            "cabs.un",

            "cabs.eq",

            "cabs.ueq",

            "cabs.olt",

            "cabs.ult",

            "cabs.ole",

            "cabs.ule",

            "cabs.sf",

            "cabs.ngle",

            "cabs.seq",

            "cabs.ngl",

            "cabs.lt",

            "cabs.nge",

            "cabs.le",

            "cabs.ngt",

    };

    enum { BINOP, CMPOP, OTHEROP } optype = OTHEROP;

    uint32_t func = ctx->opcode & 0x3f;

    switch (ctx->opcode & FOP(0x3f, 0x1f)) {

    case FOP(0, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            TCGv_i32 fp1 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_load_fpr32(fp1, ft);

            gen_helper_float_add_s(fp0, fp0, fp1);

            tcg_temp_free_i32(fp1);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "add.s";

        optype = BINOP;

        break;

    case FOP(1, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            TCGv_i32 fp1 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_load_fpr32(fp1, ft);

            gen_helper_float_sub_s(fp0, fp0, fp1);

            tcg_temp_free_i32(fp1);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "sub.s";

        optype = BINOP;

        break;

    case FOP(2, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            TCGv_i32 fp1 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_load_fpr32(fp1, ft);

            gen_helper_float_mul_s(fp0, fp0, fp1);

            tcg_temp_free_i32(fp1);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "mul.s";

        optype = BINOP;

        break;

    case FOP(3, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            TCGv_i32 fp1 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_load_fpr32(fp1, ft);

            gen_helper_float_div_s(fp0, fp0, fp1);

            tcg_temp_free_i32(fp1);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "div.s";

        optype = BINOP;

        break;

    case FOP(4, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_sqrt_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "sqrt.s";

        break;

    case FOP(5, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_abs_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "abs.s";

        break;

    case FOP(6, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "mov.s";

        break;

    case FOP(7, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_chs_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "neg.s";

        break;

    case FOP(8, 16):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr32(fp32, fs);

            gen_helper_float_roundl_s(fp64, fp32);

            tcg_temp_free_i32(fp32);

            gen_store_fpr64(ctx, fp64, fd);

            tcg_temp_free_i64(fp64);

        }

        opn = "round.l.s";

        break;

    case FOP(9, 16):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr32(fp32, fs);

            gen_helper_float_truncl_s(fp64, fp32);

            tcg_temp_free_i32(fp32);

            gen_store_fpr64(ctx, fp64, fd);

            tcg_temp_free_i64(fp64);

        }

        opn = "trunc.l.s";

        break;

    case FOP(10, 16):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr32(fp32, fs);

            gen_helper_float_ceill_s(fp64, fp32);

            tcg_temp_free_i32(fp32);

            gen_store_fpr64(ctx, fp64, fd);

            tcg_temp_free_i64(fp64);

        }

        opn = "ceil.l.s";

        break;

    case FOP(11, 16):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr32(fp32, fs);

            gen_helper_float_floorl_s(fp64, fp32);

            tcg_temp_free_i32(fp32);

            gen_store_fpr64(ctx, fp64, fd);

            tcg_temp_free_i64(fp64);

        }

        opn = "floor.l.s";

        break;

    case FOP(12, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_roundw_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "round.w.s";

        break;

    case FOP(13, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_truncw_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "trunc.w.s";

        break;

    case FOP(14, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_ceilw_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "ceil.w.s";

        break;

    case FOP(15, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_floorw_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "floor.w.s";

        break;

    case FOP(17, 16):

        gen_movcf_s(fs, fd, (ft >> 2) & 0x7, ft & 0x1);

        opn = "movcf.s";

        break;

    case FOP(18, 16):

        {

            int l1 = gen_new_label();

            TCGv_i32 fp0;

            if (ft != 0) {

                tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[ft], 0, l1);

            }

            fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

            gen_set_label(l1);

        }

        opn = "movz.s";

        break;

    case FOP(19, 16):

        {

            int l1 = gen_new_label();

            TCGv_i32 fp0;

            if (ft != 0) {

                tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[ft], 0, l1);

                fp0 = tcg_temp_new_i32();

                gen_load_fpr32(fp0, fs);

                gen_store_fpr32(fp0, fd);

                tcg_temp_free_i32(fp0);

                gen_set_label(l1);

            }

        }

        opn = "movn.s";

        break;

    case FOP(21, 16):

        check_cop1x(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_recip_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "recip.s";

        break;

    case FOP(22, 16):

        check_cop1x(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_rsqrt_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "rsqrt.s";

        break;

    case FOP(28, 16):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            TCGv_i32 fp1 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_load_fpr32(fp1, fd);

            gen_helper_float_recip2_s(fp0, fp0, fp1);

            tcg_temp_free_i32(fp1);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "recip2.s";

        break;

    case FOP(29, 16):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_recip1_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "recip1.s";

        break;

    case FOP(30, 16):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_rsqrt1_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "rsqrt1.s";

        break;

    case FOP(31, 16):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            TCGv_i32 fp1 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_load_fpr32(fp1, ft);

            gen_helper_float_rsqrt2_s(fp0, fp0, fp1);

            tcg_temp_free_i32(fp1);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "rsqrt2.s";

        break;

    case FOP(33, 16):

        check_cp1_registers(ctx, fd);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr32(fp32, fs);

            gen_helper_float_cvtd_s(fp64, fp32);

            tcg_temp_free_i32(fp32);

            gen_store_fpr64(ctx, fp64, fd);

            tcg_temp_free_i64(fp64);

        }

        opn = "cvt.d.s";

        break;

    case FOP(36, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_cvtw_s(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "cvt.w.s";

        break;

    case FOP(37, 16):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr32(fp32, fs);

            gen_helper_float_cvtl_s(fp64, fp32);

            tcg_temp_free_i32(fp32);

            gen_store_fpr64(ctx, fp64, fd);

            tcg_temp_free_i64(fp64);

        }

        opn = "cvt.l.s";

        break;

    case FOP(38, 16):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp64 = tcg_temp_new_i64();

            TCGv_i32 fp32_0 = tcg_temp_new_i32();

            TCGv_i32 fp32_1 = tcg_temp_new_i32();

            gen_load_fpr32(fp32_0, fs);

            gen_load_fpr32(fp32_1, ft);

            tcg_gen_concat_i32_i64(fp64, fp32_0, fp32_1);

            tcg_temp_free_i32(fp32_1);

            tcg_temp_free_i32(fp32_0);

            gen_store_fpr64(ctx, fp64, fd);

            tcg_temp_free_i64(fp64);

        }

        opn = "cvt.ps.s";

        break;

    case FOP(48, 16):

    case FOP(49, 16):

    case FOP(50, 16):

    case FOP(51, 16):

    case FOP(52, 16):

    case FOP(53, 16):

    case FOP(54, 16):

    case FOP(55, 16):

    case FOP(56, 16):

    case FOP(57, 16):

    case FOP(58, 16):

    case FOP(59, 16):

    case FOP(60, 16):

    case FOP(61, 16):

    case FOP(62, 16):

    case FOP(63, 16):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            TCGv_i32 fp1 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_load_fpr32(fp1, ft);

            if (ctx->opcode & (1 << 6)) {

                check_cop1x(ctx);

                gen_cmpabs_s(func-48, fp0, fp1, cc);

                opn = condnames_abs[func-48];

            } else {

                gen_cmp_s(func-48, fp0, fp1, cc);

                opn = condnames[func-48];

            }

            tcg_temp_free_i32(fp0);

            tcg_temp_free_i32(fp1);

        }

        break;

    case FOP(0, 17):

        check_cp1_registers(ctx, fs | ft | fd);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            gen_helper_float_add_d(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "add.d";

        optype = BINOP;

        break;

    case FOP(1, 17):

        check_cp1_registers(ctx, fs | ft | fd);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            gen_helper_float_sub_d(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "sub.d";

        optype = BINOP;

        break;

    case FOP(2, 17):

        check_cp1_registers(ctx, fs | ft | fd);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            gen_helper_float_mul_d(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "mul.d";

        optype = BINOP;

        break;

    case FOP(3, 17):

        check_cp1_registers(ctx, fs | ft | fd);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            gen_helper_float_div_d(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "div.d";

        optype = BINOP;

        break;

    case FOP(4, 17):

        check_cp1_registers(ctx, fs | fd);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_sqrt_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "sqrt.d";

        break;

    case FOP(5, 17):

        check_cp1_registers(ctx, fs | fd);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_abs_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "abs.d";

        break;

    case FOP(6, 17):

        check_cp1_registers(ctx, fs | fd);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "mov.d";

        break;

    case FOP(7, 17):

        check_cp1_registers(ctx, fs | fd);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_chs_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "neg.d";

        break;

    case FOP(8, 17):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_roundl_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "round.l.d";

        break;

    case FOP(9, 17):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_truncl_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "trunc.l.d";

        break;

    case FOP(10, 17):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_ceill_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "ceil.l.d";

        break;

    case FOP(11, 17):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_floorl_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "floor.l.d";

        break;

    case FOP(12, 17):

        check_cp1_registers(ctx, fs);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp64, fs);

            gen_helper_float_roundw_d(fp32, fp64);

            tcg_temp_free_i64(fp64);

            gen_store_fpr32(fp32, fd);

            tcg_temp_free_i32(fp32);

        }

        opn = "round.w.d";

        break;

    case FOP(13, 17):

        check_cp1_registers(ctx, fs);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp64, fs);

            gen_helper_float_truncw_d(fp32, fp64);

            tcg_temp_free_i64(fp64);

            gen_store_fpr32(fp32, fd);

            tcg_temp_free_i32(fp32);

        }

        opn = "trunc.w.d";

        break;

    case FOP(14, 17):

        check_cp1_registers(ctx, fs);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp64, fs);

            gen_helper_float_ceilw_d(fp32, fp64);

            tcg_temp_free_i64(fp64);

            gen_store_fpr32(fp32, fd);

            tcg_temp_free_i32(fp32);

        }

        opn = "ceil.w.d";

        break;

    case FOP(15, 17):

        check_cp1_registers(ctx, fs);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp64, fs);

            gen_helper_float_floorw_d(fp32, fp64);

            tcg_temp_free_i64(fp64);

            gen_store_fpr32(fp32, fd);

            tcg_temp_free_i32(fp32);

        }

        opn = "floor.w.d";

        break;

    case FOP(17, 17):

        gen_movcf_d(ctx, fs, fd, (ft >> 2) & 0x7, ft & 0x1);

        opn = "movcf.d";

        break;

    case FOP(18, 17):

        {

            int l1 = gen_new_label();

            TCGv_i64 fp0;

            if (ft != 0) {

                tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[ft], 0, l1);

            }

            fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

            gen_set_label(l1);

        }

        opn = "movz.d";

        break;

    case FOP(19, 17):

        {

            int l1 = gen_new_label();

            TCGv_i64 fp0;

            if (ft != 0) {

                tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[ft], 0, l1);

                fp0 = tcg_temp_new_i64();

                gen_load_fpr64(ctx, fp0, fs);

                gen_store_fpr64(ctx, fp0, fd);

                tcg_temp_free_i64(fp0);

                gen_set_label(l1);

            }

        }

        opn = "movn.d";

        break;

    case FOP(21, 17):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_recip_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "recip.d";

        break;

    case FOP(22, 17):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_rsqrt_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "rsqrt.d";

        break;

    case FOP(28, 17):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            gen_helper_float_recip2_d(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "recip2.d";

        break;

    case FOP(29, 17):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_recip1_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "recip1.d";

        break;

    case FOP(30, 17):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_rsqrt1_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "rsqrt1.d";

        break;

    case FOP(31, 17):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            gen_helper_float_rsqrt2_d(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "rsqrt2.d";

        break;

    case FOP(48, 17):

    case FOP(49, 17):

    case FOP(50, 17):

    case FOP(51, 17):

    case FOP(52, 17):

    case FOP(53, 17):

    case FOP(54, 17):

    case FOP(55, 17):

    case FOP(56, 17):

    case FOP(57, 17):

    case FOP(58, 17):

    case FOP(59, 17):

    case FOP(60, 17):

    case FOP(61, 17):

    case FOP(62, 17):

    case FOP(63, 17):

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            if (ctx->opcode & (1 << 6)) {

                check_cop1x(ctx);

                check_cp1_registers(ctx, fs | ft);

                gen_cmpabs_d(func-48, fp0, fp1, cc);

                opn = condnames_abs[func-48];

            } else {

                check_cp1_registers(ctx, fs | ft);

                gen_cmp_d(func-48, fp0, fp1, cc);

                opn = condnames[func-48];

            }

            tcg_temp_free_i64(fp0);

            tcg_temp_free_i64(fp1);

        }

        break;

    case FOP(32, 17):

        check_cp1_registers(ctx, fs);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp64, fs);

            gen_helper_float_cvts_d(fp32, fp64);

            tcg_temp_free_i64(fp64);

            gen_store_fpr32(fp32, fd);

            tcg_temp_free_i32(fp32);

        }

        opn = "cvt.s.d";

        break;

    case FOP(36, 17):

        check_cp1_registers(ctx, fs);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp64, fs);

            gen_helper_float_cvtw_d(fp32, fp64);

            tcg_temp_free_i64(fp64);

            gen_store_fpr32(fp32, fd);

            tcg_temp_free_i32(fp32);

        }

        opn = "cvt.w.d";

        break;

    case FOP(37, 17):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_cvtl_d(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "cvt.l.d";

        break;

    case FOP(32, 20):

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_cvts_w(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "cvt.s.w";

        break;

    case FOP(33, 20):

        check_cp1_registers(ctx, fd);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr32(fp32, fs);

            gen_helper_float_cvtd_w(fp64, fp32);

            tcg_temp_free_i32(fp32);

            gen_store_fpr64(ctx, fp64, fd);

            tcg_temp_free_i64(fp64);

        }

        opn = "cvt.d.w";

        break;

    case FOP(32, 21):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp32 = tcg_temp_new_i32();

            TCGv_i64 fp64 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp64, fs);

            gen_helper_float_cvts_l(fp32, fp64);

            tcg_temp_free_i64(fp64);

            gen_store_fpr32(fp32, fd);

            tcg_temp_free_i32(fp32);

        }

        opn = "cvt.s.l";

        break;

    case FOP(33, 21):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_cvtd_l(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "cvt.d.l";

        break;

    case FOP(38, 20):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_cvtps_pw(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "cvt.ps.pw";

        break;

    case FOP(0, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            gen_helper_float_add_ps(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "add.ps";

        break;

    case FOP(1, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            gen_helper_float_sub_ps(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "sub.ps";

        break;

    case FOP(2, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            gen_helper_float_mul_ps(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "mul.ps";

        break;

    case FOP(5, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_abs_ps(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "abs.ps";

        break;

    case FOP(6, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "mov.ps";

        break;

    case FOP(7, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_chs_ps(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "neg.ps";

        break;

    case FOP(17, 22):

        check_cp1_64bitmode(ctx);

        gen_movcf_ps(fs, fd, (ft >> 2) & 0x7, ft & 0x1);

        opn = "movcf.ps";

        break;

    case FOP(18, 22):

        check_cp1_64bitmode(ctx);

        {

            int l1 = gen_new_label();

            TCGv_i64 fp0;

            if (ft != 0)

                tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[ft], 0, l1);

            fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

            gen_set_label(l1);

        }

        opn = "movz.ps";

        break;

    case FOP(19, 22):

        check_cp1_64bitmode(ctx);

        {

            int l1 = gen_new_label();

            TCGv_i64 fp0;

            if (ft != 0) {

                tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[ft], 0, l1);

                fp0 = tcg_temp_new_i64();

                gen_load_fpr64(ctx, fp0, fs);

                gen_store_fpr64(ctx, fp0, fd);

                tcg_temp_free_i64(fp0);

                gen_set_label(l1);

            }

        }

        opn = "movn.ps";

        break;

    case FOP(24, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, ft);

            gen_load_fpr64(ctx, fp1, fs);

            gen_helper_float_addr_ps(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "addr.ps";

        break;

    case FOP(26, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, ft);

            gen_load_fpr64(ctx, fp1, fs);

            gen_helper_float_mulr_ps(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "mulr.ps";

        break;

    case FOP(28, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, fd);

            gen_helper_float_recip2_ps(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "recip2.ps";

        break;

    case FOP(29, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_recip1_ps(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "recip1.ps";

        break;

    case FOP(30, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_rsqrt1_ps(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "rsqrt1.ps";

        break;

    case FOP(31, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            gen_helper_float_rsqrt2_ps(fp0, fp0, fp1);

            tcg_temp_free_i64(fp1);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "rsqrt2.ps";

        break;

    case FOP(32, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32h(fp0, fs);

            gen_helper_float_cvts_pu(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "cvt.s.pu";

        break;

    case FOP(36, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_helper_float_cvtpw_ps(fp0, fp0);

            gen_store_fpr64(ctx, fp0, fd);

            tcg_temp_free_i64(fp0);

        }

        opn = "cvt.pw.ps";

        break;

    case FOP(40, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_helper_float_cvts_pl(fp0, fp0);

            gen_store_fpr32(fp0, fd);

            tcg_temp_free_i32(fp0);

        }

        opn = "cvt.s.pl";

        break;

    case FOP(44, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            TCGv_i32 fp1 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_load_fpr32(fp1, ft);

            gen_store_fpr32h(fp0, fd);

            gen_store_fpr32(fp1, fd);

            tcg_temp_free_i32(fp0);

            tcg_temp_free_i32(fp1);

        }

        opn = "pll.ps";

        break;

    case FOP(45, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            TCGv_i32 fp1 = tcg_temp_new_i32();

            gen_load_fpr32(fp0, fs);

            gen_load_fpr32h(fp1, ft);

            gen_store_fpr32(fp1, fd);

            gen_store_fpr32h(fp0, fd);

            tcg_temp_free_i32(fp0);

            tcg_temp_free_i32(fp1);

        }

        opn = "plu.ps";

        break;

    case FOP(46, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            TCGv_i32 fp1 = tcg_temp_new_i32();

            gen_load_fpr32h(fp0, fs);

            gen_load_fpr32(fp1, ft);

            gen_store_fpr32(fp1, fd);

            gen_store_fpr32h(fp0, fd);

            tcg_temp_free_i32(fp0);

            tcg_temp_free_i32(fp1);

        }

        opn = "pul.ps";

        break;

    case FOP(47, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i32 fp0 = tcg_temp_new_i32();

            TCGv_i32 fp1 = tcg_temp_new_i32();

            gen_load_fpr32h(fp0, fs);

            gen_load_fpr32h(fp1, ft);

            gen_store_fpr32(fp1, fd);

            gen_store_fpr32h(fp0, fd);

            tcg_temp_free_i32(fp0);

            tcg_temp_free_i32(fp1);

        }

        opn = "puu.ps";

        break;

    case FOP(48, 22):

    case FOP(49, 22):

    case FOP(50, 22):

    case FOP(51, 22):

    case FOP(52, 22):

    case FOP(53, 22):

    case FOP(54, 22):

    case FOP(55, 22):

    case FOP(56, 22):

    case FOP(57, 22):

    case FOP(58, 22):

    case FOP(59, 22):

    case FOP(60, 22):

    case FOP(61, 22):

    case FOP(62, 22):

    case FOP(63, 22):

        check_cp1_64bitmode(ctx);

        {

            TCGv_i64 fp0 = tcg_temp_new_i64();

            TCGv_i64 fp1 = tcg_temp_new_i64();

            gen_load_fpr64(ctx, fp0, fs);

            gen_load_fpr64(ctx, fp1, ft);

            if (ctx->opcode & (1 << 6)) {

                gen_cmpabs_ps(func-48, fp0, fp1, cc);

                opn = condnames_abs[func-48];

            } else {

                gen_cmp_ps(func-48, fp0, fp1, cc);

                opn = condnames[func-48];

            }

            tcg_temp_free_i64(fp0);

            tcg_temp_free_i64(fp1);

        }

        break;

    default:

        MIPS_INVAL(opn);

        generate_exception (ctx, EXCP_RI);

        return;

    }

    switch (optype) {

    case BINOP:

        MIPS_DEBUG("%s %s, %s, %s", opn, fregnames[fd], fregnames[fs], fregnames[ft]);

        break;

    case CMPOP:

        MIPS_DEBUG("%s %s,%s", opn, fregnames[fs], fregnames[ft]);

        break;

    default:

        MIPS_DEBUG("%s %s,%s", opn, fregnames[fd], fregnames[fs]);

        break;

    }

}


static void mkv_write_block(AVFormatContext *s, AVIOContext *pb,

                            unsigned int blockid, AVPacket *pkt, int flags)

{

    MatroskaMuxContext *mkv = s->priv_data;

    AVCodecContext *codec = s->streams[pkt->stream_index]->codec;

    uint8_t *data = NULL, *side_data = NULL;

    int offset = 0, size = pkt->size, side_data_size = 0;

    int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;

    uint64_t additional_id = 0;

    int64_t discard_padding = 0;

    ebml_master block_group, block_additions, block_more;

    av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "

           "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",

           avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);

    if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 &&

        (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))

        ff_avc_parse_nal_units_buf(pkt->data, &data, &size);

    else if (codec->codec_id == AV_CODEC_ID_HEVC && codec->extradata_size > 6 &&

             (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))

        /* extradata is Annex B, assume the bitstream is too and convert it */

        ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL);

    else if (codec->codec_id == AV_CODEC_ID_WAVPACK) {

        int ret = mkv_strip_wavpack(pkt->data, &data, &size);

        if (ret < 0) {

            av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n");

            return;

        }

    } else

        data = pkt->data;

    if (codec->codec_id == AV_CODEC_ID_PRORES) {

        /* Matroska specification requires to remove the first QuickTime atom

         */

        size  -= 8;

        offset = 8;

    }

    side_data = av_packet_get_side_data(pkt,

                                        AV_PKT_DATA_SKIP_SAMPLES,

                                        &side_data_size);

    if (side_data && side_data_size >= 10) {

        discard_padding = av_rescale_q(AV_RL32(side_data + 4),

                                       (AVRational){1, codec->sample_rate},

                                       (AVRational){1, 1000000000});

    }

    side_data = av_packet_get_side_data(pkt,

                                        AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,

                                        &side_data_size);

    if (side_data) {

        additional_id = AV_RB64(side_data);

        side_data += 8;

        side_data_size -= 8;

    }

    if ((side_data_size && additional_id == 1) || discard_padding) {

        block_group = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, 0);

        blockid = MATROSKA_ID_BLOCK;

    }

    put_ebml_id(pb, blockid);

    put_ebml_num(pb, size + 4, 0);

    // this assumes stream_index is less than 126

    avio_w8(pb, 0x80 | (mkv->is_dash ? mkv->dash_track_number : (pkt->stream_index + 1)));

    avio_wb16(pb, ts - mkv->cluster_pts);

    avio_w8(pb, flags);

    avio_write(pb, data + offset, size);

    if (data != pkt->data)

        av_free(data);

    if (discard_padding) {

        put_ebml_sint(pb, MATROSKA_ID_DISCARDPADDING, discard_padding);

    }

    if (side_data_size && additional_id == 1) {

        block_additions = start_ebml_master(pb, MATROSKA_ID_BLOCKADDITIONS, 0);

        block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);

        put_ebml_uint(pb, MATROSKA_ID_BLOCKADDID, 1);

        put_ebml_id(pb, MATROSKA_ID_BLOCKADDITIONAL);

        put_ebml_num(pb, side_data_size, 0);

        avio_write(pb, side_data, side_data_size);

        end_ebml_master(pb, block_more);

        end_ebml_master(pb, block_additions);

    }

    if ((side_data_size && additional_id == 1) || discard_padding) {

        end_ebml_master(pb, block_group);

    }

}


static void omap2_mpu_reset(void *opaque)

{

    struct omap_mpu_state_s *mpu = (struct omap_mpu_state_s *) opaque;

    omap_inth_reset(mpu->ih[0]);

    omap_dma_reset(mpu->dma);

    omap_prcm_reset(mpu->prcm);

    omap_sysctl_reset(mpu->sysc);

    omap_gp_timer_reset(mpu->gptimer[0]);

    omap_gp_timer_reset(mpu->gptimer[1]);

    omap_gp_timer_reset(mpu->gptimer[2]);

    omap_gp_timer_reset(mpu->gptimer[3]);

    omap_gp_timer_reset(mpu->gptimer[4]);

    omap_gp_timer_reset(mpu->gptimer[5]);

    omap_gp_timer_reset(mpu->gptimer[6]);

    omap_gp_timer_reset(mpu->gptimer[7]);

    omap_gp_timer_reset(mpu->gptimer[8]);

    omap_gp_timer_reset(mpu->gptimer[9]);

    omap_gp_timer_reset(mpu->gptimer[10]);

    omap_gp_timer_reset(mpu->gptimer[11]);

    omap_synctimer_reset(mpu->synctimer);

    omap_sdrc_reset(mpu->sdrc);

    omap_gpmc_reset(mpu->gpmc);

    omap_dss_reset(mpu->dss);

    omap_uart_reset(mpu->uart[0]);

    omap_uart_reset(mpu->uart[1]);

    omap_uart_reset(mpu->uart[2]);

    omap_mmc_reset(mpu->mmc);

    omap_mcspi_reset(mpu->mcspi[0]);

    omap_mcspi_reset(mpu->mcspi[1]);

    omap_i2c_reset(mpu->i2c[0]);

    omap_i2c_reset(mpu->i2c[1]);

    cpu_reset(mpu->env);

}


void do_pci_device_hot_remove(Monitor *mon, const QDict *qdict)

{

    pci_device_hot_remove(mon, qdict_get_str(qdict, "pci_addr"));

}


static void register_multipage(AddressSpaceDispatch *d,

                               MemoryRegionSection *section)

{

    hwaddr start_addr = section->offset_within_address_space;

    uint16_t section_index = phys_section_add(section);

    uint64_t num_pages = int128_get64(int128_rshift(section->size,

                                                    TARGET_PAGE_BITS));

    assert(num_pages);

    phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index);

}


static void test_visitor_in_errors(TestInputVisitorData *data,

                                   const void *unused)

{

    TestStruct *p = NULL;

    Error *err = NULL;

    Visitor *v;

    strList *q = NULL;

    UserDefTwo *r = NULL;

    WrapAlternate *s = NULL;

    v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', "

                                "'string': -42 }");

    visit_type_TestStruct(v, NULL, &p, &err);

    error_free_or_abort(&err);

    g_assert(!p);

    v = visitor_input_test_init(data, "[ '1', '2', false, '3' ]");

    visit_type_strList(v, NULL, &q, &err);

    error_free_or_abort(&err);

    assert(!q);

    v = visitor_input_test_init(data, "{ 'str':'hi' }");

    visit_type_UserDefTwo(v, NULL, &r, &err);

    error_free_or_abort(&err);

    assert(!r);

    v = visitor_input_test_init(data, "{ }");

    visit_type_WrapAlternate(v, NULL, &s, &err);

    error_free_or_abort(&err);

    assert(!s);

}


static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)

{

    BusChild *kid, *next;

    PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);

    /* Execute any pending removes during reset */

    while (s->acpi_pcihp_pci_status[bsel].down) {

        acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);

    }

    s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;

    s->acpi_pcihp_pci_status[bsel].device_present = 0;

    if (!bus) {

        return;

    }

    QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {

        DeviceState *qdev = kid->child;

        PCIDevice *pdev = PCI_DEVICE(qdev);

        int slot = PCI_SLOT(pdev->devfn);

        if (acpi_pcihp_pc_no_hotplug(s, pdev)) {

            s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);

        }

        s->acpi_pcihp_pci_status[bsel].device_present |= (1U << slot);

    }

}


static void ahci_idp_write(void *opaque, target_phys_addr_t addr,

                           uint64_t val, unsigned size)

{

    AHCIState *s = opaque;

    if (addr == s->idp_offset) {

        /* index register - mask off reserved bits */

        s->idp_index = (uint32_t)val & ((AHCI_MEM_BAR_SIZE - 1) & ~3);

    } else if (addr == s->idp_offset + 4) {

        /* data register - do memory write at location selected by index */

        ahci_mem_write(opaque, s->idp_index, val, size);

    }

}


static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)

{

    BDRVNBDState *s = bs->opaque;

    QDict *opts = qdict_new();

    QObject *saddr_qdict;

    Visitor *ov;

    const char *host = NULL, *port = NULL, *path = NULL;

    if (s->saddr->type == SOCKET_ADDRESS_KIND_INET) {

        const InetSocketAddress *inet = s->saddr->u.inet.data;

        if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) {

            host = inet->host;

            port = inet->port;

        }

    } else if (s->saddr->type == SOCKET_ADDRESS_KIND_UNIX) {

        path = s->saddr->u.q_unix.data->path;

    }

    qdict_put(opts, "driver", qstring_from_str("nbd"));

    if (path && s->export) {

        snprintf(bs->exact_filename, sizeof(bs->exact_filename),

                 "nbd+unix:///%s?socket=%s", s->export, path);

    } else if (path && !s->export) {

        snprintf(bs->exact_filename, sizeof(bs->exact_filename),

                 "nbd+unix://?socket=%s", path);

    } else if (host && s->export) {

        snprintf(bs->exact_filename, sizeof(bs->exact_filename),

                 "nbd://%s:%s/%s", host, port, s->export);

    } else if (host && !s->export) {

        snprintf(bs->exact_filename, sizeof(bs->exact_filename),

                 "nbd://%s:%s", host, port);

    }

    ov = qobject_output_visitor_new(&saddr_qdict);

    visit_type_SocketAddress(ov, NULL, &s->saddr, &error_abort);

    visit_complete(ov, &saddr_qdict);

    visit_free(ov);

    qdict_put_obj(opts, "server", saddr_qdict);

    if (s->export) {

        qdict_put(opts, "export", qstring_from_str(s->export));

    }

    if (s->tlscredsid) {

        qdict_put(opts, "tls-creds", qstring_from_str(s->tlscredsid));

    }

    qdict_flatten(opts);

    bs->full_open_options = opts;

}


static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)

{

    VirtIOBalloon *s = VIRTIO_BALLOON(vdev);

    VirtQueueElement *elem = &s->stats_vq_elem;

    VirtIOBalloonStat stat;

    size_t offset = 0;

    qemu_timeval tv;

    if (!virtqueue_pop(vq, elem)) {

        goto out;

    }

    /* Initialize the stats to get rid of any stale values.  This is only

     * needed to handle the case where a guest supports fewer stats than it

     * used to (ie. it has booted into an old kernel).

     */

    reset_stats(s);

    while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))

           == sizeof(stat)) {

        uint16_t tag = virtio_tswap16(vdev, stat.tag);

        uint64_t val = virtio_tswap64(vdev, stat.val);

        offset += sizeof(stat);

        if (tag < VIRTIO_BALLOON_S_NR)

            s->stats[tag] = val;

    }

    s->stats_vq_offset = offset;

    if (qemu_gettimeofday(&tv) < 0) {

        fprintf(stderr, "warning: %s: failed to get time of day\n", __func__);

        goto out;

    }

    s->stats_last_update = tv.tv_sec;

out:

    if (balloon_stats_enabled(s)) {

        balloon_stats_change_timer(s, s->stats_poll_interval);

    }

}


ISABus *isa_bus_new(DeviceState *dev)

{

    if (isabus) {

        fprintf(stderr, "Can't create a second ISA bus\n");

        return NULL;

    }

    if (NULL == dev) {

        dev = qdev_create(NULL, "isabus-bridge");

        qdev_init(dev);

    }

    isabus = FROM_QBUS(ISABus, qbus_create(&isa_bus_info, dev, NULL));

    return isabus;

}


void os_set_proc_name(const char *s)

{

#if defined(PR_SET_NAME)

    char name[16];

    if (!s)

        return;

    name[sizeof(name) - 1] = 0;

    strncpy(name, s, sizeof(name));

    /* Could rewrite argv[0] too, but that's a bit more complicated.

       This simple way is enough for `top'. */

    if (prctl(PR_SET_NAME, name)) {

        perror("unable to change process name");

        exit(1);

    }

#else

    fprintf(stderr, "Change of process name not supported by your OS\n");

    exit(1);

#endif

}


static int oss_poll_out (HWVoiceOut *hw)

{

    OSSVoiceOut *oss = (OSSVoiceOut *) hw;

    return qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);

}


void OPPROTO op_addzeo (void)

{

    do_addzeo();

    RETURN();

}


static void avc_loopfilter_cb_or_cr_inter_edge_hor_msa(uint8_t *data,

                                                       uint8_t bs0, uint8_t bs1,

                                                       uint8_t bs2, uint8_t bs3,

                                                       uint8_t tc0, uint8_t tc1,

                                                       uint8_t tc2, uint8_t tc3,

                                                       uint8_t alpha_in,

                                                       uint8_t beta_in,

                                                       uint32_t img_width)

{

    v16u8 alpha, beta;

    v8i16 tmp_vec;

    v8i16 bs = { 0 };

    v8i16 tc = { 0 };

    v16u8 p0, q0, p0_asub_q0, p1_asub_p0, q1_asub_q0;

    v16u8 is_less_than;

    v16u8 is_less_than_beta, is_less_than_alpha, is_bs_greater_than0;

    v8i16 p0_r, q0_r;

    v16u8 p1_org, p0_org, q0_org, q1_org;

    v8i16 p1_org_r, p0_org_r, q0_org_r, q1_org_r;

    v16i8 negate_tc, sign_negate_tc;

    v8i16 tc_r, negate_tc_r;

    v16i8 zero = { 0 };

    tmp_vec = (v8i16) __msa_fill_b(bs0);

    bs = __msa_insve_h(bs, 0, tmp_vec);

    tmp_vec = (v8i16) __msa_fill_b(bs1);

    bs = __msa_insve_h(bs, 1, tmp_vec);

    tmp_vec = (v8i16) __msa_fill_b(bs2);

    bs = __msa_insve_h(bs, 2, tmp_vec);

    tmp_vec = (v8i16) __msa_fill_b(bs3);

    bs = __msa_insve_h(bs, 3, tmp_vec);

    if (!__msa_test_bz_v((v16u8) bs)) {

        tmp_vec = (v8i16) __msa_fill_b(tc0);

        tc = __msa_insve_h(tc, 0, tmp_vec);

        tmp_vec = (v8i16) __msa_fill_b(tc1);

        tc = __msa_insve_h(tc, 1, tmp_vec);

        tmp_vec = (v8i16) __msa_fill_b(tc2);

        tc = __msa_insve_h(tc, 2, tmp_vec);

        tmp_vec = (v8i16) __msa_fill_b(tc3);

        tc = __msa_insve_h(tc, 3, tmp_vec);

        is_bs_greater_than0 = (v16u8) (zero < (v16i8) bs);

        alpha = (v16u8) __msa_fill_b(alpha_in);

        beta = (v16u8) __msa_fill_b(beta_in);

        p1_org = LOAD_UB(data - (img_width << 1));

        p0_org = LOAD_UB(data - img_width);

        q0_org = LOAD_UB(data);

        q1_org = LOAD_UB(data + img_width);

        p0_asub_q0 = __msa_asub_u_b(p0_org, q0_org);

        p1_asub_p0 = __msa_asub_u_b(p1_org, p0_org);

        q1_asub_q0 = __msa_asub_u_b(q1_org, q0_org);

        is_less_than_alpha = (p0_asub_q0 < alpha);

        is_less_than_beta = (p1_asub_p0 < beta);

        is_less_than = is_less_than_beta & is_less_than_alpha;

        is_less_than_beta = (q1_asub_q0 < beta);

        is_less_than = is_less_than_beta & is_less_than;

        is_less_than = is_less_than & is_bs_greater_than0;

        is_less_than = (v16u8) __msa_ilvr_d((v2i64) zero, (v2i64) is_less_than);

        if (!__msa_test_bz_v(is_less_than)) {

            negate_tc = zero - (v16i8) tc;

            sign_negate_tc = __msa_clti_s_b(negate_tc, 0);

            negate_tc_r = (v8i16) __msa_ilvr_b(sign_negate_tc, negate_tc);

            tc_r = (v8i16) __msa_ilvr_b(zero, (v16i8) tc);

            p1_org_r = (v8i16) __msa_ilvr_b(zero, (v16i8) p1_org);

            p0_org_r = (v8i16) __msa_ilvr_b(zero, (v16i8) p0_org);

            q0_org_r = (v8i16) __msa_ilvr_b(zero, (v16i8) q0_org);

            q1_org_r = (v8i16) __msa_ilvr_b(zero, (v16i8) q1_org);

            AVC_LOOP_FILTER_P0Q0(q0_org_r, p0_org_r, p1_org_r, q1_org_r,

                                 negate_tc_r, tc_r, p0_r, q0_r);

            p0 = (v16u8) __msa_pckev_b(zero, (v16i8) p0_r);

            q0 = (v16u8) __msa_pckev_b(zero, (v16i8) q0_r);

            p0_org = __msa_bmnz_v(p0_org, p0, is_less_than);

            q0_org = __msa_bmnz_v(q0_org, q0, is_less_than);

            STORE_UB(q0_org, data);

            STORE_UB(p0_org, (data - img_width));

        }

    }

}


static int64_t truehd_layout(int chanmap)

{

    int layout = 0, i;

    for (i = 0; i < 13; i++)

        layout |= thd_layout[i] * ((chanmap >> i) & 1);

    return layout;

}


void copy_picture_field(TInterlaceContext *tinterlace,

                        uint8_t *dst[4], int dst_linesize[4],

                        const uint8_t *src[4], int src_linesize[4],

                        enum AVPixelFormat format, int w, int src_h,

                        int src_field, int interleave, int dst_field,

                        int flags)

{

    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);

    int hsub = desc->log2_chroma_w;

    int plane, vsub = desc->log2_chroma_h;

    int k = src_field == FIELD_UPPER_AND_LOWER ? 1 : 2;

    int h;

    for (plane = 0; plane < desc->nb_components; plane++) {

        int lines = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(src_h, vsub) : src_h;

        int cols  = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(    w, hsub) : w;

        uint8_t *dstp = dst[plane];

        const uint8_t *srcp = src[plane];

        int srcp_linesize = src_linesize[plane] * k;

        int dstp_linesize = dst_linesize[plane] * (interleave ? 2 : 1);

        lines = (lines + (src_field == FIELD_UPPER)) / k;

        if (src_field == FIELD_LOWER)

            srcp += src_linesize[plane];

        if (interleave && dst_field == FIELD_LOWER)

            dstp += dst_linesize[plane];

        // Low-pass filtering is required when creating an interlaced destination from

        // a progressive source which contains high-frequency vertical detail.

        // Filtering will reduce interlace 'twitter' and Moire patterning.

        if (flags & TINTERLACE_FLAG_VLPF || flags & TINTERLACE_FLAG_CVLPF) {

            int x = 0;

            if (flags & TINTERLACE_FLAG_CVLPF)

                x = 1;

            for (h = lines; h > 0; h--) {

                ptrdiff_t pref = src_linesize[plane];

                ptrdiff_t mref = -pref;

                if (h >= (lines - x))  mref = 0; // there is no line above

                else if (h <= (1 + x)) pref = 0; // there is no line below

                tinterlace->lowpass_line(dstp, cols, srcp, mref, pref);

                dstp += dstp_linesize;

                srcp += srcp_linesize;

            }

        } else {

            av_image_copy_plane(dstp, dstp_linesize, srcp, srcp_linesize, cols, lines);

        }

    }

}


static int check_jni_invocation(void *log_ctx)

{

    int ret = AVERROR_EXTERNAL;

    void *handle = NULL;

    void **jni_invocation = NULL;

    handle = dlopen(NULL, RTLD_LOCAL);

    if (!handle) {

        goto done;

    }

    jni_invocation = (void **)dlsym(handle, "_ZN13JniInvocation15jni_invocation_E");

    if (!jni_invocation) {

        av_log(log_ctx, AV_LOG_ERROR, "Could not find JniInvocation::jni_invocation_ symbol\n");

        goto done;

    }

    ret = !(jni_invocation != NULL && *jni_invocation != NULL);

done:

    if (handle) {

        dlclose(handle);

    }

    return ret;

}


static int gif_image_write_header(uint8_t **bytestream,

                                  int width, int height, int loop_count,

                                  uint32_t *palette)

{

    int i;

    unsigned int v;

    bytestream_put_buffer(bytestream, "GIF", 3);

    bytestream_put_buffer(bytestream, "89a", 3);

    bytestream_put_le16(bytestream, width);

    bytestream_put_le16(bytestream, height);

    bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */

    bytestream_put_byte(bytestream, 0x1f); /* background color index */

    bytestream_put_byte(bytestream, 0); /* aspect ratio */

    /* the global palette */

    if (!palette) {

        bytestream_put_buffer(bytestream, (const unsigned char *)gif_clut, 216*3);

        for(i=0;i<((256-216)*3);i++)

            bytestream_put_byte(bytestream, 0);

    } else {

        for(i=0;i<256;i++) {

            v = palette[i];

            bytestream_put_be24(bytestream, v);

        }

    }

        /*        update: this is the 'NETSCAPE EXTENSION' that allows for looped animated gif

                see http://members.aol.com/royalef/gifabout.htm#net-extension

                byte   1       : 33 (hex 0x21) GIF Extension code

                byte   2       : 255 (hex 0xFF) Application Extension Label

                byte   3       : 11 (hex (0x0B) Length of Application Block

                                         (eleven bytes of data to follow)

                bytes  4 to 11 : "NETSCAPE"

                bytes 12 to 14 : "2.0"

                byte  15       : 3 (hex 0x03) Length of Data Sub-Block

                                         (three bytes of data to follow)

                byte  16       : 1 (hex 0x01)

                bytes 17 to 18 : 0 to 65535, an unsigned integer in

                                         lo-hi byte format. This indicate the

                                         number of iterations the loop should

                                         be executed.

                bytes 19       : 0 (hex 0x00) a Data Sub-block Terminator

        */

    /* application extension header */

#ifdef GIF_ADD_APP_HEADER

    if (loop_count >= 0 && loop_count <= 65535) {

        bytestream_put_byte(bytestream, 0x21);

        bytestream_put_byte(bytestream, 0xff);

        bytestream_put_byte(bytestream, 0x0b);

        bytestream_put_buffer(bytestream, "NETSCAPE2.0", 11);  // bytes 4 to 14

        bytestream_put_byte(bytestream, 0x03); // byte 15

        bytestream_put_byte(bytestream, 0x01); // byte 16

        bytestream_put_le16(bytestream, (uint16_t)loop_count);

        bytestream_put_byte(bytestream, 0x00); // byte 19

    }

#endif

    return 0;

}


static int decode_p_mbs(VC9Context *v)

{

    int x, y, current_mb = 0, i; /* MB/Block Position info */

    int skip_mb_bit = 0, cbpcy; /* MB/B skip */

    int hybrid_pred, ac_pred; /* Prediction types */

    int mb_has_coeffs = 1  /* last_flag */, mb_is_intra;

    int dmv_x, dmv_y; /* Differential MV components */

    int mv_mode_bit = 0; /* mv_mode_bit: 1MV=0, 4MV=0 */

    int mqdiff, mquant; /* MB quantization */

    int tt_block; /* MB Transform type */

    static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },

        offset_table[6] = { 0, 1, 3, 7, 15, 31 };

    int k_x, k_y; /* Long MV fixed bitlength */

    int hpel_flag, intra_flag; /* Some MB properties */

    int index, index1; /* LUT indices */

    int val, sign;

    /* Select ttmb table depending on pq */

    if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0];

    else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1];

    else v->ttmb_vlc = &vc9_ttmb_vlc[2];

    /* Select proper long MV range */

    switch (v->mvrange)

    {

    case 1: k_x = 10; k_y = 9; break;

    case 2: k_x = 12; k_y = 10; break;

    case 3: k_x = 13; k_y = 11; break;

    default: /*case 0 too */ k_x = 9; k_y = 8; break;

    }

    hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL

    k_x -= hpel_flag;

    k_y -= hpel_flag;

    for (y=0; y<v->height_mb; y++)

    {

        for (x=0; x<v->width_mb; x++)

        {

            if (v->mv_type_mb_plane[current_mb])

                mv_mode_bit = get_bits(&v->gb, 1);

            if (0) //skipmb is rawmode

                skip_mb_bit = get_bits(&v->gb, 1);

            if (!mv_mode_bit) /* 1MV mode */

            {

                if (!v->skip_mb_plane[current_mb])

                {

                    GET_MVDATA();

                    /* hybrid mv pred, 8.3.5.3.4 */

                    if (v->mv_mode == MV_PMODE_1MV ||

                        v->mv_mode == MV_PMODE_MIXED_MV)

                        hybrid_pred = get_bits(&v->gb, 1);

                    if (mb_is_intra && !mb_has_coeffs)

                    {

                        GET_MQUANT();

                        ac_pred = get_bits(&v->gb, 1);

                    }

                    else if (mb_has_coeffs)

                    {

                        if (mb_is_intra) ac_pred = get_bits(&v->gb, 1);

                        cbpcy = get_vlc2(&v->gb, v->cbpcy_vlc->table,

                                         VC9_CBPCY_P_VLC_BITS, 2);

                        GET_MQUANT();

                    }

                    if (!v->ttmbf)

                        v->ttfrm = get_vlc2(&v->gb, v->ttmb_vlc->table,

                                            VC9_TTMB_VLC_BITS, 2);

                    //Decode blocks from that mb wrt cbpcy

                }

                else //Skipped

                {

                    /* hybrid mv pred, 8.3.5.3.4 */

                    if (v->mv_mode == MV_PMODE_1MV ||

                        v->mv_mode == MV_PMODE_MIXED_MV)

                        hybrid_pred = get_bits(&v->gb, 1);

                }

            } //1MV mode

            else //4MV mode

            {

              if (!v->skip_mb_plane[current_mb] /* unskipped MB */)

                {

                    cbpcy = get_vlc2(&v->gb, v->cbpcy_vlc->table,

                                     VC9_CBPCY_P_VLC_BITS, 2);

                    for (i=0; i<4; i++) //For all 4 Y blocks

                    {

                        if (cbpcy & (1<<6) /* cbpcy set for this block */)

                        {

                            GET_MVDATA();

                        }

                        if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)

                            hybrid_pred = get_bits(&v->gb, 1);

                        GET_MQUANT();

                        if (mb_is_intra /* One of the 4 blocks is intra */ &&

                            index /* non-zero pred for that block */)

                            ac_pred = get_bits(&v->gb, 1);

                        if (!v->ttmbf)

                            tt_block = get_vlc2(&v->gb, v->ttmb_vlc->table,

                                                VC9_TTMB_VLC_BITS, 2);

                        /* TODO: Process blocks wrt cbpcy */

                        /* Prepare cbpcy for next block */

                        cbpcy <<= 1;

                    }

                }

                else //Skipped MB

                {

                    for (i=0; i<4; i++) //All 4 Y blocks

                    {

                        if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)

                            hybrid_pred = get_bits(&v->gb, 1);

                        /* FIXME: do something */

                    }

                }

            }

        }

        current_mb++;

    }

    return 0;

}


matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,

                     int64_t pos, uint64_t cluster_time, uint64_t duration,

                     int is_keyframe, int is_bframe)

{

    int res = 0;

    int track;

    AVStream *st;

    AVPacket *pkt;

    uint8_t *origdata = data;

    int16_t block_time;

    uint32_t *lace_size = NULL;

    int n, flags, laces = 0;

    uint64_t num;

    /* first byte(s): tracknum */

    if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {

        av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");

        av_free(origdata);

        return res;

    }

    data += n;

    size -= n;

    /* fetch track from num */

    track = matroska_find_track_by_num(matroska, num);

    if (size <= 3 || track < 0 || track >= matroska->num_tracks) {

        av_log(matroska->ctx, AV_LOG_INFO,

               "Invalid stream %d or size %u\n", track, size);

        av_free(origdata);

        return res;

    }

    if (matroska->tracks[track]->stream_index < 0)

        return res;

    st = matroska->ctx->streams[matroska->tracks[track]->stream_index];

    if (st->discard >= AVDISCARD_ALL) {

        av_free(origdata);

        return res;

    }

    if (duration == AV_NOPTS_VALUE)

        duration = matroska->tracks[track]->default_duration;

    /* block_time (relative to cluster time) */

    block_time = AV_RB16(data);

    data += 2;

    flags = *data++;

    size -= 3;

    if (is_keyframe == -1)

        is_keyframe = flags & 1 ? PKT_FLAG_KEY : 0;

    if (matroska->skip_to_keyframe) {

        if (!is_keyframe || st != matroska->skip_to_stream)

            return res;

        matroska->skip_to_keyframe = 0;

    }

    switch ((flags & 0x06) >> 1) {

        case 0x0: /* no lacing */

            laces = 1;

            lace_size = av_mallocz(sizeof(int));

            lace_size[0] = size;

            break;

        case 0x1: /* xiph lacing */

        case 0x2: /* fixed-size lacing */

        case 0x3: /* EBML lacing */

            if (size == 0) {

                res = -1;

                break;

            }

            laces = (*data) + 1;

            data += 1;

            size -= 1;

            lace_size = av_mallocz(laces * sizeof(int));

            switch ((flags & 0x06) >> 1) {

                case 0x1: /* xiph lacing */ {

                    uint8_t temp;

                    uint32_t total = 0;

                    for (n = 0; res == 0 && n < laces - 1; n++) {

                        while (1) {

                            if (size == 0) {

                                res = -1;

                                break;

                            }

                            temp = *data;

                            lace_size[n] += temp;

                            data += 1;

                            size -= 1;

                            if (temp != 0xff)

                                break;

                        }

                        total += lace_size[n];

                    }

                    lace_size[n] = size - total;

                    break;

                }

                case 0x2: /* fixed-size lacing */

                    for (n = 0; n < laces; n++)

                        lace_size[n] = size / laces;

                    break;

                case 0x3: /* EBML lacing */ {

                    uint32_t total;

                    n = matroska_ebmlnum_uint(data, size, &num);

                    if (n < 0) {

                        av_log(matroska->ctx, AV_LOG_INFO,

                               "EBML block data error\n");

                        break;

                    }

                    data += n;

                    size -= n;

                    total = lace_size[0] = num;

                    for (n = 1; res == 0 && n < laces - 1; n++) {

                        int64_t snum;

                        int r;

                        r = matroska_ebmlnum_sint (data, size, &snum);

                        if (r < 0) {

                            av_log(matroska->ctx, AV_LOG_INFO,

                                   "EBML block data error\n");

                            break;

                        }

                        data += r;

                        size -= r;

                        lace_size[n] = lace_size[n - 1] + snum;

                        total += lace_size[n];

                    }

                    lace_size[n] = size - total;

                    break;

                }

            }

            break;

    }

    if (res == 0) {

        int real_v = matroska->tracks[track]->flags & MATROSKA_TRACK_REAL_V;

        uint64_t timecode = AV_NOPTS_VALUE;

        if (cluster_time != (uint64_t)-1 && cluster_time + block_time >= 0)

            timecode = cluster_time + block_time;

        for (n = 0; n < laces; n++) {

            int slice, slices = 1;

            if (real_v) {

                slices = *data++ + 1;

                lace_size[n]--;

            }

            for (slice=0; slice<slices; slice++) {

                int slice_size, slice_offset = 0;

                if (real_v)

                    slice_offset = rv_offset(data, slice, slices);

                if (slice+1 == slices)

                    slice_size = lace_size[n] - slice_offset;

                else

                    slice_size = rv_offset(data, slice+1, slices) - slice_offset;

                if (st->codec->codec_id == CODEC_ID_RA_288 ||

                    st->codec->codec_id == CODEC_ID_COOK ||

                    st->codec->codec_id == CODEC_ID_ATRAC3) {

                    MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];

                    int a = st->codec->block_align;

                    int sps = audiotrack->sub_packet_size;

                    int cfs = audiotrack->coded_framesize;

                    int h = audiotrack->sub_packet_h;

                    int y = audiotrack->sub_packet_cnt;

                    int w = audiotrack->frame_size;

                    int x;

                    if (!audiotrack->pkt_cnt) {

                        if (st->codec->codec_id == CODEC_ID_RA_288)

                            for (x=0; x<h/2; x++)

                                memcpy(audiotrack->buf+x*2*w+y*cfs,

                                       data+x*cfs, cfs);

                        else

                            for (x=0; x<w/sps; x++)

                                memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);

                        if (++audiotrack->sub_packet_cnt >= h) {

                            audiotrack->sub_packet_cnt = 0;

                            audiotrack->pkt_cnt = h*w / a;

                        }

                    }

                    while (audiotrack->pkt_cnt) {

                        pkt = av_mallocz(sizeof(AVPacket));

                        av_new_packet(pkt, a);

                        memcpy(pkt->data, audiotrack->buf

                               + a * (h*w / a - audiotrack->pkt_cnt--), a);

                        pkt->pos = pos;

                        pkt->stream_index = matroska->tracks[track]->stream_index;

                        matroska_queue_packet(matroska, pkt);

                    }

                } else {

                    int offset = 0;

                    if (st->codec->codec_id == CODEC_ID_TEXT

                        && ((MatroskaSubtitleTrack *)(matroska->tracks[track]))->ass) {

                        int i;

                        for (i=0; i<8 && data[slice_offset+offset]; offset++)

                            if (data[slice_offset+offset] == ',')

                                i++;

                    }

                    pkt = av_mallocz(sizeof(AVPacket));

                    /* XXX: prevent data copy... */

                    if (av_new_packet(pkt, slice_size-offset) < 0) {

                        res = AVERROR(ENOMEM);

                        n = laces-1;

                        break;

                    }

                    memcpy (pkt->data, data+slice_offset+offset, slice_size-offset);

                    if (n == 0)

                        pkt->flags = is_keyframe;

                    pkt->stream_index = matroska->tracks[track]->stream_index;

                    pkt->pts = timecode;

                    pkt->pos = pos;

                    pkt->duration = duration;

                    matroska_queue_packet(matroska, pkt);

                }

                if (timecode != AV_NOPTS_VALUE)

                    timecode = duration ? timecode + duration : AV_NOPTS_VALUE;

            }

            data += lace_size[n];

        }

    }

    av_free(lace_size);

    av_free(origdata);

    return res;

}


static void vfio_listener_region_add(MemoryListener *listener,

                                     MemoryRegionSection *section)

{

    VFIOContainer *container = container_of(listener, VFIOContainer,

                                            iommu_data.listener);

    hwaddr iova, end;

    void *vaddr;

    int ret;

    assert(!memory_region_is_iommu(section->mr));

    if (vfio_listener_skipped_section(section)) {

        DPRINTF("SKIPPING region_add %"HWADDR_PRIx" - %"PRIx64"\n",

                section->offset_within_address_space,

                section->offset_within_address_space +

                int128_get64(int128_sub(section->size, int128_one())));

        return;

    }

    if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=

                 (section->offset_within_region & ~TARGET_PAGE_MASK))) {

        error_report("%s received unaligned region", __func__);

        return;

    }

    iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);

    end = (section->offset_within_address_space + int128_get64(section->size)) &

          TARGET_PAGE_MASK;

    if (iova >= end) {

        return;

    }

    vaddr = memory_region_get_ram_ptr(section->mr) +

            section->offset_within_region +

            (iova - section->offset_within_address_space);

    DPRINTF("region_add %"HWADDR_PRIx" - %"HWADDR_PRIx" [%p]\n",

            iova, end - 1, vaddr);

    memory_region_ref(section->mr);

    ret = vfio_dma_map(container, iova, end - iova, vaddr, section->readonly);

    if (ret) {

        error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", "

                     "0x%"HWADDR_PRIx", %p) = %d (%m)",

                     container, iova, end - iova, vaddr, ret);

    }

}


static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)

{

    AVStream *st = c->fc->streams[c->fc->nb_streams-1];

    int len = 0;

    uint8_t *buf;

    uint32_t type;

    uint32_t ctype;

    print_atom("hdlr", atom);

    get_byte(pb); /* version */

    get_byte(pb); get_byte(pb); get_byte(pb); /* flags */

    /* component type */

    ctype = get_le32(pb);

    type = get_le32(pb); /* component subtype */

#ifdef DEBUG

    printf("ctype= %c%c%c%c (0x%08lx)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (long) ctype);

    printf("stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);

#endif

#ifdef DEBUG

/* XXX: yeah this is ugly... */

    if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */

        if(type == MKTAG('v', 'i', 'd', 'e'))

            puts("hdlr: vide");

        else if(type == MKTAG('s', 'o', 'u', 'n'))

            puts("hdlr: soun");

    } else if(ctype == 0) { /* MP4 */

        if(type == MKTAG('v', 'i', 'd', 'e'))

            puts("hdlr: vide");

        else if(type == MKTAG('s', 'o', 'u', 'n'))

            puts("hdlr: soun");

        else if(type == MKTAG('o', 'd', 's', 'm'))

            puts("hdlr: odsm");

        else if(type == MKTAG('s', 'd', 's', 'm'))

            puts("hdlr: sdsm");

    } else puts("hdlr: meta");

#endif

    if(ctype == MKTAG('m', 'h', 'l', 'r')) { /* MOV */

        /* helps parsing the string hereafter... */

        c->mp4 = 0;

        if(type == MKTAG('v', 'i', 'd', 'e'))

            st->codec.codec_type = CODEC_TYPE_VIDEO;

        else if(type == MKTAG('s', 'o', 'u', 'n'))

            st->codec.codec_type = CODEC_TYPE_AUDIO;

    } else if(ctype == 0) { /* MP4 */

        /* helps parsing the string hereafter... */

        c->mp4 = 1;

        if(type == MKTAG('v', 'i', 'd', 'e'))

            st->codec.codec_type = CODEC_TYPE_VIDEO;

        else if(type == MKTAG('s', 'o', 'u', 'n'))

            st->codec.codec_type = CODEC_TYPE_AUDIO;

    }

    get_be32(pb); /* component  manufacture */

    get_be32(pb); /* component flags */

    get_be32(pb); /* component flags mask */

    if(atom.size <= 24)

        return 0; /* nothing left to read */

    /* XXX: MP4 uses a C string, not a pascal one */

    /* component name */

    if(c->mp4) {

        /* .mp4: C string */

        while(get_byte(pb) && (++len < (atom.size - 24)));

    } else {

        /* .mov: PASCAL string */

        len = get_byte(pb);

#ifdef DEBUG

	buf = (uint8_t*) av_malloc(len+1);

	if (buf) {

	    get_buffer(pb, buf, len);

	    buf[len] = '\0';

	    printf("**buf='%s'\n", buf);

	    av_free(buf);

	} else

#endif

	    url_fskip(pb, len);

    }

    return 0;

}


static void qemu_rdma_dump_gid(const char *who, struct rdma_cm_id *id)

{

    char sgid[33];

    char dgid[33];

    inet_ntop(AF_INET6, &id->route.addr.addr.ibaddr.sgid, sgid, sizeof sgid);

    inet_ntop(AF_INET6, &id->route.addr.addr.ibaddr.dgid, dgid, sizeof dgid);

    DPRINTF("%s Source GID: %s, Dest GID: %s\n", who, sgid, dgid);

}


static int v410_decode_frame(AVCodecContext *avctx, void *data,
                             int *data_size, AVPacket *avpkt)
{
    AVFrame *pic = avctx->coded_frame;
    uint8_t *src = avpkt->data;
    uint16_t *y, *u, *v;
    uint32_t val;
    int i, j;
    if (pic->data[0])
        avctx->release_buffer(avctx, pic);
    pic->reference = 0;
    if (avctx->get_buffer(avctx, pic) < 0) {
        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
        return AVERROR(ENOMEM);
    pic->key_frame = 1;
    pic->pict_type = FF_I_TYPE;
    y = (uint16_t *)pic->data[0];
    u = (uint16_t *)pic->data[1];
    v = (uint16_t *)pic->data[2];
    for (i = 0; i < avctx->height; i++) {
        for (j = 0; j < avctx->width; j++) {
            val = AV_RL32(src);
            u[j] = (val >>  2) & 0x3FF;
            y[j] = (val >> 12) & 0x3FF;
            v[j] = (val >> 22);
            src += 4;
        y += pic->linesize[0] >> 1;
        u += pic->linesize[1] >> 1;
        v += pic->linesize[2] >> 1;
    *data_size = sizeof(AVFrame);
    *(AVFrame *)data = *pic;
    return avpkt->size;


static void host_memory_backend_init(Object *obj)

{

    HostMemoryBackend *backend = MEMORY_BACKEND(obj);

    backend->merge = qemu_opt_get_bool(qemu_get_machine_opts(),

                                       "mem-merge", true);

    backend->dump = qemu_opt_get_bool(qemu_get_machine_opts(),

                                      "dump-guest-core", true);

    backend->prealloc = mem_prealloc;

    object_property_add_bool(obj, "merge",

                        host_memory_backend_get_merge,

                        host_memory_backend_set_merge, NULL);

    object_property_add_bool(obj, "dump",

                        host_memory_backend_get_dump,

                        host_memory_backend_set_dump, NULL);

    object_property_add_bool(obj, "prealloc",

                        host_memory_backend_get_prealloc,

                        host_memory_backend_set_prealloc, NULL);

    object_property_add(obj, "size", "int",

                        host_memory_backend_get_size,

                        host_memory_backend_set_size, NULL, NULL, NULL);

    object_property_add(obj, "host-nodes", "int",

                        host_memory_backend_get_host_nodes,

                        host_memory_backend_set_host_nodes, NULL, NULL, NULL);

    object_property_add_enum(obj, "policy", "HostMemPolicy",

                             HostMemPolicy_lookup,

                             host_memory_backend_get_policy,

                             host_memory_backend_set_policy, NULL);

}


static void adaptive_gain_control(float *out, const float *in,

                                  const float *speech_synth,

                                  int size, float alpha, float *gain_mem)

{

    int i;

    float speech_energy = 0.0, postfilter_energy = 0.0, gain_scale_factor;

    float mem = *gain_mem;

    for (i = 0; i < size; i++) {

        speech_energy     += fabsf(speech_synth[i]);

        postfilter_energy += fabsf(in[i]);

    }

    gain_scale_factor = (1.0 - alpha) * speech_energy / postfilter_energy;

    for (i = 0; i < size; i++) {

        mem = alpha * mem + gain_scale_factor;

        out[i] = in[i] * mem;

    }

    *gain_mem = mem;

}


void *qxl_phys2virt(PCIQXLDevice *qxl, QXLPHYSICAL pqxl, int group_id)

{

    uint64_t phys   = le64_to_cpu(pqxl);

    uint32_t slot   = (phys >> (64 -  8)) & 0xff;

    uint64_t offset = phys & 0xffffffffffff;

    switch (group_id) {

    case MEMSLOT_GROUP_HOST:

        return (void *)(intptr_t)offset;

    case MEMSLOT_GROUP_GUEST:

        PANIC_ON(slot >= NUM_MEMSLOTS);

        PANIC_ON(!qxl->guest_slots[slot].active);

        PANIC_ON(offset < qxl->guest_slots[slot].delta);

        offset -= qxl->guest_slots[slot].delta;

        PANIC_ON(offset > qxl->guest_slots[slot].size)

        return qxl->guest_slots[slot].ptr + offset;

    default:

        PANIC_ON(1);

    }

}


static void ps_add_squares_c(INTFLOAT *dst, const INTFLOAT (*src)[2], int n)

{

    int i;

    for (i = 0; i < n; i++)

        dst[i] += AAC_MADD28(src[i][0], src[i][0], src[i][1], src[i][1]);

}


static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPRMachineState *spapr,

                                uint32_t token, uint32_t nargs,

                                target_ulong args, uint32_t nret,

                                target_ulong rets)

{

    uint32_t config_addr = rtas_ld(args, 0);

    uint64_t buid = rtas_ldq(args, 1);

    unsigned int func = rtas_ld(args, 3);

    unsigned int req_num = rtas_ld(args, 4); /* 0 == remove all */

    unsigned int seq_num = rtas_ld(args, 5);

    unsigned int ret_intr_type;

    unsigned int irq, max_irqs = 0;

    sPAPRPHBState *phb = NULL;

    PCIDevice *pdev = NULL;

    spapr_pci_msi *msi;

    int *config_addr_key;

    switch (func) {

    case RTAS_CHANGE_MSI_FN:

    case RTAS_CHANGE_FN:

        ret_intr_type = RTAS_TYPE_MSI;

        break;

    case RTAS_CHANGE_MSIX_FN:

        ret_intr_type = RTAS_TYPE_MSIX;

        break;

    default:

        error_report("rtas_ibm_change_msi(%u) is not implemented", func);

        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);

        return;

    }

    /* Fins sPAPRPHBState */

    phb = spapr_pci_find_phb(spapr, buid);

    if (phb) {

        pdev = spapr_pci_find_dev(spapr, buid, config_addr);

    }

    if (!phb || !pdev) {

        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);

        return;

    }

    /* Releasing MSIs */

    if (!req_num) {

        msi = (spapr_pci_msi *) g_hash_table_lookup(phb->msi, &config_addr);

        if (!msi) {

            trace_spapr_pci_msi("Releasing wrong config", config_addr);

            rtas_st(rets, 0, RTAS_OUT_HW_ERROR);

            return;

        }

        xics_free(spapr->icp, msi->first_irq, msi->num);

        if (msi_present(pdev)) {

            spapr_msi_setmsg(pdev, 0, false, 0, 0);

        }

        if (msix_present(pdev)) {

            spapr_msi_setmsg(pdev, 0, true, 0, 0);

        }

        g_hash_table_remove(phb->msi, &config_addr);

        trace_spapr_pci_msi("Released MSIs", config_addr);

        rtas_st(rets, 0, RTAS_OUT_SUCCESS);

        rtas_st(rets, 1, 0);

        return;

    }

    /* Enabling MSI */

    /* Check if the device supports as many IRQs as requested */

    if (ret_intr_type == RTAS_TYPE_MSI) {

        max_irqs = msi_nr_vectors_allocated(pdev);

    } else if (ret_intr_type == RTAS_TYPE_MSIX) {

        max_irqs = pdev->msix_entries_nr;

    }

    if (!max_irqs) {

        error_report("Requested interrupt type %d is not enabled for device %x",

                     ret_intr_type, config_addr);

        rtas_st(rets, 0, -1); /* Hardware error */

        return;

    }

    /* Correct the number if the guest asked for too many */

    if (req_num > max_irqs) {

        trace_spapr_pci_msi_retry(config_addr, req_num, max_irqs);

        req_num = max_irqs;

        irq = 0; /* to avoid misleading trace */

        goto out;

    }

    /* Allocate MSIs */

    irq = xics_alloc_block(spapr->icp, 0, req_num, false,

                           ret_intr_type == RTAS_TYPE_MSI);

    if (!irq) {

        error_report("Cannot allocate MSIs for device %x", config_addr);

        rtas_st(rets, 0, RTAS_OUT_HW_ERROR);

        return;

    }

    /* Setup MSI/MSIX vectors in the device (via cfgspace or MSIX BAR) */

    spapr_msi_setmsg(pdev, SPAPR_PCI_MSI_WINDOW, ret_intr_type == RTAS_TYPE_MSIX,

                     irq, req_num);

    /* Add MSI device to cache */

    msi = g_new(spapr_pci_msi, 1);

    msi->first_irq = irq;

    msi->num = req_num;

    config_addr_key = g_new(int, 1);

    *config_addr_key = config_addr;

    g_hash_table_insert(phb->msi, config_addr_key, msi);

out:

    rtas_st(rets, 0, RTAS_OUT_SUCCESS);

    rtas_st(rets, 1, req_num);

    rtas_st(rets, 2, ++seq_num);

    if (nret > 3) {

        rtas_st(rets, 3, ret_intr_type);

    }

    trace_spapr_pci_rtas_ibm_change_msi(config_addr, func, req_num, irq);

}


static inline TCGv gen_extend(TCGv val, int opsize, int sign)

{

    TCGv tmp;

    switch (opsize) {

    case OS_BYTE:

        tmp = tcg_temp_new();

        if (sign)

            tcg_gen_ext8s_i32(tmp, val);

        else

            tcg_gen_ext8u_i32(tmp, val);

        break;

    case OS_WORD:

        tmp = tcg_temp_new();

        if (sign)

            tcg_gen_ext16s_i32(tmp, val);

        else

            tcg_gen_ext16u_i32(tmp, val);

        break;

    case OS_LONG:

    case OS_SINGLE:

        tmp = val;

        break;

    default:

        qemu_assert(0, "Bad operand size");

    }

    return tmp;

}


static void select_vgahw (const char *p)
{
    const char *opts;
    vga_interface_type = VGA_NONE;
    if (strstart(p, "std", &opts)) {
        if (vga_available()) {
            vga_interface_type = VGA_STD;
            fprintf(stderr, "Error: standard VGA not available\n");
            exit(0);
    } else if (strstart(p, "cirrus", &opts)) {
        if (cirrus_vga_available()) {
            vga_interface_type = VGA_CIRRUS;
            fprintf(stderr, "Error: Cirrus VGA not available\n");
            exit(0);
    } else if (strstart(p, "vmware", &opts)) {
        if (vmware_vga_available()) {
            vga_interface_type = VGA_VMWARE;
            fprintf(stderr, "Error: VMWare SVGA not available\n");
            exit(0);
    } else if (strstart(p, "xenfb", &opts)) {
        vga_interface_type = VGA_XENFB;
    } else if (strstart(p, "qxl", &opts)) {
        vga_interface_type = VGA_QXL;
    } else if (!strstart(p, "none", &opts)) {
    invalid_vga:
        fprintf(stderr, "Unknown vga type: %s\n", p);
        exit(1);
        const char *nextopt;
        if (strstart(opts, ",retrace=", &nextopt)) {
            if (strstart(opts, "dumb", &nextopt))
                vga_retrace_method = VGA_RETRACE_DUMB;
            else if (strstart(opts, "precise", &nextopt))
                vga_retrace_method = VGA_RETRACE_PRECISE;
            else goto invalid_vga;
        } else goto invalid_vga;


void kvm_irqchip_commit_routes(KVMState *s)
{
    int ret;
    s->irq_routes->flags = 0;
    trace_kvm_irqchip_commit_routes();
    ret = kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
    assert(ret == 0);


void cpu_exit(CPUState *cpu)

{

    cpu->exit_request = 1;

    /* Ensure cpu_exec will see the exit request after TCG has exited.  */

    smp_wmb();

    cpu->tcg_exit_req = 1;

}


void FUNCC(ff_h264_idct8_add)(uint8_t *_dst, DCTELEM *_block, int stride){

    int i;

    INIT_CLIP

    pixel *dst = (pixel*)_dst;

    dctcoef *block = (dctcoef*)_block;

    stride /= sizeof(pixel);

    block[0] += 32;

    for( i = 0; i < 8; i++ )

    {

        const int a0 =  block[i+0*8] + block[i+4*8];

        const int a2 =  block[i+0*8] - block[i+4*8];

        const int a4 = (block[i+2*8]>>1) - block[i+6*8];

        const int a6 = (block[i+6*8]>>1) + block[i+2*8];

        const int b0 = a0 + a6;

        const int b2 = a2 + a4;

        const int b4 = a2 - a4;

        const int b6 = a0 - a6;

        const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1);

        const int a3 =  block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1);

        const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1);

        const int a7 =  block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1);

        const int b1 = (a7>>2) + a1;

        const int b3 =  a3 + (a5>>2);

        const int b5 = (a3>>2) - a5;

        const int b7 =  a7 - (a1>>2);

        block[i+0*8] = b0 + b7;

        block[i+7*8] = b0 - b7;

        block[i+1*8] = b2 + b5;

        block[i+6*8] = b2 - b5;

        block[i+2*8] = b4 + b3;

        block[i+5*8] = b4 - b3;

        block[i+3*8] = b6 + b1;

        block[i+4*8] = b6 - b1;

    }

    for( i = 0; i < 8; i++ )

    {

        const int a0 =  block[0+i*8] + block[4+i*8];

        const int a2 =  block[0+i*8] - block[4+i*8];

        const int a4 = (block[2+i*8]>>1) - block[6+i*8];

        const int a6 = (block[6+i*8]>>1) + block[2+i*8];

        const int b0 = a0 + a6;

        const int b2 = a2 + a4;

        const int b4 = a2 - a4;

        const int b6 = a0 - a6;

        const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1);

        const int a3 =  block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1);

        const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1);

        const int a7 =  block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1);

        const int b1 = (a7>>2) + a1;

        const int b3 =  a3 + (a5>>2);

        const int b5 = (a3>>2) - a5;

        const int b7 =  a7 - (a1>>2);

        dst[i + 0*stride] = CLIP( dst[i + 0*stride] + ((b0 + b7) >> 6) );

        dst[i + 1*stride] = CLIP( dst[i + 1*stride] + ((b2 + b5) >> 6) );

        dst[i + 2*stride] = CLIP( dst[i + 2*stride] + ((b4 + b3) >> 6) );

        dst[i + 3*stride] = CLIP( dst[i + 3*stride] + ((b6 + b1) >> 6) );

        dst[i + 4*stride] = CLIP( dst[i + 4*stride] + ((b6 - b1) >> 6) );

        dst[i + 5*stride] = CLIP( dst[i + 5*stride] + ((b4 - b3) >> 6) );

        dst[i + 6*stride] = CLIP( dst[i + 6*stride] + ((b2 - b5) >> 6) );

        dst[i + 7*stride] = CLIP( dst[i + 7*stride] + ((b0 - b7) >> 6) );

    }

}


static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,

                      GetByteContext *gb, int nbits,

                      int rlc, int no, uint16_t *out)

{

    uint64_t c        = 0;

    uint16_t *outb    = out;

    uint16_t *oe      = out + no;

    const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size

    uint8_t cs, s;

    int i, lc = 0;

    while (gb->buffer < ie) {

        get_char(c, lc, gb);

        while (lc >= HUF_DECBITS) {

            const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];

            if (pl.len) {

                lc -= pl.len;

                get_code(pl.lit, rlc, c, lc, gb, out, oe);

            } else {

                int j;

                if (!pl.p)

                    return AVERROR_INVALIDDATA;

                for (j = 0; j < pl.lit; j++) {

                    int l = hcode[pl.p[j]] & 63;

                    while (lc < l && bytestream2_get_bytes_left(gb) > 0)

                        get_char(c, lc, gb);

                    if (lc >= l) {

                        if ((hcode[pl.p[j]] >> 6) ==

                            ((c >> (lc - l)) & ((1LL << l) - 1))) {

                            lc -= l;

                            get_code(pl.p[j], rlc, c, lc, gb, out, oe);

                            break;

                        }

                    }

                }

                if (j == pl.lit)

                    return AVERROR_INVALIDDATA;

            }

        }

    }

    i   = (8 - nbits) & 7;

    c >>= i;

    lc -= i;

    while (lc > 0) {

        const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];

        if (pl.len) {

            lc -= pl.len;

            get_code(pl.lit, rlc, c, lc, gb, out, oe);

        } else {

            return AVERROR_INVALIDDATA;

        }

    }

    if (out - outb != no)

        return AVERROR_INVALIDDATA;

    return 0;

}