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