From: Saurabh Jha <[email protected]>

On windows targets, the size of long double is 64 bits. Therefore,
unlike aarch64-**-linux-gnu, where the size of long double is 128
bits, the size of long double for aarch64-w64-mingw32 has to be 64
bits.

This commit makes changes to gcc and libgfortran so that long double is
64 bits for aarch64-w64-mingw32.

gcc/ChangeLog:

        * config/aarch64/aarch64-coff.h
        (TARGET_LONG_DOUBLE_64): Set the size of long double to 64 bits.
        (__NO_BINARY80__): Define this so that __ENABLE_BINARY80__ is
        set to 0.
        * config/aarch64/aarch64.cc
        (aarch64_scalar_mode_supported_p): Make long double 64 bits.
        (aarch64_c_mode_for_floating_type): Make long double 64 bits.

libgfortran/ChangeLog:

        * configure: Regenerate.
        * configure.ac: Add support for mingw and cygwin.

gcc/testsuite/ChangeLog:

        * gcc.target/aarch64/mingw/mingw.exp: New test.
        * gcc.target/aarch64/mingw/long_double_size.c: New test.

co-authored-by: Radek Barton <[email protected]>
---
 gcc/config/aarch64/aarch64-coff.h             |  7 ++++
 gcc/config/aarch64/aarch64.cc                 | 12 ++++++
 .../aarch64/mingw/long_double_size.c          | 25 +++++++++++
 .../gcc.target/aarch64/mingw/mingw.exp        | 41 +++++++++++++++++++
 libgfortran/configure                         | 10 +++--
 libgfortran/configure.ac                      |  7 +++-
 6 files changed, 97 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/aarch64/mingw/long_double_size.c
 create mode 100644 gcc/testsuite/gcc.target/aarch64/mingw/mingw.exp

diff --git a/gcc/config/aarch64/aarch64-coff.h b/gcc/config/aarch64/aarch64-coff.h
index bcb3a782ba5..0385d0c7550 100644
--- a/gcc/config/aarch64/aarch64-coff.h
+++ b/gcc/config/aarch64/aarch64-coff.h
@@ -31,9 +31,16 @@
 #undef PTRDIFF_TYPE
 #define PTRDIFF_TYPE	"long long int"
 
+/* For Coff, the size of long double is 64 bits.
+   Reference:
+   https://learn.microsoft.com/en-us/cpp/c-language/type-long-double.  */
+#define TARGET_LONG_DOUBLE_64 1
+
 #undef LONG_TYPE_SIZE
 #define LONG_TYPE_SIZE 32
 
+#define __NO_BINARY80__
+
 #ifndef ASM_GENERATE_INTERNAL_LABEL
 # define ASM_GENERATE_INTERNAL_LABEL(STRING, PREFIX, NUM)  \
   sprintf (STRING, "*%s%s%u", LOCAL_LABEL_PREFIX, PREFIX, (unsigned int)(NUM))
diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 6f6dea67e0d..1433c7b0f71 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -29609,6 +29609,16 @@ aarch64_scalar_mode_supported_p (scalar_mode mode)
   if (DECIMAL_FLOAT_MODE_P (mode))
     return default_decimal_float_supported_p ();
 
+/* If long double is 64bit, we need to explicitly specify that aarch64
+   port is prepared to handle TFmode instructions.
+   If long double is 128bit, this is handled by
+   default_scalar_mode_supported_p.
+*/
+#ifdef TARGET_LONG_DOUBLE_64
+  if (mode == TFmode)
+    return true;
+#endif
+
   return ((mode == HFmode || mode == BFmode)
 	  ? true
 	  : default_scalar_mode_supported_p (mode));
@@ -29699,8 +29709,10 @@ aarch64_bitint_type_info (int n, struct bitint_info *info)
 static machine_mode
 aarch64_c_mode_for_floating_type (enum tree_index ti)
 {
+#ifndef TARGET_LONG_DOUBLE_64
   if (ti == TI_LONG_DOUBLE_TYPE)
     return TFmode;
+#endif
   return default_mode_for_floating_type (ti);
 }
 
