From patchwork Wed May 28 23:10:52 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 353604 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id B8CD31400E7 for ; Thu, 29 May 2014 09:11:12 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type; q=dns; s= default; b=bwhneRpFYi35yX7PXvmxjt0n9N1V3RRdNufeLk7n81fp5qqpuOc2V ZxfIR7SgeN9sD1mlnuyUwVT3OACEd3CNg4vpiMaOgeMiEmWBa6EZcxY5WASKRHSC mdjL0Fq9ojtomVKOv0jh3lVaEHxQlWpTLLKdzakTY8urLBjQyahPQM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type; s= default; bh=fbAp6/ZHpBZZ4MB0/bGQ+hhtI0E=; b=fcWwfMqwooMxDeAynkf3 /PGUvhVjQ0KtkmeGm5ByTR4GC4Bbr0oTUGGJ/ZxE4zD7xUlHGe9eublylaIhcks8 Bxny8gDEoEXG1KRiFI7+kGdv6nVSiNzg6gNtOjIwXEeV/pDpgE31d7/SWNCIq8b9 G0m67eS8QmTREbFmAN6XwfU= Received: (qmail 2489 invoked by alias); 28 May 2014 23:10:59 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 2398 invoked by uid 89); 28 May 2014 23:10:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS, T_TVD_MIME_NO_HEADERS autolearn=ham version=3.3.2 X-HELO: mail-pa0-f43.google.com Received: from mail-pa0-f43.google.com (HELO mail-pa0-f43.google.com) (209.85.220.43) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 28 May 2014 23:10:56 +0000 Received: by mail-pa0-f43.google.com with SMTP id hz1so11769368pad.2 for ; Wed, 28 May 2014 16:10:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:user-agent :mime-version:content-type; bh=OQ5w8lBJq84eFMrC3mXTC9WUmLjUWDVP0sDwf84GMS4=; b=cjImwfnUYZ62knotr7Nq4iVl1+6kvOhLfi9vbT1ye6HUwj8y5ZdjgJ1I69kwvnXWoh 6K7mtMbhcuUOCFqzXKUOGGOKo0e2FhOJTHv7QuwDrZNdR9X794HqgBuQU9Gg0RdyhJBC mrSFoxuy7Puhq30RmIx26jFagOWBOyHeSFc9Kku/+7SMDAIGMaEwdo068AJM12MqoiKK pUGmBgnkT2eM4nfO+7TNwPnTRRFn4awy19FYzjmpgTaw/cJyVqGqDljpb8Qm5O/adTAc aTWQ8SLMWo5O2sQLZajCRPhZhKL2h2jlYw/AA3/VZl0U2lOs7cJT7Ke+YOhNZrgGeG8M szHA== X-Gm-Message-State: ALoCoQmxCYmc4BRrsnXzkJ5W55zLFksNytMqoTFdx5rSA0gq+0K45bhm8/DLReRIRBfZQe3bjp78 X-Received: by 10.66.65.204 with SMTP id z12mr1482321pas.97.1401318654537; Wed, 28 May 2014 16:10:54 -0700 (PDT) Received: from iant-glaptop.roam.corp.google.com.google.com ([2620:0:1000:3204:5573:a1f8:f69a:eb95]) by mx.google.com with ESMTPSA id gz11sm30195525pbd.1.2014.05.28.16.10.52 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 28 May 2014 16:10:53 -0700 (PDT) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: libgo patch committed: Make libgo C code more portable Date: Wed, 28 May 2014 16:10:52 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) MIME-Version: 1.0 X-IsSubscribed: yes This patch to libgo, from Peter Collingbourne, changes some of the C code to make it easier to compile libgo with a compiler other than GCC. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline. Ian diff -r d73f07d002ef libgo/runtime/go-cdiv.c --- a/libgo/runtime/go-cdiv.c Tue May 27 15:00:31 2014 -0700 +++ b/libgo/runtime/go-cdiv.c Wed May 28 16:09:25 2014 -0700 @@ -4,6 +4,9 @@ Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ +#include +#include + /* Calls to these functions are generated by the Go frontend for division of complex64 or complex128. We use these because Go's complex division expects slightly different results from the GCC @@ -13,33 +16,33 @@ the the whole number is Inf, but an operation involving NaN ought to result in NaN, not Inf. */ -__complex float -__go_complex64_div (__complex float a, __complex float b) +complex float +__go_complex64_div (complex float a, complex float b) { - if (__builtin_expect (b == 0+0i, 0)) + if (__builtin_expect (b == 0, 0)) { - if (!__builtin_isinff (__real__ a) - && !__builtin_isinff (__imag__ a) - && (__builtin_isnanf (__real__ a) || __builtin_isnanf (__imag__ a))) + if (!isinf (crealf (a)) + && !isinf (cimagf (a)) + && (isnan (crealf (a)) || isnan (cimagf (a)))) { /* Pass "1" to nanf to match math/bits.go. */ - return __builtin_nanf("1") + __builtin_nanf("1")*1i; + return nanf("1") + nanf("1")*I; } } return a / b; } -__complex double -__go_complex128_div (__complex double a, __complex double b) +complex double +__go_complex128_div (complex double a, complex double b) { - if (__builtin_expect (b == 0+0i, 0)) + if (__builtin_expect (b == 0, 0)) { - if (!__builtin_isinf (__real__ a) - && !__builtin_isinf (__imag__ a) - && (__builtin_isnan (__real__ a) || __builtin_isnan (__imag__ a))) + if (!isinf (creal (a)) + && !isinf (cimag (a)) + && (isnan (creal (a)) || isnan (cimag (a)))) { /* Pass "1" to nan to match math/bits.go. */ - return __builtin_nan("1") + __builtin_nan("1")*1i; + return nan("1") + nan("1")*I; } } return a / b; diff -r d73f07d002ef libgo/runtime/go-type-complex.c --- a/libgo/runtime/go-type-complex.c Tue May 27 15:00:31 2014 -0700 +++ b/libgo/runtime/go-type-complex.c Wed May 28 16:09:25 2014 -0700 @@ -4,13 +4,13 @@ Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ +#include +#include +#include +#include #include "runtime.h" #include "go-type.h" -/* The 64-bit type. */ - -typedef unsigned int DItype __attribute__ ((mode (DI))); - /* Hash function for float types. */ uintptr_t @@ -18,69 +18,67 @@ { if (key_size == 8) { - union - { - unsigned char a[8]; - __complex float cf; - DItype di; - } ucf; - __complex float cf; + const complex float *cfp; + complex float cf; float cfr; float cfi; + uint64_t fi; - __builtin_memcpy (ucf.a, vkey, 8); - cf = ucf.cf; - cfr = __builtin_crealf (cf); - cfi = __builtin_cimagf (cf); - if (__builtin_isinff (cfr) || __builtin_isinff (cfi)) + cfp = (const complex float *) vkey; + cf = *cfp; + + cfr = crealf (cf); + cfi = cimagf (cf); + + if (isinf (cfr) || isinf (cfi)) return 0; /* NaN != NaN, so the hash code of a NaN is irrelevant. Make it random so that not all NaNs wind up in the same place. */ - if (__builtin_isnanf (cfr) || __builtin_isnanf (cfi)) + if (isnan (cfr) || isnan (cfi)) return runtime_fastrand1 (); /* Avoid negative zero. */ if (cfr == 0 && cfi == 0) return 0; else if (cfr == 0) - ucf.cf = cfi * 1.0iF; + cf = cfi * I; else if (cfi == 0) - ucf.cf = cfr; + cf = cfr; - return ucf.di; + memcpy (&fi, &cf, 8); + return (uintptr_t) cfi; } else if (key_size == 16) { - union - { - unsigned char a[16]; - __complex double cd; - DItype adi[2]; - } ucd; - __complex double cd; + const complex double *cdp; + complex double cd; double cdr; double cdi; + uint64_t di[2]; - __builtin_memcpy (ucd.a, vkey, 16); - cd = ucd.cd; - cdr = __builtin_crealf (cd); - cdi = __builtin_cimagf (cd); - if (__builtin_isinf (cdr) || __builtin_isinf (cdi)) + cdp = (const complex double *) vkey; + cd = *cdp; + + cdr = creal (cd); + cdi = cimag (cd); + + if (isinf (cdr) || isinf (cdi)) return 0; - if (__builtin_isnan (cdr) || __builtin_isnan (cdi)) + if (isnan (cdr) || isnan (cdi)) return runtime_fastrand1 (); /* Avoid negative zero. */ if (cdr == 0 && cdi == 0) return 0; else if (cdr == 0) - ucd.cd = cdi * 1.0i; + cd = cdi * I; else if (cdi == 0) - ucd.cd = cdr; + cd = cdr; - return ucd.adi[0] ^ ucd.adi[1]; + memcpy (&di, &cd, 16); + return di[0] ^ di[1]; } else runtime_throw ("__go_type_hash_complex: invalid complex size"); @@ -93,35 +91,23 @@ { if (key_size == 8) { - union - { - unsigned char a[8]; - __complex float cf; - } ucf; - __complex float cf1; - __complex float cf2; + const complex float *cfp1; + const complex float *cfp2; + + cfp1 = (const complex float *) vk1; + cfp2 = (const complex float *) vk2; - __builtin_memcpy (ucf.a, vk1, 8); - cf1 = ucf.cf; - __builtin_memcpy (ucf.a, vk2, 8); - cf2 = ucf.cf; - return cf1 == cf2; + return *cfp1 == *cfp2; } else if (key_size == 16) { - union - { - unsigned char a[16]; - __complex double cd; - } ucd; - __complex double cd1; - __complex double cd2; + const complex double *cdp1; + const complex double *cdp2; + + cdp1 = (const complex double *) vk1; + cdp2 = (const complex double *) vk2; - __builtin_memcpy (ucd.a, vk1, 16); - cd1 = ucd.cd; - __builtin_memcpy (ucd.a, vk2, 16); - cd2 = ucd.cd; - return cd1 == cd2; + return *cdp1 == *cdp2; } else runtime_throw ("__go_type_equal_complex: invalid complex size"); diff -r d73f07d002ef libgo/runtime/go-type-float.c --- a/libgo/runtime/go-type-float.c Tue May 27 15:00:31 2014 -0700 +++ b/libgo/runtime/go-type-float.c Wed May 28 16:09:25 2014 -0700 @@ -4,14 +4,11 @@ Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ +#include +#include #include "runtime.h" #include "go-type.h" -/* The 32-bit and 64-bit types. */ - -typedef unsigned int SItype __attribute__ ((mode (SI))); -typedef unsigned int DItype __attribute__ ((mode (DI))); - /* Hash function for float types. */ uintptr_t @@ -19,45 +16,41 @@ { if (key_size == 4) { - union - { - unsigned char a[4]; - float f; - SItype si; - } uf; + const float *fp; float f; + uint32_t si; - __builtin_memcpy (uf.a, vkey, 4); - f = uf.f; - if (__builtin_isinff (f) || f == 0) + fp = (const float *) vkey; + f = *fp; + + if (isinf (f) || f == 0) return 0; /* NaN != NaN, so the hash code of a NaN is irrelevant. Make it random so that not all NaNs wind up in the same place. */ - if (__builtin_isnanf (f)) + if (isnan (f)) return runtime_fastrand1 (); - return (uintptr_t) uf.si; + memcpy (&si, vkey, 4); + return (uintptr_t) si; } else if (key_size == 8) { - union - { - unsigned char a[8]; - double d; - DItype di; - } ud; + const double *dp; double d; + uint64_t di; - __builtin_memcpy (ud.a, vkey, 8); - d = ud.d; - if (__builtin_isinf (d) || d == 0) + dp = (const double *) vkey; + d = *dp; + + if (isinf (d) || d == 0) return 0; - if (__builtin_isnan (d)) + if (isnan (d)) return runtime_fastrand1 (); - return (uintptr_t) ud.di; + memcpy (&di, vkey, 8); + return (uintptr_t) di; } else runtime_throw ("__go_type_hash_float: invalid float size"); @@ -70,36 +63,23 @@ { if (key_size == 4) { - union - { - unsigned char a[4]; - float f; - } uf; - float f1; - float f2; + const float *fp1; + const float *fp2; - __builtin_memcpy (uf.a, vk1, 4); - f1 = uf.f; - __builtin_memcpy (uf.a, vk2, 4); - f2 = uf.f; - return f1 == f2; + fp1 = (const float *) vk1; + fp2 = (const float *) vk2; + + return *fp1 == *fp2; } else if (key_size == 8) { - union - { - unsigned char a[8]; - double d; - DItype di; - } ud; - double d1; - double d2; + const double *dp1; + const double *dp2; - __builtin_memcpy (ud.a, vk1, 8); - d1 = ud.d; - __builtin_memcpy (ud.a, vk2, 8); - d2 = ud.d; - return d1 == d2; + dp1 = (const double *) vk1; + dp2 = (const double *) vk2; + + return *dp1 == *dp2; } else runtime_throw ("__go_type_equal_float: invalid float size"); diff -r d73f07d002ef libgo/runtime/print.c --- a/libgo/runtime/print.c Tue May 27 15:00:31 2014 -0700 +++ b/libgo/runtime/print.c Wed May 28 16:09:25 2014 -0700 @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +#include +#include #include #include "runtime.h" #include "array.h" @@ -105,7 +107,7 @@ runtime_printfloat(va_arg(va, float64)); break; case 'C': - runtime_printcomplex(va_arg(va, __complex double)); + runtime_printcomplex(va_arg(va, complex double)); break; case 'i': runtime_printiface(va_arg(va, Iface)); @@ -174,13 +176,12 @@ gwrite("NaN", 3); return; } - i = __builtin_isinf_sign(v); - if(i > 0) { - gwrite("+Inf", 4); - return; - } - if(i < 0) { - gwrite("-Inf", 4); + if(isinf(v)) { + if(signbit(v)) { + gwrite("-Inf", 4); + } else { + gwrite("+Inf", 4); + } return; } @@ -243,11 +244,11 @@ } void -runtime_printcomplex(__complex double v) +runtime_printcomplex(complex double v) { gwrite("(", 1); - runtime_printfloat(__builtin_creal(v)); - runtime_printfloat(__builtin_cimag(v)); + runtime_printfloat(creal(v)); + runtime_printfloat(cimag(v)); gwrite("i)", 2); } diff -r d73f07d002ef libgo/runtime/runtime.h --- a/libgo/runtime/runtime.h Tue May 27 15:00:31 2014 -0700 +++ b/libgo/runtime/runtime.h Wed May 28 16:09:25 2014 -0700 @@ -5,6 +5,7 @@ #include "config.h" #include "go-assert.h" +#include #include #include #include @@ -710,7 +711,7 @@ void runtime_printuint(uint64); void runtime_printhex(uint64); void runtime_printslice(Slice); -void runtime_printcomplex(__complex double); +void runtime_printcomplex(complex double); void reflect_call(const struct __go_func_type *, FuncVal *, _Bool, _Bool, void **, void **) __asm__ (GOSYM_PREFIX "reflect.call");