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 package org.graalvm.compiler.asm.sparc.test;
  24 
  25 import static org.junit.Assert.assertEquals;
  26 import static org.junit.Assert.assertFalse;
  27 import static org.junit.Assert.assertTrue;
  28 
  29 import org.junit.Test;
  30 
  31 import org.graalvm.compiler.asm.sparc.SPARCAssembler.BitSpec;
  32 import org.graalvm.compiler.asm.sparc.SPARCAssembler.CompositeBitSpec;
  33 import org.graalvm.compiler.asm.sparc.SPARCAssembler.ContinousBitSpec;
  34 
  35 public class BitSpecTest {
  36 
  37     private static final BitSpec d4hi = new ContinousBitSpec(23, 20, true, "d4hi");
  38     private static final BitSpec d4lo = new ContinousBitSpec(7, 4, false, "d4lo");
  39     private static final BitSpec d8 = new CompositeBitSpec(d4hi, d4lo);
  40 
  41     @Test
  42     public void testContinousSignExtend() {
  43         testSetGet(d4hi, 0x00700000, 0x00000007);
  44         testSetGet(d4hi, 0x00800000, 0xFFFFFFF8);
  45     }
  46 
  47     @Test
  48     public void testContinousZeroExtend() {
  49         testSetGet(d4lo, 0x000000F0, 0x0000000F);
  50         testSetGet(d4lo, 0x00000070, 0x00000007);
  51     }
  52 
  53     public void testSetGet(BitSpec bs, int encoded, int decoded) {
  54         assertTrue(bs.valueFits(decoded));
  55         assertEquals(encoded, bs.setBits(0, decoded));
  56         assertEquals(decoded, bs.getBits(encoded));
  57     }
  58 
  59     @Test
  60     public void testContinousSignExtendValueFits() {
  61         assertFalse(d4hi.valueFits(0xf));
  62         assertFalse(d4hi.valueFits(0x10));
  63         assertFalse(d4hi.valueFits(0x17));
  64     }
  65 
  66     @Test
  67     public void testContinousZeroExtendValueFits() {
  68         assertFalse(d4lo.valueFits(0x10));
  69     }
  70 
  71     @Test(expected = AssertionError.class)
  72     public void testContinousSignExtendSetFail1() {
  73         d4hi.setBits(0, 0xf);
  74     }
  75 
  76     @Test(expected = AssertionError.class)
  77     public void testContinousSignExtendSetFail2() {
  78         d4hi.setBits(0, 0xFFFFFFF0);
  79     }
  80 
  81     @Test(expected = AssertionError.class)
  82     public void testContinousZeroExtendSetFail1() {
  83         d4lo.setBits(0, 0x10);
  84     }
  85 
  86     @Test
  87     public void testCompositeSignExtended() {
  88         testSetGet(d8, 0x00f000c0, 0xfffffffc);
  89         testSetGet(d8, 0x008000c0, 0xffffff8c);
  90         testSetGet(d8, 0x007000c0, 0x7c);
  91     }
  92 
  93     @Test(expected = AssertionError.class)
  94     public void testCompositeSignExtendedFail1() {
  95         d8.setBits(0, 0x00000080);
  96     }
  97 
  98     @Test(expected = AssertionError.class)
  99     public void testCompositeSignExtendedFail2() {
 100         d8.setBits(0, 0xEFFFFF80);
 101     }
 102 
 103     @Test
 104     public void testCompositeValueFits() {
 105         assertTrue(d8.valueFits(0xfffffffc));
 106         assertTrue(d8.valueFits(0xffffff8c));
 107         assertTrue(d8.valueFits(0x7c));
 108         assertFalse(d8.valueFits(0x8c));
 109         assertFalse(d8.valueFits(0xEFFFFF80));
 110     }
 111 }