public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Use __BYTE_ORDER__ predefined macro instead of runtime check
@ 2017-11-22 12:59 Janne Blomqvist
  2017-11-22 16:57 ` Jerry DeLisle
  2017-11-22 18:42 ` Thomas Koenig
  0 siblings, 2 replies; 5+ messages in thread
From: Janne Blomqvist @ 2017-11-22 12:59 UTC (permalink / raw)
  To: fortran, gcc-patches; +Cc: Janne Blomqvist

By using the __BYTE_ORDER__ predefined macro we don't need the
determine_endianness function anymore.

Regtested on x86_64-pc-linux-gnu, Ok for trunk?

libgfortran/ChangeLog:

2017-11-22  Janne Blomqvist  <jb@gcc.gnu.org>

	* io/inquire.c (inquire_via_unit): Use __BYTE_ORDER__ predefined
	macro.
	* io/open.c (st_open): Likewise.
	* io/transfer.c (data_transfer_init): Likewise.
	* io/write.c (btoa_big): Likewise.
	(otoa_big): Likewise.
	(ztoa_big): Likewise.
	* libgfortran.h (big_endian): Remove variable.
	(GFOR_POINTER_TO_L1): Use __BYTE_ORDER__ macro.
	* runtime/main.c (determine_endianness): Remove function.
	(init): Remove call to determine_endianness.
	* runtime/minimal.c: Remove setting big_endian variable.
---
 libgfortran/io/inquire.c      |  5 ++---
 libgfortran/io/open.c         |  6 ++----
 libgfortran/io/transfer.c     |  6 ++----
 libgfortran/io/write.c        |  6 +++---
 libgfortran/libgfortran.h     |  6 +-----
 libgfortran/runtime/main.c    | 28 ----------------------------
 libgfortran/runtime/minimal.c |  7 -------
 7 files changed, 10 insertions(+), 54 deletions(-)

diff --git a/libgfortran/io/inquire.c b/libgfortran/io/inquire.c
index 4cf87d3..fe353c5 100644
--- a/libgfortran/io/inquire.c
+++ b/libgfortran/io/inquire.c
@@ -612,13 +612,12 @@ inquire_via_unit (st_parameter_inquire *iqp, gfc_unit *u)
       else
 	switch (u->flags.convert)
 	  {
-	    /*  big_endian is 0 for little-endian, 1 for big-endian.  */
 	  case GFC_CONVERT_NATIVE:
-	    p = big_endian ? "BIG_ENDIAN" : "LITTLE_ENDIAN";
+	    p = __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? "BIG_ENDIAN" : "LITTLE_ENDIAN";
 	    break;
 
 	  case GFC_CONVERT_SWAP:
-	    p = big_endian ? "LITTLE_ENDIAN" : "BIG_ENDIAN";
+	    p = __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? "LITTLE_ENDIAN" : "BIG_ENDIAN";
 	    break;
 
 	  default:
diff --git a/libgfortran/io/open.c b/libgfortran/io/open.c
index 9d3988a..fab2065 100644
--- a/libgfortran/io/open.c
+++ b/libgfortran/io/open.c
@@ -805,8 +805,6 @@ st_open (st_parameter_open *opp)
 	conv = compile_options.convert;
     }
   
