1 /* 2 * Copyright (c) 1997, 2009, 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 */ 24 25 // Portions of code courtesy of Clifford Click 26 27 // Optimization - Graph Style 28 29 #include "incls/_precompiled.incl" 30 #include "incls/_divnode.cpp.incl" 31 #include <math.h> 32 33 //----------------------magic_int_divide_constants----------------------------- 34 // Compute magic multiplier and shift constant for converting a 32 bit divide 35 // by constant into a multiply/shift/add series. Return false if calculations 36 // fail. 37 // 38 // Borrowed almost verbatim from Hacker's Delight by Henry S. Warren, Jr. with 39 // minor type name and parameter changes. 40 static bool magic_int_divide_constants(jint d, jint &M, jint &s) { 41 int32_t p; 42 uint32_t ad, anc, delta, q1, r1, q2, r2, t; 43 const uint32_t two31 = 0x80000000L; // 2**31. 44 45 ad = ABS(d); 46 if (d == 0 || d == 1) return false; 47 t = two31 + ((uint32_t)d >> 31); 48 anc = t - 1 - t%ad; // Absolute value of nc. 49 p = 31; // Init. p. 50 q1 = two31/anc; // Init. q1 = 2**p/|nc|. | 1 /* 2 * Copyright (c) 1997, 2010, 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 */ 24 25 #include "precompiled.hpp" 26 #include "memory/allocation.inline.hpp" 27 #include "opto/addnode.hpp" 28 #include "opto/connode.hpp" 29 #include "opto/divnode.hpp" 30 #include "opto/machnode.hpp" 31 #include "opto/matcher.hpp" 32 #include "opto/mulnode.hpp" 33 #include "opto/phaseX.hpp" 34 #include "opto/subnode.hpp" 35 36 // Portions of code courtesy of Clifford Click 37 38 // Optimization - Graph Style 39 40 #include <math.h> 41 42 //----------------------magic_int_divide_constants----------------------------- 43 // Compute magic multiplier and shift constant for converting a 32 bit divide 44 // by constant into a multiply/shift/add series. Return false if calculations 45 // fail. 46 // 47 // Borrowed almost verbatim from Hacker's Delight by Henry S. Warren, Jr. with 48 // minor type name and parameter changes. 49 static bool magic_int_divide_constants(jint d, jint &M, jint &s) { 50 int32_t p; 51 uint32_t ad, anc, delta, q1, r1, q2, r2, t; 52 const uint32_t two31 = 0x80000000L; // 2**31. 53 54 ad = ABS(d); 55 if (d == 0 || d == 1) return false; 56 t = two31 + ((uint32_t)d >> 31); 57 anc = t - 1 - t%ad; // Absolute value of nc. 58 p = 31; // Init. p. 59 q1 = two31/anc; // Init. q1 = 2**p/|nc|. |