--- libgfortran/kinds-override.h.jj	2022-06-22 12:40:05.170224299 +0200
+++ libgfortran/kinds-override.h	2022-06-24 14:19:00.886823630 +0200
@@ -34,13 +34,24 @@ see the files COPYING3 and COPYING.RUNTI
 
 /* Keep these conditions on one line so grep can filter it out.  */
 #if defined(__powerpc64__)  && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__  && __SIZEOF_LONG_DOUBLE__ == 16
+#ifdef HAVE__FLOAT128
+typedef _Float128 GFC_REAL_17;
+typedef _Complex _Float128 GFC_COMPLEX_17;
+#else
 typedef __float128 GFC_REAL_17;
 typedef _Complex float __attribute__((mode(KC))) GFC_COMPLEX_17;
+#endif
 #define HAVE_GFC_REAL_17
 #define HAVE_GFC_COMPLEX_17
+#ifdef HAVE__FLOAT128
+#define GFC_REAL_17_HUGE 1.18973149535723176508575932662800702e4932f128
+#define GFC_REAL_17_LITERAL_SUFFIX f128
+#define GFC_REAL_17_LITERAL(X) (X ## f128)
+#else
 #define GFC_REAL_17_HUGE 1.18973149535723176508575932662800702e4932q
 #define GFC_REAL_17_LITERAL_SUFFIX q
 #define GFC_REAL_17_LITERAL(X) (X ## q)
+#endif
 #define GFC_REAL_17_DIGITS 113
 #define GFC_REAL_17_RADIX 2
 #endif
--- libgfortran/intrinsics/erfc_scaled.c.jj	2022-06-24 07:38:55.640826314 -0400
+++ libgfortran/intrinsics/erfc_scaled.c	2022-06-24 09:10:05.311263353 -0400
@@ -147,12 +147,15 @@ ERFC_SCALED(16)
 /* For quadruple-precision, netlib's implementation is
    not accurate enough.  We provide another one.  */
 
-# define _THRESH -106.566990228185312813205074546585730Q
-# define _M_2_SQRTPI M_2_SQRTPIq
+# define _THRESH GFC_REAL_17_LITERAL(-106.566990228185312813205074546585730)
+# define _M_2_SQRTPI GFC_REAL_17_LITERAL(M_2_SQRTPI)
 # define _INF __builtin_inff128()
 # ifdef POWER_IEEE128
 #  define _ERFC(x) __erfcieee128(x)
 #  define _EXP(x) __expieee128(x)
+# elif defined(HAVE__FLOAT128)
+#  define _ERFC(x) erfcf128(x)
+#  define _EXP(x) expf128(x)
 # else
 #  define _ERFC(x) erfcq(x)
 #  define _EXP(x) expq(x)
