wasm中的select和vs if if else

时间:2019-06-07 16:04:09

标签: if-statement select webassembly

wasmidxmax()if之间的区别是什么?这些命令的最佳应用是什么?

wat2wasm中的示例:

WAT代码:

select

JS代码:

(module
  (func (export "Select") (param i32) (result i32)
    (select (i32.const 3)
            (i32.const 5)
            (local.get 0) ))

  (func (export "If") (param i32) (result i32)
    (if (result i32) (local.get 0)
        (then (i32.const  7))
        (else (i32.const 11)) )) )

根据文档:

const wasmInstance = new WebAssembly.Instance(wasmModule, {}); const { Select, If } = wasmInstance.exports; console.log(Select(1)); // => 3 console.log(Select(0)); // => 5 console.log(If(1)); // => 7 console.log(If(0)); // => 11 运算符根据其第三个操作数是否为零来选择其前两个操作数之一。

selectblockloop指令是结构化指令。它们将嵌套的指令序列(称为块)括起来,以末端或其他伪指令终止或分隔。正如语法规定的那样,它们必须嵌套得很好。结构化指令可以产生带注释的结果类型所描述的值。

但是,if运算符可以包含select并执行许多指令。

block

2 个答案:

答案 0 :(得分:1)

Select从堆栈中获取所有三个操作数,而如果仅从堆栈中获取决策,则有条件地执行两个块之一。在您的示例中,如何编写s表达式隐藏了这一事实。 Select将选定的值放回堆栈,而如果内部块可能具有返回值,但也可能为空。

答案 1 :(得分:0)

public class PdfPostAdapter extends RecyclerView.Adapter<PdfPostAdapter.postViewHolder> { private List<String>postTitle; private List<String>next; Context context; public PdfPostAdapter(List<String> postTitle , List<String> next , Context context) //creation of constructor { this.postTitle = postTitle; this.next = next; this.context = context; } // implementing methodes fo postAdapter . RecyclerView @NonNull @Override public postViewHolder onCreateViewHolder( ViewGroup viewGroup, int i) { View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.post, viewGroup ,false); return new postViewHolder(itemView); } @Override public void onBindViewHolder(postViewHolder postViewHolder, final int i) { postViewHolder.tvTitle.setText(postTitle.get(i)); postViewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {//need to do some changes here to pass url data to next activity if (next.get(i).endsWith(".pdf")) { Intent intent = new Intent(context,DetailedView.class); intent.putExtra("link", next.get(i)); //need to do some changes here to pass url data to next activity intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } else if (next.get(i).endsWith(".PDF")){ Intent intent = new Intent(context,DetailedView.class); intent.putExtra("link", next.get(i)); //need to do some changes here to pass url data to next activity intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } else { Intent intent = new Intent(context,OpenDetail.class); intent.putExtra("link", next.get(i)); //need to do some changes here to pass url data to next activity intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } } }); } @Override public int getItemCount() { return postTitle.size(); } public class postViewHolder extends RecyclerView.ViewHolder{ //creation of view holder TextView tvTitle; public postViewHolder( View itemView) { super(itemView); tvTitle = itemView.findViewById(R.id.title); } } } 指令很严格,即,它始终对所有操作数求值,而select仅执行其中一个分支。因此,如果操作数很简单(例如,只是值),则if是更有效的选择,因为它不需要执行任何分支。它直接编译为单个硬件指令。