1 /*
   2  * Copyright (c) 2016, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.asm.sparc.test;
  26 
  27 import static org.junit.Assert.assertEquals;
  28 import static org.junit.Assert.assertFalse;
  29 import static org.junit.Assert.assertTrue;
  30 
  31 import org.junit.Test;
  32 
  33 import org.graalvm.compiler.asm.sparc.SPARCAssembler.BitSpec;
  34 import org.graalvm.compiler.asm.sparc.SPARCAssembler.CompositeBitSpec;
  35 import org.graalvm.compiler.asm.sparc.SPARCAssembler.ContinousBitSpec;
  36 
  37 public class BitSpecTest {
  38 
  39     private static final BitSpec d4hi = new ContinousBitSpec(23, 20, true, "d4hi");
  40     private static final BitSpec d4lo = new ContinousBitSpec(7, 4, false, "d4lo");
  41     private static final BitSpec d8 = new CompositeBitSpec(d4hi, d4lo);
  42 
  43     @Test
  44     public void testContinousSignExtend() {
  45         testSetGet(d4hi, 0x00700000, 0x00000007);
  46         testSetGet(d4hi, 0x00800000, 0xFFFFFFF8);
  47     }
  48 
  49     @Test
  50     public void testContinousZeroExtend() {
  51         testSetGet(d4lo, 0x000000F0, 0x0000000F);
  52         testSetGet(d4lo, 0x00000070, 0x00000007);
  53     }
  54 
  55     public void testSetGet(BitSpec bs, int encoded, int decoded) {
  56         assertTrue(bs.valueFits(decoded));
  57         assertEquals(encoded, bs.setBits(0, decoded));
  58         assertEquals(decoded, bs.getBits(encoded));
  59     }
  60 
  61     @Test
  62     public void testContinousSignExtendValueFits() {
  63         assertFalse(d4hi.valueFits(0xf));
  64         assertFalse(d4hi.valueFits(0x10));
  65         assertFalse(d4hi.valueFits(0x17));
  66     }
  67 
  68     @Test
  69     public void testContinousZeroExtendValueFits() {
  70         assertFalse(d4lo.valueFits(0x10));
  71     }
  72 
  73     @Test(expected = AssertionError.class)
  74     public void testContinousSignExtendSetFail1() {
  75         d4hi.setBits(0, 0xf);
  76     }
  77 
  78     @Test(expected = AssertionError.class)
  79     public void testContinousSignExtendSetFail2() {
  80         d4hi.setBits(0, 0xFFFFFFF0);
  81     }
  82 
  83     @Test(expected = AssertionError.class)
  84     public void testContinousZeroExtendSetFail1() {
  85         d4lo.setBits(0, 0x10);
  86     }
  87 
  88     @Test
  89     public void testCompositeSignExtended() {
  90         testSetGet(d8, 0x00f000c0, 0xfffffffc);
  91         testSetGet(d8, 0x008000c0, 0xffffff8c);
  92         testSetGet(d8, 0x007000c0, 0x7c);
  93     }
  94 
  95     @Test(expected = AssertionError.class)
  96     public void testCompositeSignExtendedFail1() {
  97         d8.setBits(0, 0x00000080);
  98     }
  99 
 100     @Test(expected = AssertionError.class)
 101     public void testCompositeSignExtendedFail2() {
 102         d8.setBits(0, 0xEFFFFF80);
 103     }
 104 
 105     @Test
 106     public void testCompositeValueFits() {
 107         assertTrue(d8.valueFits(0xfffffffc));
 108         assertTrue(d8.valueFits(0xffffff8c));
 109         assertTrue(d8.valueFits(0x7c));
 110         assertFalse(d8.valueFits(0x8c));
 111         assertFalse(d8.valueFits(0xEFFFFF80));
 112     }
 113 }