From 3643749b0652684231f2c3d7b522abac4adc239d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bernhard=20=C3=9Cbelacker?= <bernhardu@vr-web.de>
Date: Fri, 14 Aug 2015 04:44:07 +0200
Subject: msvcr120: Implement strtof and _strtof_l (try 3)

SuperTux 0.3.5a needs msvcr120.strtof.
Found during research for bug #39034.

Changes try 3:
- Avoid almost_equal with float, instead use compare_float from ddraw7 tests
  (thanks Henri Verbeet)
- Used functions should be available in all versions of msvcr120;
  therefore avoid the win_skips. (thanks Piotr Caban)

Changes try 2:
- In the test replaced the bit pattern comparison to detect
  infinity by msvcr120._finite
  (thanks Piotr Caban; _finitef is just available at arm and x86_64)

A note to the tests:
It seems because the functions get dynamically loaded the functions
_errno() and p__errno() returning different variables.
Therefore using also the dynamically loaded p__errno() in tests.
The executable built via make crosstest is linked against msvcrt.dll.
---
 dlls/msvcr120/msvcr120.spec         |  4 +--
 dlls/msvcr120/tests/msvcr120.c      | 69 +++++++++++++++++++++++++++++++++++++
 dlls/msvcr120_app/msvcr120_app.spec |  4 +--
 dlls/msvcrt/string.c                | 16 +++++++++
 4 files changed, 89 insertions(+), 4 deletions(-)

diff --git a/dlls/msvcr120/msvcr120.spec b/dlls/msvcr120/msvcr120.spec
index 9931820..330a83f 100644
--- a/dlls/msvcr120/msvcr120.spec
+++ b/dlls/msvcr120/msvcr120.spec
@@ -1731,7 +1731,7 @@
 @ cdecl _strtime(ptr) MSVCRT__strtime
 @ cdecl _strtime_s(ptr long)
 @ cdecl _strtod_l(str ptr ptr) MSVCRT_strtod_l
-@ stub _strtof_l
+@ cdecl _strtof_l(str ptr ptr) MSVCRT_strtof_l
 @ cdecl -ret64 _strtoi64(str ptr long) MSVCRT_strtoi64
 @ cdecl -ret64 _strtoi64_l(str ptr long ptr) MSVCRT_strtoi64_l
 @ stub _strtoimax_l
@@ -2386,7 +2386,7 @@
 @ cdecl strspn(str str) ntdll.strspn
 @ cdecl strstr(str str) MSVCRT_strstr
 @ cdecl strtod(str ptr) MSVCRT_strtod
-@ stub strtof
+@ cdecl strtof(str ptr) MSVCRT_strtof
 @ stub strtoimax
 @ cdecl strtok(str str) MSVCRT_strtok
 @ cdecl strtok_s(ptr str ptr) MSVCRT_strtok_s
diff --git a/dlls/msvcr120/tests/msvcr120.c b/dlls/msvcr120/tests/msvcr120.c
index aa78c4f..bce8aff 100644
--- a/dlls/msvcr120/tests/msvcr120.c
+++ b/dlls/msvcr120/tests/msvcr120.c
@@ -21,6 +21,8 @@
 #include <stdlib.h>
 #include <wchar.h>
 #include <stdio.h>
+#include <float.h>
+#include <limits.h>
 
 #include <windef.h>
 #include <winbase.h>
@@ -73,6 +75,11 @@ struct MSVCRT_lconv
     wchar_t* _W_negative_sign;
 };
 
+static inline BOOL almost_equal(float d1, float d2)
+{
+    return (d1-d2 > -1e-15 && d1-d2 < 1e-15);
+}
+
 static char* (CDECL *p_setlocale)(int category, const char* locale);
 static struct MSVCRT_lconv* (CDECL *p_localeconv)(void);
 static size_t (CDECL *p_wcstombs_s)(size_t *ret, char* dest, size_t sz, const wchar_t* src, size_t max);
@@ -83,6 +90,8 @@ static wchar_t** (CDECL *p____lc_locale_name_func)(void);
 static unsigned int (CDECL *p__GetConcurrency)(void);
 static void* (CDECL *p__W_Gettnames)(void);
 static void (CDECL *p_free)(void*);
+static float (CDECL *p__strtof_l)(const char *, char **, _locale_t);
+static float (CDECL *p_strtof)(const char *, char **);
 
 static BOOL init(void)
 {
@@ -105,6 +114,8 @@ static BOOL init(void)
     p__GetConcurrency = (void*)GetProcAddress(module,"?_GetConcurrency@details@Concurrency@@YAIXZ");
     p__W_Gettnames = (void*)GetProcAddress(module, "_W_Gettnames");
     p_free = (void*)GetProcAddress(module, "free");
+    p__strtof_l = (void*)GetProcAddress(module, "_strtof_l");
+    p_strtof = (void*)GetProcAddress(module, "strtof");
     return TRUE;
 }
 
@@ -321,6 +332,63 @@ static void test__W_Gettnames(void)
     p_setlocale(LC_ALL, "C");
 }
 
