Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/cpu/aarch64/vm/immediate_aarch64.cpp
32285 views
/*1* Copyright (c) 2013, Red Hat Inc.2* 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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*/2021#include <stdlib.h>22#include "immediate_aarch64.hpp"2324// there are at most 2^13 possible logical immediate encodings25// however, some combinations of immr and imms are invalid26static const unsigned LI_TABLE_SIZE = (1 << 13);2728static int li_table_entry_count;2930// for forward lookup we just use a direct array lookup31// and assume that the cient has supplied a valid encoding32// table[encoding] = immediate33static u_int64_t LITable[LI_TABLE_SIZE];3435// for reverse lookup we need a sparse map so we store a table of36// immediate and encoding pairs sorted by immediate value3738struct li_pair {39u_int64_t immediate;40u_int32_t encoding;41};4243static struct li_pair InverseLITable[LI_TABLE_SIZE];4445// comparator to sort entries in the inverse table46int compare_immediate_pair(const void *i1, const void *i2)47{48struct li_pair *li1 = (struct li_pair *)i1;49struct li_pair *li2 = (struct li_pair *)i2;50if (li1->immediate < li2->immediate) {51return -1;52}53if (li1->immediate > li2->immediate) {54return 1;55}56return 0;57}5859// helper functions used by expandLogicalImmediate6061// for i = 1, ... N result<i-1> = 1 other bits are zero62static inline u_int64_t ones(int N)63{64return (N == 64 ? (u_int64_t)-1UL : ((1UL << N) - 1));65}6667/*68* bit twiddling helpers for instruction decode69*/7071// 32 bit mask with bits [hi,...,lo] set72static inline u_int32_t mask32(int hi = 31, int lo = 0)73{74int nbits = (hi + 1) - lo;75return ((1 << nbits) - 1) << lo;76}7778static inline u_int64_t mask64(int hi = 63, int lo = 0)79{80int nbits = (hi + 1) - lo;81return ((1L << nbits) - 1) << lo;82}8384// pick bits [hi,...,lo] from val85static inline u_int32_t pick32(u_int32_t val, int hi = 31, int lo = 0)86{87return (val & mask32(hi, lo));88}8990// pick bits [hi,...,lo] from val91static inline u_int64_t pick64(u_int64_t val, int hi = 31, int lo = 0)92{93return (val & mask64(hi, lo));94}9596// mask [hi,lo] and shift down to start at bit 097static inline u_int32_t pickbits32(u_int32_t val, int hi = 31, int lo = 0)98{99return (pick32(val, hi, lo) >> lo);100}101102// mask [hi,lo] and shift down to start at bit 0103static inline u_int64_t pickbits64(u_int64_t val, int hi = 63, int lo = 0)104{105return (pick64(val, hi, lo) >> lo);106}107108// result<0> to val<N>109static inline u_int64_t pickbit(u_int64_t val, int N)110{111return pickbits64(val, N, N);112}113114static inline u_int32_t uimm(u_int32_t val, int hi, int lo)115{116return pickbits32(val, hi, lo);117}118119// SPEC bits(M*N) Replicate(bits(M) x, integer N);120// this is just an educated guess121122u_int64_t replicate(u_int64_t bits, int nbits, int count)123{124u_int64_t result = 0;125// nbits may be 64 in which case we want mask to be -1126u_int64_t mask = ones(nbits);127for (int i = 0; i < count ; i++) {128result <<= nbits;129result |= (bits & mask);130}131return result;132}133134// this function writes the supplied bimm reference and returns a135// boolean to indicate success (1) or fail (0) because an illegal136// encoding must be treated as an UNALLOC instruction137138// construct a 32 bit immediate value for a logical immediate operation139int expandLogicalImmediate(u_int32_t immN, u_int32_t immr,140u_int32_t imms, u_int64_t &bimm)141{142int len; // ought to be <= 6143u_int32_t levels; // 6 bits144u_int32_t tmask_and; // 6 bits145u_int32_t wmask_and; // 6 bits146u_int32_t tmask_or; // 6 bits147u_int32_t wmask_or; // 6 bits148u_int64_t imm64; // 64 bits149u_int64_t tmask, wmask; // 64 bits150u_int32_t S, R, diff; // 6 bits?151152if (immN == 1) {153len = 6; // looks like 7 given the spec above but this cannot be!154} else {155len = 0;156u_int32_t val = (~imms & 0x3f);157for (int i = 5; i > 0; i--) {158if (val & (1 << i)) {159len = i;160break;161}162}163if (len < 1) {164return 0;165}166// for valid inputs leading 1s in immr must be less than leading167// zeros in imms168int len2 = 0; // ought to be < len169u_int32_t val2 = (~immr & 0x3f);170for (int i = 5; i > 0; i--) {171if (!(val2 & (1 << i))) {172len2 = i;173break;174}175}176if (len2 >= len) {177return 0;178}179}180181levels = (1 << len) - 1;182183if ((imms & levels) == levels) {184return 0;185}186187S = imms & levels;188R = immr & levels;189190// 6 bit arithmetic!191diff = S - R;192tmask_and = (diff | ~levels) & 0x3f;193tmask_or = (diff & levels) & 0x3f;194tmask = 0xffffffffffffffffULL;195196for (int i = 0; i < 6; i++) {197int nbits = 1 << i;198u_int64_t and_bit = pickbit(tmask_and, i);199u_int64_t or_bit = pickbit(tmask_or, i);200u_int64_t and_bits_sub = replicate(and_bit, 1, nbits);201u_int64_t or_bits_sub = replicate(or_bit, 1, nbits);202u_int64_t and_bits_top = (and_bits_sub << nbits) | ones(nbits);203u_int64_t or_bits_top = (0 << nbits) | or_bits_sub;204205tmask = ((tmask206& (replicate(and_bits_top, 2 * nbits, 32 / nbits)))207| replicate(or_bits_top, 2 * nbits, 32 / nbits));208}209210wmask_and = (immr | ~levels) & 0x3f;211wmask_or = (immr & levels) & 0x3f;212213wmask = 0;214215for (int i = 0; i < 6; i++) {216int nbits = 1 << i;217u_int64_t and_bit = pickbit(wmask_and, i);218u_int64_t or_bit = pickbit(wmask_or, i);219u_int64_t and_bits_sub = replicate(and_bit, 1, nbits);220u_int64_t or_bits_sub = replicate(or_bit, 1, nbits);221u_int64_t and_bits_top = (ones(nbits) << nbits) | and_bits_sub;222u_int64_t or_bits_top = (or_bits_sub << nbits) | 0;223224wmask = ((wmask225& (replicate(and_bits_top, 2 * nbits, 32 / nbits)))226| replicate(or_bits_top, 2 * nbits, 32 / nbits));227}228229if (diff & (1U << 6)) {230imm64 = tmask & wmask;231} else {232imm64 = tmask | wmask;233}234235236bimm = imm64;237return 1;238}239240// constructor to initialise the lookup tables241242static void initLITables() __attribute__ ((constructor));243static void initLITables()244{245li_table_entry_count = 0;246for (unsigned index = 0; index < LI_TABLE_SIZE; index++) {247u_int32_t N = uimm(index, 12, 12);248u_int32_t immr = uimm(index, 11, 6);249u_int32_t imms = uimm(index, 5, 0);250if (expandLogicalImmediate(N, immr, imms, LITable[index])) {251InverseLITable[li_table_entry_count].immediate = LITable[index];252InverseLITable[li_table_entry_count].encoding = index;253li_table_entry_count++;254}255}256// now sort the inverse table257qsort(InverseLITable, li_table_entry_count,258sizeof(InverseLITable[0]), compare_immediate_pair);259}260261// public APIs provided for logical immediate lookup and reverse lookup262263u_int64_t logical_immediate_for_encoding(u_int32_t encoding)264{265return LITable[encoding];266}267268u_int32_t encoding_for_logical_immediate(u_int64_t immediate)269{270struct li_pair pair;271struct li_pair *result;272273pair.immediate = immediate;274275result = (struct li_pair *)276bsearch(&pair, InverseLITable, li_table_entry_count,277sizeof(InverseLITable[0]), compare_immediate_pair);278279if (result) {280return result->encoding;281}282283return 0xffffffff;284}285286// floating point immediates are encoded in 8 bits287// fpimm[7] = sign bit288// fpimm[6:4] = signed exponent289// fpimm[3:0] = fraction (assuming leading 1)290// i.e. F = s * 1.f * 2^(e - b)291292u_int64_t fp_immediate_for_encoding(u_int32_t imm8, int is_dp)293{294union {295float fpval;296double dpval;297u_int64_t val;298};299300u_int32_t s, e, f;301s = (imm8 >> 7 ) & 0x1;302e = (imm8 >> 4) & 0x7;303f = imm8 & 0xf;304// the fp value is s * n/16 * 2r where n is 16+e305fpval = (16.0 + f) / 16.0;306// n.b. exponent is signed307if (e < 4) {308int epos = e;309for (int i = 0; i <= epos; i++) {310fpval *= 2.0;311}312} else {313int eneg = 7 - e;314for (int i = 0; i < eneg; i++) {315fpval /= 2.0;316}317}318319if (s) {320fpval = -fpval;321}322if (is_dp) {323dpval = (double)fpval;324}325return val;326}327328u_int32_t encoding_for_fp_immediate(float immediate)329{330// given a float which is of the form331//332// s * n/16 * 2r333//334// where n is 16+f and imm1:s, imm4:f, simm3:r335// return the imm8 result [s:r:f]336//337338union {339float fpval;340u_int32_t val;341};342fpval = immediate;343u_int32_t s, r, f, res;344// sign bit is 31345s = (val >> 31) & 0x1;346// exponent is bits 30-23 but we only want the bottom 3 bits347// strictly we ought to check that the bits bits 30-25 are348// either all 1s or all 0s349r = (val >> 23) & 0x7;350// fraction is bits 22-0351f = (val >> 19) & 0xf;352res = (s << 7) | (r << 4) | f;353return res;354}355356357