--- libgfortran/m4/mtype.m4.jj	2022-06-22 13:52:56.987824469 +0200
+++ libgfortran/m4/mtype.m4	2022-06-24 14:21:40.435769659 +0200
@@ -15,6 +15,8 @@ ifelse(kind,17,`1 /* FIXME: figure this
 define(mathfunc_macro,`ifelse(kind,17,dnl
 `#if defined(POWER_IEEE128)
 #define MATHFUNC(funcname) __ ## funcname ## ieee128
+#elif defined(HAVE__FLOAT128)
+#define MATHFUNC(funcname) funcname ## f128
 #else
 #define MATHFUNC(funcname) funcname ## q
 #endif',dnl
--- libgfortran/io/read.c.jj	2022-06-22 14:13:35.351567588 +0200
+++ libgfortran/io/read.c	2022-06-24 14:29:43.199555539 +0200
@@ -203,6 +203,8 @@ convert_real (st_parameter_dt *dtp, void
     case 17:
 # if defined(POWER_IEEE128)
       *((GFC_REAL_17*) dest) = __strtoieee128 (buffer, &endptr);
+# elif defined(HAVE__FLOAT128)
+      *((GFC_REAL_17*) dest) = strtof128 (buffer, &endptr);
 # else
       *((GFC_REAL_17*) dest) = __qmath_(strtoflt128) (buffer, &endptr);
 # endif
--- libgfortran/io/transfer128.c.jj	2022-06-22 13:20:31.657252114 +0200
+++ libgfortran/io/transfer128.c	2022-06-24 14:31:18.616329211 +0200
@@ -67,7 +67,8 @@ export_proto(transfer_complex128_write);
    that there is a non-weakref dependence if the quadmath functions
    are used. That avoids segfault when libquadmath is statically linked.  */
 # ifndef GFC_REAL_16_IS__FLOAT128
-#  if !defined(HAVE_GFC_REAL_17) || !defined(POWER_IEEE128)
+#  if !defined(HAVE_GFC_REAL_17) \
+      || (!defined(POWER_IEEE128) && !defined(HAVE__FLOAT128))
 static void __attribute__((used)) *tmp1 = strtoflt128;
 static void __attribute__((used)) *tmp2 = quadmath_snprintf;
 #  endif
--- libgfortran/io/write_float.def.jj	2022-06-24 07:38:55.640826314 -0400
+++ libgfortran/io/write_float.def	2022-06-24 09:13:48.653519940 -0400
@@ -837,7 +837,9 @@ snprintf (buffer, size, "%+-#.*e", (prec
 #define DTOA2L(prec,val) \
 snprintf (buffer, size, "%+-#.*Le", (prec), (val))
 
-#ifdef GFC_REAL_16_IS__FLOAT128
+#if defined(GFC_REAL_16_IS__FLOAT128) \
+    || (defined(HAVE_GFC_REAL_17) && !defined(POWER_IEEE128) \
+	&& defined(HAVE__FLOAT128))
 /* strfromf128 unfortunately doesn't allow +, - and # modifiers
    nor .* (only allows .number).  For +, work around it by adding
    leading + manually for !signbit values.  For - I don't see why
@@ -870,6 +872,9 @@ gfor_strfromf128 (char *buffer, size_t s
     }
   return ret;
 }
+#endif
+
+#ifdef GFC_REAL_16_IS__FLOAT128
 # define DTOA2F128(prec,val) \
 gfor_strfromf128 (buffer, size, 'e', (prec), (val))
 #endif
@@ -878,6 +883,9 @@ gfor_strfromf128 (buffer, size, 'e', (pr
 # if defined(POWER_IEEE128)
 #  define DTOA2Q(prec,val) \
 __snprintfieee128 (buffer, size, "%+-#.*Le", (prec), (val))
+# elif defined(HAVE__FLOAT128)
+#  define DTOA2Q(prec,val) \
+gfor_strfromf128 (buffer, size, 'e', (prec), (val))
 # else
 #  define DTOA2Q(prec,val) \
 quadmath_snprintf (buffer, size, "%+-#.*Qe", (prec), (val))
@@ -905,6 +913,9 @@ gfor_strfromf128 (buffer, size, 'f', (pr
 # if defined(POWER_IEEE128)
 #  define FDTOA2Q(prec,val) \
 __snprintfieee128 (buffer, size, "%+-#.*Lf", (prec), (val))
+# elif defined(HAVE__FLOAT128)
+#  define FDTOA2Q(prec,val) \
+gfor_strfromf128 (buffer, size, 'f', (prec), (val))
 # else
 # define FDTOA2Q(prec,val) \
 quadmath_snprintf (buffer, size, "%+-#.*Qf", (prec), (val))
--- libgfortran/generated/norm2_r17.c.jj	2022-01-11 23:38:13.062640651 +0100
+++ libgfortran/generated/norm2_r17.c	2022-06-24 14:22:30.614123682 +0200
@@ -31,6 +31,8 @@ see the files COPYING3 and COPYING.RUNTI
 
 #if defined(POWER_IEEE128)
 #define MATHFUNC(funcname) __ ## funcname ## ieee128
+#elif defined(HAVE__FLOAT128)
+#define MATHFUNC(funcname) funcname ## f128
 #else
 #define MATHFUNC(funcname) funcname ## q
 #endif
--- libgfortran/generated/bessel_r17.c.jj	2022-01-11 23:38:13.057640721 +0100
+++ libgfortran/generated/bessel_r17.c	2022-06-24 14:22:11.108374785 +0200
@@ -30,6 +30,8 @@ see the files COPYING3 and COPYING.RUNTI
 
 #if defined(POWER_IEEE128)
 #define MATHFUNC(funcname) __ ## funcname ## ieee128
+#elif defined(HAVE__FLOAT128)
+#define MATHFUNC(funcname) funcname ## f128
 #else
 #define MATHFUNC(funcname) funcname ## q
 #endif