+static void test__strtof(void)
+{
+    const char float1[] = "12.1";
+    const char float2[] = "3.402823466e+38";          /* FLT_MAX */
+    const char float3[] = "-3.402823466e+38";
+    const char float4[] = "1.7976931348623158e+308";  /* DBL_MAX */
+    const char float5[] = "-1.7976931348623158e+308";
+
+    char *end;
+    float f;
+
+    f = FLT_MAX * FLT_MAX;
+    ok(_finite(f) == 0, "_finite failed\n");
+
+    /* strtof */
+    f = p_strtof(float1, &end);
+    ok(almost_equal(f, 12.1), "f = %lf\n", f);
+    ok(end == float1+4, "incorrect end (%d)\n", (int)(end-float1));
+
+    f = p_strtof(float2, &end);
+    ok(almost_equal(f, FLT_MAX), "f = %lf\n", f);
+    ok(end == float2+15, "incorrect end (%d)\n", (int)(end-float2));
+
+    f = p_strtof(float3, &end);
+    ok(almost_equal(f, -FLT_MAX), "f = %lf\n", f);
+    ok(end == float3+16, "incorrect end (%d)\n", (int)(end-float3));
+
+    f = p_strtof(float4, &end);
+    ok(_finite(f) == 0, "f = %lf\n", f);
+    ok(end == float4+23, "incorrect end (%d)\n", (int)(end-float4));
+
+    f = p_strtof(float5, &end);
+    ok(_finite(f) == 0, "f = %lf\n", f);
+    ok(end == float5+24, "incorrect end (%d)\n", (int)(end-float5));
+
+    /* _strtof_l */
+    f = p__strtof_l(float1, &end, NULL);
+    ok(almost_equal(f, 12.1), "f = %lf\n", f);
+    ok(end == float1+4, "incorrect end (%d)\n", (int)(end-float1));
+
+    f = p__strtof_l(float2, &end, NULL);
+    ok(almost_equal(f, FLT_MAX), "f = %lf\n", f);
+    ok(end == float2+15, "incorrect end (%d)\n", (int)(end-float2));
+
+    f = p__strtof_l(float3, &end, NULL);
+    ok(almost_equal(f, -FLT_MAX), "f = %lf\n", f);
+    ok(end == float3+16, "incorrect end (%d)\n", (int)(end-float3));
+
+    f = p__strtof_l(float4, &end, NULL);
+    ok(_finite(f) == 0, "f = %lf\n", f);
+    ok(end == float4+23, "incorrect end (%d)\n", (int)(end-float4));
+
+    f = p__strtof_l(float5, &end, NULL);
+    ok(_finite(f) == 0, "f = %lf\n", f);
+    ok(end == float5+24, "incorrect end (%d)\n", (int)(end-float5));
+}
+
 START_TEST(msvcr120)
 {
     if (!init()) return;
@@ -330,4 +398,5 @@ START_TEST(msvcr120)
     test____lc_locale_name_func();
     test__GetConcurrency();
     test__W_Gettnames();
+    test__strtof();
 }
diff --git a/dlls/msvcr120_app/msvcr120_app.spec b/dlls/msvcr120_app/msvcr120_app.spec
index 50066ed..28c929d 100644
--- a/dlls/msvcr120_app/msvcr120_app.spec
+++ b/dlls/msvcr120_app/msvcr120_app.spec
@@ -1442,7 +1442,7 @@
 @ cdecl _strtime(ptr) msvcr120._strtime
 @ cdecl _strtime_s(ptr long) msvcr120._strtime_s
 @ cdecl _strtod_l(str ptr ptr) msvcr120._strtod_l
-@ stub _strtof_l
+@ cdecl _strtof_l(str ptr ptr) msvcr120._strtof_l
 @ cdecl -ret64 _strtoi64(str ptr long) msvcr120._strtoi64
 @ cdecl -ret64 _strtoi64_l(str ptr long ptr) msvcr120._strtoi64_l
 @ stub _strtoimax_l
@@ -2049,7 +2049,7 @@
 @ cdecl strspn(str str) msvcr120.strspn
 @ cdecl strstr(str str) msvcr120.strstr
 @ cdecl strtod(str ptr) msvcr120.strtod
-@ stub strtof
+@ cdecl strtof(str ptr) msvcr120.strtof
 @ stub strtoimax
 @ cdecl strtok(str str) msvcr120.strtok
 @ cdecl strtok_s(ptr str ptr) msvcr120.strtok_s
diff --git a/dlls/msvcrt/string.c b/dlls/msvcrt/string.c
index f44ac52..9961549 100644
--- a/dlls/msvcrt/string.c
+++ b/dlls/msvcrt/string.c
@@ -497,6 +497,22 @@ double CDECL MSVCRT_strtod( const char *str, char **end )
 }
 
 /*********************************************************************
+ *		strtof_l  (MSVCRT.@)
+ */
+float CDECL MSVCRT_strtof_l( const char *str, char **end, MSVCRT__locale_t locale )
+{
+    return MSVCRT_strtod_l(str, end, locale);
+}
+
+/*********************************************************************
+ *		strtof  (MSVCRT.@)
+ */
+float CDECL MSVCRT_strtof( const char *str, char **end )
+{
+    return MSVCRT_strtof_l(str, end, NULL);
+}
+
+/*********************************************************************
  *		atof  (MSVCRT.@)
  */
 double CDECL MSVCRT_atof( const char *str )
-- 
2.6.4