diff --git a/gcc/testsuite/gcc.target/aarch64/mingw/long_double_size.c b/gcc/testsuite/gcc.target/aarch64/mingw/long_double_size.c
new file mode 100644
index 00000000000..9579beb40c5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/mingw/long_double_size.c
@@ -0,0 +1,25 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-O2" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+/**
+ * For aarch64-w64-mingw32 target, long double is 64 bits. Unlike in
+ * aarch64-linux-gnu, where long double is 128 bits.
+ *
+ * The tests below validate that. In aarch64-linux-gnu, the results would
+ * be the opposite.
+ */
+
+/*
+** test:
+**	fmov	d0, 4.0e\+0
+**	ret
+*/
+long double
+test ()
+{
+  long double i = 4;
+  return i;
+}
+
+/* { dg-final { scan-assembler-not "ldr\tq\[0-9\]+, \[x\[0-9\]+\]*" } } */
\ No newline at end of file
diff --git a/gcc/testsuite/gcc.target/aarch64/mingw/mingw.exp b/gcc/testsuite/gcc.target/aarch64/mingw/mingw.exp
new file mode 100644
index 00000000000..9986e0421af
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/mingw/mingw.exp
@@ -0,0 +1,41 @@
+# Copyright (C) 2025 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# GCC testsuite that uses the `dg.exp' driver.
+
+# Exit immediately if this isn't an AArch64-Mingw target.
+if {![istarget aarch64-w64-mingw32] } then {
+    return
+}
+
+# Load support procs.
+load_lib gcc-dg.exp
+
+# If a testcase doesn't have special options, use these.
+global DEFAULT_CFLAGS
+if ![info exists DEFAULT_CFLAGS] then {
+    set DEFAULT_CFLAGS " -ansi -pedantic-errors"
+}
+
+# Initialize `dg'.
+dg-init
+
+# Main loop.
+dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.\[cCS\]]] \
+	"" $DEFAULT_CFLAGS
+
+# All done.
+dg-finish
diff --git a/libgfortran/configure b/libgfortran/configure
index c6b00a9a2a4..712417b656a 100755
--- a/libgfortran/configure
+++ b/libgfortran/configure
@@ -6226,10 +6226,12 @@ fi
 # or floating point numbers – or may want to reduce the libgfortran size
 # although they do have the support.
 LIBGOMP_CHECKED_INT_KINDS="1 2 4 8 16"
-LIBGOMP_CHECKED_REAL_KINDS="4 8 10 16"
-
-
-
+case $host in
+  aarch64-*-mingw* | aarch64-*-cygwin*)
+    LIBGOMP_CHECKED_REAL_KINDS="4 8" ;;
+  *)
+    LIBGOMP_CHECKED_REAL_KINDS="4 8 10 16" ;;
+esac
 
 # Figure out whether the compiler supports "-ffunction-sections -fdata-sections",
 # similarly to how libstdc++ does it
diff --git a/libgfortran/configure.ac b/libgfortran/configure.ac
index cca1ea0ea97..c01d5024833 100644
--- a/libgfortran/configure.ac
+++ b/libgfortran/configure.ac
@@ -220,7 +220,12 @@ AM_CONDITIONAL(LIBGFOR_MINIMAL, false)
 # or floating point numbers – or may want to reduce the libgfortran size
 # although they do have the support.
 LIBGOMP_CHECKED_INT_KINDS="1 2 4 8 16"
-LIBGOMP_CHECKED_REAL_KINDS="4 8 10 16"
+case "${host}" in
+  aarch64-*-mingw* | aarch64-*-cygwin*)
+    LIBGOMP_CHECKED_REAL_KINDS="4 8" ;;
+  *)
+    LIBGOMP_CHECKED_REAL_KINDS="4 8 10 16" ;;
+esac
 
 AC_SUBST(LIBGOMP_CHECKED_INT_KINDS)
 AC_SUBST(LIBGOMP_CHECKED_REAL_KINDS)

Reply via email to