001/*
002 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.replacements;
024
025import static com.oracle.graal.hotspot.HotSpotGraalRuntime.*;
026import static com.oracle.graal.hotspot.replacements.HotSpotReplacementsUtil.*;
027
028import java.util.zip.*;
029
030import jdk.internal.jvmci.meta.*;
031
032import com.oracle.graal.api.replacements.*;
033import com.oracle.graal.compiler.common.spi.*;
034import com.oracle.graal.graph.Node.ConstantNodeParameter;
035import com.oracle.graal.graph.Node.NodeIntrinsic;
036import com.oracle.graal.hotspot.nodes.*;
037import com.oracle.graal.nodes.extended.*;
038import com.oracle.graal.word.*;
039
040// JaCoCo Exclude
041
042/**
043 * Substitutions for {@link CRC32}.
044 */
045public class CRC32Substitutions {
046
047    /**
048     * Gets the address of {@code StubRoutines::x86::_crc_table} in {@code stubRoutines_x86.hpp}.
049     */
050    @Fold
051    private static long crcTableAddress() {
052        return runtime().getConfig().crcTableAddress;
053    }
054
055    static int update(int crc, int b) {
056        int c = ~crc;
057        int index = (b ^ c) & 0xFF;
058        int offset = index << 2;
059        int result = Word.unsigned(crcTableAddress()).readInt(offset);
060        result = result ^ (c >>> 8);
061        return ~result;
062    }
063
064    static int updateBytes(int crc, byte[] buf, int off, int len) {
065        Word bufAddr = Word.unsigned(ComputeObjectAddressNode.get(buf, arrayBaseOffset(Kind.Byte) + off));
066        return updateBytes(UPDATE_BYTES_CRC32, crc, bufAddr, len);
067    }
068
069    static int updateByteBuffer(int crc, long addr, int off, int len) {
070        Word bufAddr = Word.unsigned(addr).add(off);
071        return updateBytes(UPDATE_BYTES_CRC32, crc, bufAddr, len);
072    }
073
074    public static final ForeignCallDescriptor UPDATE_BYTES_CRC32 = new ForeignCallDescriptor("updateBytesCRC32", int.class, int.class, Word.class, int.class);
075
076    @NodeIntrinsic(ForeignCallNode.class)
077    public static native int updateBytes(@ConstantNodeParameter ForeignCallDescriptor descriptor, int crc, Word buf, int length);
078}