I've noticed that for many years now there have been a recurring Windowsrelated 
complaint about "Error: unknown constraint 't'" message, which goes asearly as 
2007, e.g. 
https://lists.nongnu.org/archive/html/tinycc-devel/2007-07/msg00013.html

There appears to be a patch for it (didn't test it), but it seems it was never 
taken:http://lists.gnu.org/archive/html/tinycc-devel/2014-08/msg00024.html
I decided to approach it from a different direction, and while at it also add 3 
missing libmfunctions. Following these changes, at least 2 medium sized 
projects which tcc couldn'tbuilt on windows (using the windows official 
distribution package), now build and appearto be working well (mujs and 
Duktape).
If there are no objections, I'll push that to the mob branch soon. The patch is 
attached,and that's the commit 
message:----------------------------------------------------------
win: libm: fix "unknown constraint 't'", missing  fpclassify/round/etc

Some libm functions were broken at win32/include/math.h in few ways:

1. Unknown constraint 't': a similar piece of code was used in several inline
   functions at math.h (from mingw), and currently the tcc asm doesn't support
   it. This broke __fpclassifyl, __isnan* and __signbit*, and indirectly broke
   the macros fpclassify, isfinite, isinf, isnan, isnormal and signbit.

2. Even if 't' was supported by tcc's asm, there were no definitions for
   __fpclassify and __fpclassifyf, therefore breaking the macro fpclassify.
   Newer mingw(w64) math.h doesn't have this issue, but still uses 't' in asm.

3. Other than the above, some common libm functions werere not implemented
   anywhere in the tcc Windows distribution package - mainly fmin/fmax/round.
   Newer mingw(64) math.h stil doesn't include these implementations.

To address these issues, code which used inline asm with 't' was replaced with
a C implementation, and the missing __fpclassify functions were added.

The code is mostly taken from MUSL rs-1.0 (MIT) [*], and is placed as inline
functions at win32/include/tcc/tcc_libm.h, which is now included from math.h,
and is also copied to the win32 install target.

Future enhancements:
- Support 't' contraint in asm. Newer mingw headers still use it, and it would
  make future porting of mingw headers easier.
- Enumerate the still-missing libm functions (if there are such) and add missing
  implementations. Most should be simple and will add good value to tcc.
- Possibly move the code at tcc/tcc_libm.h to a C file and create an actual
  libm.a library, or just create a dummy libm. For build procedures which expect
  libm, this could allow to complete successfully, assuming no yet-unimplemented
  functions are used.
- Add some tests for common libm functions.

[*] - http://git.musl-libc.org/cgit/musl/tree/src/math?h=rs-1.0
    - MUSL's license: http://git.musl-libc.org/cgit/musl/tree/COPYRIGHT?h=rs-1.0
From 67b64295332c11641ccdcd582edbb040a5e528e0 Mon Sep 17 00:00:00 2001
From: "Avi Halachmi (:avih)" <[email protected]>
Date: Sun, 1 Nov 2015 18:47:03 +0200
Subject: [PATCH] win: libm: fix "unknown constraint 't'", missing
 fpclassify/round/etc

Some libm functions were broken at win32/include/math.h in few ways:

1. Unknown constraint 't': a similar piece of code was used in several inline
   functions at math.h (from mingw), and currently the tcc asm doesn't support
   it. This broke __fpclassifyl, __isnan* and __signbit*, and indirectly broke
   the macros fpclassify, isfinite, isinf, isnan, isnormal and signbit.

2. Even if 't' was supported by tcc's asm, there were no definitions for
   __fpclassify and __fpclassifyf, therefore breaking the macro fpclassify.
   Newer mingw(w64) math.h doesn't have this issue, but still uses 't' in asm.

3. Other than the above, some common libm functions werere not implemented
   anywhere in the tcc Windows distribution package - mainly fmin/fmax/round.
   Newer mingw(64) math.h stil doesn't include these implementations.

To address these issues, code which used inline asm with 't' was replaced with
a C implementation, the missing __fpclassify functions were added, as well as
fmin/fmax/round and their variants.

The code is mostly taken from MUSL rs-1.0 (MIT) [*], and is placed as inline
functions at win32/include/tcc/tcc_libm.h, which is now included from math.h,
and is also copied to the win32 install target.

