Jason Winnebeck created GROOVY-7659:
---------------------------------------

             Summary: primopts for CompileStatic integer division
                 Key: GROOVY-7659
                 URL: https://issues.apache.org/jira/browse/GROOVY-7659
             Project: Groovy
          Issue Type: Improvement
          Components: primtive opts, Static compilation
    Affects Versions: 2.4.5
            Reporter: Jason Winnebeck
            Assignee: Cédric Champeau
            Priority: Minor


Since operations a+b with int can be supported with primitive operations, it 
would be nice if division could as well. I think this is possible to do in 
certain circumstances and still adhere to the semantics that int / int results 
in BigDecimal. I believe if you have (int)(int / int), I can't think of a case 
where a primitive division would give a different result than with BigDecimal.

{code}
@groovy.transform.CompileStatic
int idiv_static(int a) {
  (int)(a / 2)
}

int idiv_dynamic(int a) {
  (int)(a / 2)
}

@groovy.transform.CompileStatic
int iplus_static(int a) {
  a + 5
}

int iplus_dynamic(int a) {
  a + 5
}
{code}

We can see the bytecode for iplus_static generated by Groovy 2.4.5:
{code}
ILOAD 1
ICONST_5
IADD
IRETURN
{code}

But for idiv_static, it converts the ints to Integer, calls a method that 
results in BigDecimal division and then converts that to int.
{code}
    ILOAD 1
    INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
    ICONST_2
    INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
    INVOKESTATIC org/codehaus/groovy/runtime/dgmimpl/NumberNumberDiv.div 
(Ljava/lang/Number;Ljava/lang/Number;)Ljava/lang/Number;
    INVOKESTATIC 
org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.intUnbox 
(Ljava/lang/Object;)I
    IRETURN
{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to