-  /* We use big_endian, which is 0 on little-endian machines
-     and 1 on big-endian machines.  */
   switch (conv)
     {
     case GFC_CONVERT_NATIVE:
@@ -814,11 +812,11 @@ st_open (st_parameter_open *opp)
       break;
       
     case GFC_CONVERT_BIG:
-      conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
+      conv = __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
       break;
       
     case GFC_CONVERT_LITTLE:
-      conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
+      conv = __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
       break;
       
     default:
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c
index 1eb23fb..acaa88a 100644
--- a/libgfortran/io/transfer.c
+++ b/libgfortran/io/transfer.c
@@ -2723,8 +2723,6 @@ data_transfer_init (st_parameter_dt *dtp, int read_flag)
       if (conv == GFC_CONVERT_NONE)
 	conv = compile_options.convert;
 
-      /* We use big_endian, which is 0 on little-endian machines
-	 and 1 on big-endian machines.  */
       switch (conv)
 	{
 	case GFC_CONVERT_NATIVE:
@@ -2732,11 +2730,11 @@ data_transfer_init (st_parameter_dt *dtp, int read_flag)
 	  break;
 
 	case GFC_CONVERT_BIG:
-	  conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
+	  conv = __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
 	  break;
 
 	case GFC_CONVERT_LITTLE:
-	  conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
+	  conv = __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
 	  break;
 
 	default:
diff --git a/libgfortran/io/write.c b/libgfortran/io/write.c
index c9aad15..f417202 100644
--- a/libgfortran/io/write.c
+++ b/libgfortran/io/write.c
@@ -986,7 +986,7 @@ btoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n)
   int i, j;
 
   q = buffer;
-  if (big_endian)
+  if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
     {
       const char *p = s;
       for (i = 0; i < len; i++)
@@ -1051,7 +1051,7 @@ otoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n)
   *q = '\0';
   i = k = octet = 0;
 
-  if (big_endian)
+  if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
     {
       const char *p = s + len - 1;
       char c = *p;
@@ -1126,7 +1126,7 @@ ztoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n)
 
   q = buffer;
 
-  if (big_endian)
+  if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
     {
       const char *p = s;
       for (i = 0; i < len; i++)
diff --git a/libgfortran/libgfortran.h b/libgfortran/libgfortran.h
index cdbdd951..10c7507 100644
--- a/libgfortran/libgfortran.h
+++ b/libgfortran/libgfortran.h
@@ -266,12 +266,8 @@ typedef GFC_UINTEGER_4 gfc_char4_t;
    simply equal to the kind parameter itself.  */
 #define GFC_SIZE_OF_CHAR_KIND(kind) (kind)
 
-/* This will be 0 on little-endian machines and one on big-endian machines.  */
-extern int big_endian;
-internal_proto(big_endian);
-
 #define GFOR_POINTER_TO_L1(p, kind) \
-  (big_endian * (kind - 1) + (GFC_LOGICAL_1 *)(p))
+  ((__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? 1: 0) * (kind - 1) + (GFC_LOGICAL_1 *)(p))
 
 #define GFC_INTEGER_1_HUGE \
   (GFC_INTEGER_1)((((GFC_UINTEGER_1)1) << 7) - 1)
diff --git a/libgfortran/runtime/main.c b/libgfortran/runtime/main.c
index 8d466d1..2096271 100644
--- a/libgfortran/runtime/main.c
+++ b/libgfortran/runtime/main.c
@@ -33,31 +33,6 @@ stupid_function_name_for_static_linking (void)
   return;
 }
 
-/* This will be 0 for little-endian
-   machines and 1 for big-endian machines.  */
-int big_endian = 0;
-
-
-/* Figure out endianness for this machine.  */
-
-static void
-determine_endianness (void)
-{
-  union
-  {
-    GFC_LOGICAL_8 l8;
-    GFC_LOGICAL_4 l4[2];
-  } u;
-
-  u.l8 = 1;
-  if (u.l4[0])
-    big_endian = 0;
-  else if (u.l4[1])
-    big_endian = 1;
-  else
-    runtime_error ("Unable to determine machine endianness");
-}
-
 
 static int argc_save;
 static char **argv_save;
@@ -89,9 +64,6 @@ get_args (int *argc, char ***argv)
 static void __attribute__((constructor))
 init (void)
 {
-  /* Figure out the machine endianness.  */
-  determine_endianness ();
-
   /* Must be first */
   init_variables ();
 
diff --git a/libgfortran/runtime/minimal.c b/libgfortran/runtime/minimal.c
index 2ef4f15..3c7eca1 100644
--- a/libgfortran/runtime/minimal.c
+++ b/libgfortran/runtime/minimal.c
@@ -40,13 +40,6 @@ stupid_function_name_for_static_linking (void)
 
 options_t options;
 
-/* This will be 0 for little-endian
-   machines and 1 for big-endian machines.
-
-   Currently minimal libgfortran only runs on little-endian devices
-   which don't support constructors so this is just a constant.  */
-int big_endian = 0;
-
 static int argc_save;
 static char **argv_save;
 
-- 
2.7.4

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Use __BYTE_ORDER__ predefined macro instead of runtime check
  2017-11-22 12:59 [PATCH] Use __BYTE_ORDER__ predefined macro instead of runtime check Janne Blomqvist
@ 2017-11-22 16:57 ` Jerry DeLisle
  2017-11-22 18:42 ` Thomas Koenig
  1 sibling, 0 replies; 5+ messages in thread
From: Jerry DeLisle @ 2017-11-22 16:57 UTC (permalink / raw)
  To: Janne Blomqvist, fortran, gcc-patches

On 11/22/2017 04:34 AM, Janne Blomqvist wrote:
> By using the __BYTE_ORDER__ predefined macro we don't need the
> determine_endianness function anymore.
> 
> Regtested on x86_64-pc-linux-gnu, Ok for trunk?
> 

Looks good, thanks for patch!

Jerry

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Use __BYTE_ORDER__ predefined macro instead of runtime check
  2017-11-22 12:59 [PATCH] Use __BYTE_ORDER__ predefined macro instead of runtime check Janne Blomqvist
  2017-11-22 16:57 ` Jerry DeLisle
@ 2017-11-22 18:42 ` Thomas Koenig
  2017-11-22 19:22   ` Andreas Schwab
  2017-11-22 20:03   ` Janne Blomqvist
  1 sibling, 2 replies; 5+ messages in thread
From: Thomas Koenig @ 2017-11-22 18:42 UTC (permalink / raw)
  To: Janne Blomqvist, fortran, gcc-patches

Hi janne,

> Regtested on x86_64-pc-linux-gnu, Ok for trunk?

Jerry already OK'd this, so you can commit if you want.
What you could do is to hide the macro invocation behind
a macro in libgfortran.h, something like

#define BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)

Also, I have opened PR libfortran/83097 for this, you
can mention this in the ChangeLog entry.

Regards

	Thomas

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Use __BYTE_ORDER__ predefined macro instead of runtime check
  2017-11-22 18:42 ` Thomas Koenig
@ 2017-11-22 19:22   ` Andreas Schwab
  2017-11-22 20:03   ` Janne Blomqvist
  1 sibling, 0 replies; 5+ messages in thread
From: Andreas Schwab @ 2017-11-22 19:22 UTC (permalink / raw)
  To: Thomas Koenig; +Cc: Janne Blomqvist, fortran, gcc-patches

On Nov 22 2017, Thomas Koenig <tkoenig@netcologne.de> wrote:

> Jerry already OK'd this, so you can commit if you want.
> What you could do is to hide the macro invocation behind
> a macro in libgfortran.h, something like
>
> #define BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)

That can easily be confused with the <endian.h> macro which has
different semantics.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Use __BYTE_ORDER__ predefined macro instead of runtime check
  2017-11-22 18:42 ` Thomas Koenig
  2017-11-22 19:22   ` Andreas Schwab
@ 2017-11-22 20:03   ` Janne Blomqvist
  1 sibling, 0 replies; 5+ messages in thread
From: Janne Blomqvist @ 2017-11-22 20:03 UTC (permalink / raw)
  To: Thomas Koenig; +Cc: Fortran List, GCC Patches

On Wed, Nov 22, 2017 at 8:16 PM, Thomas Koenig <tkoenig@netcologne.de> wrote:
> Hi janne,
>
>> Regtested on x86_64-pc-linux-gnu, Ok for trunk?
>
>
> Jerry already OK'd this, so you can commit if you want.
> What you could do is to hide the macro invocation behind
> a macro in libgfortran.h, something like
>
> #define BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
>
> Also, I have opened PR libfortran/83097 for this, you
> can mention this in the ChangeLog entry.
>
> Regards
>
>         Thomas

Hi,

in light of Andreas comments, and also since the meaning of the old
big_endian variable was apparently confusing enough that somebody had
deemed it necessary to explain it in multiple places where it was
used, I committed the original patch as r255072.  Having the
conditional explicitly where it's used at least makes it pretty clear
what we're testing.

Also thanks for opening a PR, I mentioned this in the ChangeLog and commit msg.


-- 
Janne Blomqvist

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-11-22 19:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-22 12:59 [PATCH] Use __BYTE_ORDER__ predefined macro instead of runtime check Janne Blomqvist
2017-11-22 16:57 ` Jerry DeLisle
2017-11-22 18:42 ` Thomas Koenig
2017-11-22 19:22   ` Andreas Schwab
2017-11-22 20:03   ` Janne Blomqvist

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).