Future enhancements:
- Support 't' contraint in asm. Newer mingw headers still use it, and it would
  make future porting of mingw headers easier.
- Enumerate the still-missing libm functions (if there are such) and add missing
  implementations. Most should be simple and will add good value to tcc.
- Possibly move the code at tcc/tcc_libm.h to a C file and create an actual
  libm.a library, or just create a dummy libm. For build procedures which expect
  libm, this could allow to complete successfully, assuming no yet-unimplemented
  functions are used.
- Add some tests for common libm functions.

[*] - http://git.musl-libc.org/cgit/musl/tree/src/math?h=rs-1.0
    - MUSL's license: http://git.musl-libc.org/cgit/musl/tree/COPYRIGHT?h=rs-1.0
---
 win32/include/math.h         |  90 ++------------------
 win32/include/tcc/tcc_libm.h | 192 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 200 insertions(+), 82 deletions(-)
 create mode 100644 win32/include/tcc/tcc_libm.h

diff --git a/win32/include/math.h b/win32/include/math.h
index 251d3f7..9e33a8c 100644
--- a/win32/include/math.h
+++ b/win32/include/math.h
@@ -311,15 +311,7 @@ extern "C" {
   converted to double, and zero when converted to float.)
   */
 
-  extern int __cdecl __fpclassifyf (float);
-  extern int __cdecl __fpclassify (double);
-
-  __CRT_INLINE int __cdecl __fpclassifyl (long double x){
-    unsigned short sw;
-    __asm__ ("fxam; fstsw %%ax;" : "=a" (sw): "t" (x));
-    return sw & (FP_NAN | FP_NORMAL | FP_ZERO );
-  }
-
+/* implemented at tcc/tcc_libm.h */
 #define fpclassify(x) (sizeof (x) == sizeof (float) ? __fpclassifyf (x)	  \
   : sizeof (x) == sizeof (double) ? __fpclassify (x) \
   : __fpclassifyl (x))
@@ -333,61 +325,12 @@ extern "C" {
   /* 7.12.3.4 */
   /* We don't need to worry about trucation here:
   A NaN stays a NaN. */
-
-  __CRT_INLINE int __cdecl __isnan (double _x)
-  {
-    unsigned short sw;
-    __asm__ ("fxam;"
-      "fstsw %%ax": "=a" (sw) : "t" (_x));
-    return (sw & (FP_NAN | FP_NORMAL | FP_INFINITE | FP_ZERO | FP_SUBNORMAL))
-      == FP_NAN;
-  }
-
-  __CRT_INLINE int __cdecl __isnanf (float _x)
-  {
-    unsigned short sw;
-    __asm__ ("fxam;"
-      "fstsw %%ax": "=a" (sw) : "t" (_x));
-    return (sw & (FP_NAN | FP_NORMAL | FP_INFINITE | FP_ZERO | FP_SUBNORMAL))
-      == FP_NAN;
-  }
-
-  __CRT_INLINE int __cdecl __isnanl (long double _x)
-  {
-    unsigned short sw;
-    __asm__ ("fxam;"
-      "fstsw %%ax": "=a" (sw) : "t" (_x));
-    return (sw & (FP_NAN | FP_NORMAL | FP_INFINITE | FP_ZERO | FP_SUBNORMAL))
-      == FP_NAN;
-  }
-
-
-#define isnan(x) (sizeof (x) == sizeof (float) ? __isnanf (x)	\
-  : sizeof (x) == sizeof (double) ? __isnan (x)	\
-  : __isnanl (x))
+#define isnan(x) (fpclassify(x) == FP_NAN)
 
   /* 7.12.3.5 */
 #define isnormal(x) (fpclassify(x) == FP_NORMAL)
 
