From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 39893 invoked by alias); 22 Feb 2016 18:13:57 -0000 Mailing-List: contact java-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: java-patches-owner@gcc.gnu.org Received: (qmail 39874 invoked by uid 89); 22 Feb 2016 18:13:56 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=4.2 required=5.0 tests=AWL,BAYES_50,RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=fmod, H*Ad:U*java-patches, Annotate, 233190 X-Spam-User: qpsmtpd, 2 recipients X-HELO: server.nextmovesoftware.com Received: from server.nextmovesoftware.com (HELO server.nextmovesoftware.com) (162.254.253.69) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 22 Feb 2016 18:13:55 +0000 Received: from host109-147-72-25.range109-147.btcentralplus.com ([109.147.72.25]:61163 helo=macbookpro.home) by server.nextmovesoftware.com with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.85) (envelope-from ) id 1aXuzZ-00061H-0E; Mon, 22 Feb 2016 13:13:53 -0500 From: "roger@nextmovesoftware.com" Content-Type: multipart/mixed; boundary="Apple-Mail=_800444BF-D226-4945-990C-3CDC46EA7936" Subject: [JAVA PATCH] Builtin support for popcount* and bswap* functions Message-Id: Date: Mon, 22 Feb 2016 18:13:00 -0000 To: java-patches@gcc.gnu.org, gcc-patches@gcc.gnu.org Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-SW-Source: 2016-q1/txt/msg00016.txt.bz2 --Apple-Mail=_800444BF-D226-4945-990C-3CDC46EA7936 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Content-length: 1663 The following patch provides builtin support for byte swapping and bit coun= ting. On suitable hardware, these generate the x86 popcount instructions, as also generated by the SUN HotSpot JIT/JVM for these method calls. java.lang.Integer.bitCount -> __builtin_popcount java.lang.Long.bitCount -> __builtin_popcountl java.lang.Short.reverseBytes -> __builtin_bswap16 java.lang.Integer.reverseBytes -> __builtin_bswap32 java.lang.Long.reverseBytes -> __builtin_bswap64 New test cases have been added to libjava to double check nothing breaks. Whist I was there I noticed that the math builtins (many of which I added s= upport for back in 2003/2004) weren't marked as ECF_LEAF, indicating that they don't/c= an't invoke any user specified functions. This has also been fixed in the patc= h below. The following patch has been tested on x86_64-pc-linux-gnu with a full make= bootstrap and make check with no new failures/regressions. Ok for stage1 once it reopens? Cheers, Roger -- Roger Sayle, Ph.D. CEO and founder NextMove Software Limited Registered in England No. 07588305 Registered Office: Innovation Centre (Unit 23), Cambridge Science Park, Cam= bridge CB4 0EY 2016-02-21 Roger Sayle gcc/java: * builtins.c (java_builtins): Use popcount* and bswap* builtins to implement bitCount() and reverseBytes() methods in java.lang.Integer and friends. (initialize_builtins): Annotate math builtins with ECF_LEAF. Call define_builtin for the new popcount* and bswap* builtins. libjava: * testsuite/libjava.lang/BuiltinBitCount.java: New test case. * testsuite/libjava.lang/BuiltinReverseBytes.java: Likewise. =20 --Apple-Mail=_800444BF-D226-4945-990C-3CDC46EA7936 Content-Disposition: attachment; filename=patch7b.txt Content-Type: text/plain; name="patch7b.txt" Content-Transfer-Encoding: 7bit Content-length: 4959 Index: builtins.c =================================================================== --- builtins.c (revision 233190) +++ builtins.c (working copy) @@ -98,6 +98,11 @@ { { "java.lang.Math" }, { "sin" }, NULL, BUILT_IN_SIN }, { { "java.lang.Math" }, { "sqrt" }, NULL, BUILT_IN_SQRT }, { { "java.lang.Math" }, { "tan" }, NULL, BUILT_IN_TAN }, + { { "java.lang.Integer" }, { "bitCount" }, NULL, BUILT_IN_POPCOUNT }, + { { "java.lang.Integer" }, { "reverseBytes" }, NULL, BUILT_IN_BSWAP32 }, + { { "java.lang.Long" }, { "bitCount" }, NULL, BUILT_IN_POPCOUNTL }, + { { "java.lang.Long" }, { "reverseBytes" }, NULL, BUILT_IN_BSWAP64 }, + { { "java.lang.Short" }, { "reverseBytes" }, NULL, BUILT_IN_BSWAP16 }, { { "java.lang.Float" }, { "intBitsToFloat" }, convert_real, (enum built_in_function) 0 }, { { "java.lang.Double" }, { "longBitsToDouble" }, convert_real, @@ -483,6 +488,7 @@ tree double_ftype_double, double_ftype_double_double; tree float_ftype_float_float; tree boolean_ftype_boolean_boolean; + tree int_ftype_int; int i; for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i) @@ -507,50 +513,77 @@ double_type_node, double_type_node, NULL_TREE); define_builtin (BUILT_IN_FMOD, "__builtin_fmod", - double_ftype_double_double, "fmod", ECF_CONST); + double_ftype_double_double, "fmod", ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_FMODF, "__builtin_fmodf", - float_ftype_float_float, "fmodf", ECF_CONST); + float_ftype_float_float, "fmodf", ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_ACOS, "__builtin_acos", double_ftype_double, "_ZN4java4lang4Math4acosEJdd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_ASIN, "__builtin_asin", double_ftype_double, "_ZN4java4lang4Math4asinEJdd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_ATAN, "__builtin_atan", double_ftype_double, "_ZN4java4lang4Math4atanEJdd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_ATAN2, "__builtin_atan2", double_ftype_double_double, "_ZN4java4lang4Math5atan2EJddd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_CEIL, "__builtin_ceil", double_ftype_double, "_ZN4java4lang4Math4ceilEJdd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_COS, "__builtin_cos", double_ftype_double, "_ZN4java4lang4Math3cosEJdd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_EXP, "__builtin_exp", double_ftype_double, "_ZN4java4lang4Math3expEJdd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_FLOOR, "__builtin_floor", double_ftype_double, "_ZN4java4lang4Math5floorEJdd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_LOG, "__builtin_log", double_ftype_double, "_ZN4java4lang4Math3logEJdd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_POW, "__builtin_pow", double_ftype_double_double, "_ZN4java4lang4Math3powEJddd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_SIN, "__builtin_sin", double_ftype_double, "_ZN4java4lang4Math3sinEJdd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_SQRT, "__builtin_sqrt", double_ftype_double, "_ZN4java4lang4Math4sqrtEJdd", - ECF_CONST); + ECF_CONST | ECF_LEAF); define_builtin (BUILT_IN_TAN, "__builtin_tan", double_ftype_double, "_ZN4java4lang4Math3tanEJdd", - ECF_CONST); - + ECF_CONST | ECF_LEAF); + + int_ftype_int = build_function_type_list (int_type_node, + int_type_node, NULL_TREE); + + define_builtin (BUILT_IN_POPCOUNT, "__builtin_popcount", int_ftype_int, + "_ZN4java4lang7Integer8bitCountEJii", + ECF_CONST | ECF_LEAF | ECF_NOTHROW); + define_builtin (BUILT_IN_BSWAP32, "__builtin_bswap32", int_ftype_int, + "_ZN4java4lang7Integer12reverseBytesEJii", + ECF_CONST | ECF_LEAF | ECF_NOTHROW); + + define_builtin (BUILT_IN_POPCOUNTL, "__builtin_popcountl", + build_function_type_list (int_type_node, + long_type_node, NULL_TREE), + "_ZN4java4lang4Long8bitCountEJix", + ECF_CONST | ECF_LEAF | ECF_NOTHROW); + define_builtin (BUILT_IN_BSWAP64, "__builtin_bswap64", + build_function_type_list (long_type_node, + long_type_node, NULL_TREE), + "_ZN4java4lang4Long12reverseBytesEJxx", + ECF_CONST | ECF_LEAF | ECF_NOTHROW); + + define_builtin (BUILT_IN_BSWAP16, "__builtin_bswap16", + build_function_type_list (short_type_node, + short_type_node, NULL_TREE), + "_ZN4java4lang5Short12reverseBytesEJss", + ECF_CONST | ECF_LEAF | ECF_NOTHROW); + boolean_ftype_boolean_boolean = build_function_type_list (boolean_type_node, boolean_type_node, boolean_type_node, --Apple-Mail=_800444BF-D226-4945-990C-3CDC46EA7936 Content-Disposition: attachment; filename=BuiltinBitCount.java Content-Type: application/octet-stream; name="BuiltinBitCount.java" Content-Transfer-Encoding: 7bit Content-length: 1135 class BuiltinBitCount { public static int popcount(int x) { return Integer.bitCount(x); } public static int popcountl(long x) { return Long.bitCount(x); } public static void main(String[] args) { if (Integer.bitCount(0) != 0) throw new Error(); if (Integer.bitCount(8) != 1) throw new Error(); if (Integer.bitCount(123456) != 6) throw new Error(); if (Integer.bitCount(-1) != 32) throw new Error(); if (Long.bitCount(0) != 0) throw new Error(); if (Long.bitCount(8) != 1) throw new Error(); if (Long.bitCount(123456) != 6) throw new Error(); if (Long.bitCount(-1) != 64) throw new Error(); if (popcount(0) != 0) throw new Error(); if (popcount(8) != 1) throw new Error(); if (popcount(123456) != 6) throw new Error(); if (popcount(-1) != 32) throw new Error(); if (popcountl(0) != 0) throw new Error(); if (popcountl(8) != 1) throw new Error(); if (popcountl(123456) != 6) throw new Error(); if (popcountl(-1) != 64) throw new Error(); } } --Apple-Mail=_800444BF-D226-4945-990C-3CDC46EA7936 Content-Disposition: attachment; filename=BuiltinReverseBytes.java Content-Type: application/octet-stream; name="BuiltinReverseBytes.java" Content-Transfer-Encoding: 7bit Content-length: 1569 class BuiltinReverseBytes { public static short bswap16(short x) { return Short.reverseBytes(x); } public static int bswap32(int x) { return Integer.reverseBytes(x); } public static long bswap64(long x) { return Long.reverseBytes(x); } public static void main(String[] args) { if (Short.reverseBytes((short)0) != (short)0) throw new Error(); if (Short.reverseBytes((short)0x1234) != (short)0x3412) throw new Error(); if (Short.reverseBytes((short)-1) != (short)-1) throw new Error(); if (Integer.reverseBytes(0) != 0) throw new Error(); if (Integer.reverseBytes(0x12345678) != 0x78563412) throw new Error(); if (Integer.reverseBytes(-1) != -1) throw new Error(); if (Long.reverseBytes(0L) != 0L) throw new Error(); if (Long.reverseBytes(0x123456789abcde0fL) != 0x0fdebc9a78563412L) throw new Error(); if (Long.reverseBytes(-1L) != -1L) throw new Error(); if (bswap16((short)0) != (short)0) throw new Error(); if (bswap16((short)0x1234) != (short)0x3412) throw new Error(); if (bswap16((short)-1) != (short)-1) throw new Error(); if (bswap32(0) != 0) throw new Error(); if (bswap32(0x12345678) != 0x78563412) throw new Error(); if (bswap32(-1) != -1) throw new Error(); if (bswap64(0L) != 0L) throw new Error(); if (bswap64(0x123456789abcde0fL) != 0x0fdebc9a78563412L) throw new Error(); if (bswap64(-1L) != -1L) throw new Error(); } } --Apple-Mail=_800444BF-D226-4945-990C-3CDC46EA7936 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii Content-length: 2 --Apple-Mail=_800444BF-D226-4945-990C-3CDC46EA7936--