1 /* 2 * Copyright (c) 2002, 2025, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2012, 2019 SAP SE. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "asm/macroAssembler.inline.hpp" 27 #include "runtime/os.hpp" 28 #include "runtime/stubRoutines.hpp" 29 #include "runtime/vm_version.hpp" 30 #include "utilities/byteswap.hpp" 31 32 // Implementation of the platform-specific part of StubRoutines - for 33 // a description of how to extend it, see the stubRoutines.hpp file. 34 35 36 #define __ masm-> 37 38 // CRC constant compute functions 39 static juint fold_byte(juint w, juint reverse_poly) { 40 for (int i = 0; i < 8; i++) { 41 int poly_if_odd = (-(w & 1)) & reverse_poly; 42 w = (w >> 1) ^ poly_if_odd; 43 } 44 return w; 45 } 46 47 static juint fold_word(juint w, juint reverse_poly) { 48 for (int i = 0; i < 32; i++) { 49 int poly_if_odd = (-(w & 1)) & reverse_poly; 50 w = (w >> 1) ^ poly_if_odd; 51 } 52 return w; 53 } 54 55 static julong numberOfLeadingZeros(julong p) { 56 julong l = 1ull << 63; 57 for (int i = 0; i < 64; ++i) { 58 if (p & l) return i; 59 l >>= 1; 60 } 61 return 64; 62 } 63 64 static julong compute_inverse_poly(julong long_poly) { 65 // 2^64 / p 66 julong mod = 0, div = 0; 67 int d = numberOfLeadingZeros(long_poly); 68 int s = d + 1; 69 do { 70 mod ^= (long_poly << s); 71 div |= (1L << s); 72 s = d - numberOfLeadingZeros(mod); 73 } while (s >= 0); 74 return div; 75 } 76 77 // Constants to fold n words as needed by macroAssembler. 78 address StubRoutines::ppc::generate_crc_constants(juint reverse_poly) { 79 // Layout of constant table: 80 // >= Power8: 1 table for single byte folding + constants for fast vector implementation 81 const int vector_size = 16 * (CRC32_UNROLL_FACTOR2 + CRC32_UNROLL_FACTOR / CRC32_UNROLL_FACTOR2); 82 83 const int size = CRC32_TABLE_SIZE + vector_size; 84 const address consts = (address)os::malloc(size, mtInternal); 85 if (consts == nullptr) { 86 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "CRC constants: no enough space"); 87 } 88 juint* ptr = (juint*)consts; 89 90 // Simple table used for single byte folding 91 for (int i = 0; i < 256; ++i) { 92 ptr[i] = fold_byte(i, reverse_poly); 93 } 94 95 // >= Power8: vector constants 96 juint* ptr1 = (juint*)(consts + CRC32_TABLE_SIZE); 97 guarantee(((intptr_t)ptr1 & 0xF) == 0, "16-byte alignment needed"); 98 99 // Generate constants for outer loop 100 juint v0, v1, v2, v3 = 1; 101 for (int i = 0; i < CRC32_UNROLL_FACTOR2 - 1; ++i) { 102 v0 = fold_word(v3, reverse_poly); 103 v1 = fold_word(v0, reverse_poly); 104 v2 = fold_word(v1, reverse_poly); 105 v3 = fold_word(v2, reverse_poly); 106 #ifdef VM_LITTLE_ENDIAN 107 ptr1[4*i ] = v3; 108 ptr1[4*i+1] = v2; 109 ptr1[4*i+2] = v3; 110 ptr1[4*i+3] = v2; 111 #else 112 ptr1[4*i ] = v2; 113 ptr1[4*i+1] = v3; 114 ptr1[4*i+2] = v2; 115 ptr1[4*i+3] = v3; 116 #endif 117 } 118 119 // Generate constants for inner loop 120 juint* ptr2 = ptr1 + 4 * (CRC32_UNROLL_FACTOR2 - 1); 121 v3 = 1; // Restart from scratch. 122 for (int i = 0; i < CRC32_UNROLL_FACTOR; ++i) { 123 v0 = fold_word(v3, reverse_poly); 124 v1 = fold_word(v0, reverse_poly); 125 v2 = fold_word(v1, reverse_poly); 126 v3 = fold_word(v2, reverse_poly); 127 if (i % CRC32_UNROLL_FACTOR2 == 0) { 128 int idx = CRC32_UNROLL_FACTOR / CRC32_UNROLL_FACTOR2 - 1 - i / CRC32_UNROLL_FACTOR2; 129 for (int j = 0; j < 4; ++j) { 130 #ifdef VM_LITTLE_ENDIAN 131 ptr2[4*idx ] = v3; 132 ptr2[4*idx+1] = v2; 133 ptr2[4*idx+2] = v1; 134 ptr2[4*idx+3] = v0; 135 #else 136 ptr2[4*idx ] = v0; 137 ptr2[4*idx+1] = v1; 138 ptr2[4*idx+2] = v2; 139 ptr2[4*idx+3] = v3; 140 #endif 141 } 142 } 143 } 144 145 // Constants to reduce 64 to 32 bit as needed by macroAssembler. 146 juint* ptr3 = ptr2 + 4 * (CRC32_UNROLL_FACTOR / CRC32_UNROLL_FACTOR2); 147 julong* c = (julong*)ptr3; 148 julong long_poly = (((julong)reverse_poly) << 1) | 1; 149 julong inverse_long_poly = compute_inverse_poly(long_poly); 150 #ifdef VM_LITTLE_ENDIAN 151 c[0] = inverse_long_poly; 152 c[1] = long_poly; 153 #else 154 c[0] = long_poly; 155 c[1] = inverse_long_poly; 156 #endif 157 158 #ifdef ASSERT 159 if (reverse_poly == REVERSE_CRC32_POLY) { 160 assert(INVERSE_REVERSE_CRC32_POLY == inverse_long_poly, "sanity"); 161 } else if (reverse_poly == REVERSE_CRC32C_POLY) { 162 assert(INVERSE_REVERSE_CRC32C_POLY == inverse_long_poly, "sanity"); 163 } 164 #endif 165 166 //printf("inv poly: 0x%016llx\n", (long long unsigned int)inverse_long_poly); 167 168 return consts; 169 }