java有一个divmod指令吗?

Qqw*_*qwy 6 java divmod

除了divmod作为许多芯片组的本地指令外,在以多个不同面额细分数字时,它也更容易上手

(例如毫秒 - >日期时间转换,或美分 - >硬币面额转换).

那么是否有一个divmod同时返回除法结果和余数的结果?

Mil*_*les 7

如果支持,HotSpot JIT 编译器将使用单个 divmod 操作替换针对相同参数的除法和求模操作。因此,虽然这可能无法解决可读性问题,但您无需担心性能。

来自 OpenJDK 9 源代码

  case Op_ModI:
    if (UseDivMod) {
      // Check if a%b and a/b both exist
      Node* d = n->find_similar(Op_DivI);
      if (d) {
        // Replace them with a fused divmod if supported
        if (Matcher::has_match_rule(Op_DivModI)) {
          DivModINode* divmod = DivModINode::make(n);
          d->subsume_by(divmod->div_proj(), this);
          n->subsume_by(divmod->mod_proj(), this);
        } else {
          // replace a%b with a-((a/b)*b)
          Node* mult = new MulINode(d, d->in(2));
          Node* sub  = new SubINode(d->in(1), mult);
          n->subsume_by(sub, this);
        }
      }
    }
    break;
Run Code Online (Sandbox Code Playgroud)

通过使用诊断选项打印生成的 JIT 指令,我能够看到在 C1 优化级别同时使用idiv和指令的方法在 C2 级别irem仅使用一条指令。idiv