dup java,在Java方法中使用dup进行计算

I currently have a method that does the follow: given: value, offset, stride, it adds offset to value keeping it within a group of size stride.

Examples:

20, 5, 30 => 25

29, 5, 30 => 4

42, 5, 30 => 47

And my current code is:

int cycle(int value, int offset, int stride) {

final int rem = value % stride;

return ((rem + offset) % stride - rem + value);

}

Which compiles to the following:

int cycle(int, int, int);

Code:

0: iload_1

1: iload_3

2: irem

3: istore 4

5: iload 4

7: iload_2

8: iadd

9: iload_3

10: irem

11: iload 4

13: isub

14: iload_1

15: iadd

16: ireturn

Is there any combination of code changes and / or compiler options that can make it produce something like this instead? (The example was written by hand):

int cycle(int, int, int);

Code:

0: iload_1

1: iload_3

2: irem

3: dup

4: iload_2

5: iadd

6: iload_3

7: irem

8: isub

9: iload_1

10: iadd

11: ireturn

解决方案

Most optimizing JVMs, most notably the commonly using HotSpot JVM, will transform the code into the SSA form before applying any other optimization. For this representation, it is entirely irrelevant whether the original code used temporary local variables or dup on the operand stack, the in­ter­me­di­ate representation will be the same.

So javac doesn’t offer any option to control the byte code representation of such a construct, but it doesn’t matter anyway.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值