1 /*
   2  * Copyright (c) 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.hotspot;
  24 
  25 /**
  26  * A compact representation of the different encoding strategies for Objects and metadata.
  27  */
  28 public class CompressEncoding {
  29     public final long base;
  30     public final int shift;
  31     public final int alignment;
  32 
  33     CompressEncoding(long base, int shift, int alignment) {
  34         this.base = base;
  35         this.shift = shift;
  36         this.alignment = alignment;
  37     }
  38 
  39     public int compress(long ptr) {
  40         if (ptr == 0L) {
  41             return 0;
  42         } else {
  43             return (int) ((ptr - base) >>> shift);
  44         }
  45     }
  46 
  47     public long uncompress(int ptr) {
  48         if (ptr == 0) {
  49             return 0L;
  50         } else {
  51             return ((ptr & 0xFFFFFFFFL) << shift) + base;
  52         }
  53     }
  54 
  55     @Override
  56     public String toString() {
  57         return "base: " + base + " shift: " + shift + " alignment: " + alignment;
  58     }
  59 
  60     @Override
  61     public int hashCode() {
  62         final int prime = 31;
  63         int result = 1;
  64         result = prime * result + alignment;
  65         result = prime * result + (int) (base ^ (base >>> 32));
  66         result = prime * result + shift;
  67         return result;
  68     }
  69 
  70     @Override
  71     public boolean equals(Object obj) {
  72         if (obj instanceof CompressEncoding) {
  73             CompressEncoding other = (CompressEncoding) obj;
  74             return alignment == other.alignment && base == other.base && shift == other.shift;
  75         } else {
  76             return false;
  77         }
  78     }
  79 }