-  /* 7.12.3.6 The signbit macro */
-  __CRT_INLINE int __cdecl __signbit (double x) {
-    unsigned short stw;
-    __asm__ ( "fxam; fstsw %%ax;": "=a" (stw) : "t" (x));
-    return stw & 0x0200;
-  }
-
-  __CRT_INLINE int __cdecl __signbitf (float x) {
-    unsigned short stw;
-    __asm__ ("fxam; fstsw %%ax;": "=a" (stw) : "t" (x));
-    return stw & 0x0200;
-  }
-
-  __CRT_INLINE int __cdecl __signbitl (long double x) {
-    unsigned short stw;
-    __asm__ ("fxam; fstsw %%ax;": "=a" (stw) : "t" (x));
-    return stw & 0x0200;
-  }
-
+/* implemented at tcc/tcc_libm.h */
 #define signbit(x) (sizeof (x) == sizeof (float) ? __signbitf (x)	\
   : sizeof (x) == sizeof (double) ? __signbit (x)	\
   : __signbitl (x))
@@ -620,18 +563,7 @@ extern "C" {
 
   /* 7.12.9.6 */
   /* round away from zero, regardless of fpu control word settings */
-  extern double __cdecl round (double);
-  extern float __cdecl roundf (float);
-  extern long double __cdecl roundl (long double);
-
-  /* 7.12.9.7  */
-  extern long __cdecl lround (double);
-  extern long __cdecl lroundf (float);
-  extern long __cdecl lroundl (long double);
-
-  extern long long __cdecl llround (double);
-  extern long long __cdecl llroundf (float);
-  extern long long __cdecl llroundl (long double);
+  /* implemented at tcc/tcc_libm.h */
 
   /* 7.12.9.8 */
   /* round towards zero, regardless of fpu control word settings */
@@ -684,16 +616,7 @@ extern "C" {
   NaN arguments are treated as missing data: if one argument is a NaN
   and the other numeric, then these functions choose the numeric
   value. */
-
-  /* 7.12.12.2 */
-  extern double __cdecl fmax  (double, double);
-  extern float __cdecl fmaxf (float, float);
-  extern long double __cdecl fmaxl (long double, long double);
-
-  /* 7.12.12.3 */
-  extern double __cdecl fmin (double, double);
-  extern float __cdecl fminf (float, float);
-  extern long double __cdecl fminl (long double, long double);
+  /* implemented at tcc/tcc_libm.h */
 
   /* 7.12.13.1 */
   /* return x * y + z as a ternary op */ 
@@ -777,5 +700,8 @@ extern "C++" {
  *  which always returns true: yes, (NaN != NaN) is true).
  */
 
+/* Mini libm (inlined fpclassify, fmin, fmax, round, and f/l variants) */
+#include "tcc/tcc_libm.h"
+
 #endif /* End _MATH_H_ */
 
diff --git a/win32/include/tcc/tcc_libm.h b/win32/include/tcc/tcc_libm.h
new file mode 100644
index 0000000..2e95c40
--- /dev/null
+++ b/win32/include/tcc/tcc_libm.h
@@ -0,0 +1,192 @@
+#ifndef _TCC_LIBM_H_
+#define _TCC_LIBM_H_
+
+#include "../include/math.h"
+
+/* TCC uses 8 bytes for double and long double, so effectively the l variants
+ * are never used. For now, they just run the normal (double) variant.
+ */
+
+/*
+ * most of the code in this file is taken from MUSL rs-1.0 (MIT license)
+ * - http://git.musl-libc.org/cgit/musl/tree/src/math?h=rs-1.0
+ * - MUSL's license: http://git.musl-libc.org/cgit/musl/tree/COPYRIGHT?h=rs-1.0
+ */
+
+/*******************************************************************************
+  Start of code based on MUSL
+*******************************************************************************/
+/*
+musl as a whole is licensed under the following standard MIT license:
+
+----------------------------------------------------------------------
+Copyright © 2005-2014 Rich Felker, et al.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+----------------------------------------------------------------------
+*/
+
+
+__CRT_INLINE int __cdecl __fpclassify (double x) {
+  union {double f; uint64_t i;} u = {x};
+  int e = u.i>>52 & 0x7ff;
+  if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
+  if (e==0x7ff) return u.i<<12 ? FP_NAN : FP_INFINITE;
+  return FP_NORMAL;
+}
+
+__CRT_INLINE int __cdecl __fpclassifyf (float x) {
+  union {float f; uint32_t i;} u = {x};
+  int e = u.i>>23 & 0xff;
+  if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
+  if (e==0xff) return u.i<<9 ? FP_NAN : FP_INFINITE;
+  return FP_NORMAL;
+}
+
+__CRT_INLINE int __cdecl __fpclassifyl (long double x) {
+  return __fpclassifyl(x);
+}
+
+__CRT_INLINE int __cdecl __signbit (double x) {
+  union {double d; uint64_t i;} y = { x };
+  return y.i>>63;
+}
+
+__CRT_INLINE int __cdecl __signbitf (float x) {
+  union {float f; uint32_t i; } y = { x };
+  return y.i>>31;
+}
+
+__CRT_INLINE int __cdecl __signbitl (long double x) {
+  return __signbit(x);
+}
+
+
+#define TCCFP_FMIN_EVAL (isnan(x) ? y :                                      \
+                         isnan(y) ? x :                                      \
+                         (signbit(x) != signbit(y)) ? (signbit(x) ? x : y) : \
+                         x < y ? x : y)
+
+__CRT_INLINE double __cdecl fmin (double x, double y) {
+  return TCCFP_FMIN_EVAL;
+}
+
+__CRT_INLINE float __cdecl fminf (float x, float y) {
+  return TCCFP_FMIN_EVAL;
+}
+
+__CRT_INLINE long double __cdecl fminl (long double x, long double y) {
+  return TCCFP_FMIN_EVAL;
+}
+
+#define TCCFP_FMAX_EVAL (isnan(x) ? y :                                      \
+                         isnan(y) ? x :                                      \
+                         (signbit(x) != signbit(y)) ? (signbit(x) ? y : x) : \
+                         x < y ? y : x)
+
+__CRT_INLINE double __cdecl fmax (double x, double y) {
+  return TCCFP_FMAX_EVAL;
+}
+
+__CRT_INLINE float __cdecl fmaxf (float x, float y) {
+  return TCCFP_FMAX_EVAL;
+}
+
+__CRT_INLINE long double __cdecl fmaxl (long double x, long double y) {
+  return TCCFP_FMAX_EVAL;
+}
+
+#define TCCFP_FORCE_EVAL(x) do {            \
+if (sizeof(x) == sizeof(float)) {           \
+  volatile float __x;                       \
+  __x = (x);                                \
+} else if (sizeof(x) == sizeof(double)) {   \
+  volatile double __x;                      \
+  __x = (x);                                \
+} else {                                    \
+  volatile long double __x;                 \
+  __x = (x);                                \
+}                                           \
+} while(0)
+
+__CRT_INLINE double __cdecl round (double x) {
+  union {double f; uint64_t i;} u = {x};
+  int e = u.i >> 52 & 0x7ff;
+  double y;
+
+  if (e >= 0x3ff+52)
+    return x;
+  if (u.i >> 63)
+    x = -x;
+  if (e < 0x3ff-1) {
+    /* raise inexact if x!=0 */
+    TCCFP_FORCE_EVAL(x + 0x1p52);
+    return 0*u.f;
+  }
+  y = (double)(x + 0x1p52) - 0x1p52 - x;
+  if (y > 0.5)
+    y = y + x - 1;
+  else if (y <= -0.5)
+    y = y + x + 1;
+  else
+    y = y + x;
+  if (u.i >> 63)
+    y = -y;
+  return y;
+}
+
+__CRT_INLINE long __cdecl lround (double x) {
+  return round(x);
+}
+
+__CRT_INLINE long long __cdecl llround (double x) {
+  return round(x);
+}
+
+__CRT_INLINE float __cdecl roundf (float x) {
+  return round(x);
+}
+
+__CRT_INLINE long __cdecl lroundf (float x) {
+  return round(x);
+}
+
+__CRT_INLINE long long __cdecl llroundf (float x) {
+  return round(x);
+}
+
+__CRT_INLINE long double __cdecl roundl (long double x) {
+  return round(x);
+}
+
+__CRT_INLINE long __cdecl lroundl (long double x) {
+  return round(x);
+}
+
+__CRT_INLINE long long __cdecl llroundl (long double x) {
+  return round(x);
+}
+
+
+/*******************************************************************************
+  End of code based on MUSL
+*******************************************************************************/
+
+#endif /* _TCC_LIBM_H_ */
-- 
2.6.2.windows.1

_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to