未验证 提交 56589d82 编写于 作者: O openharmony_ci 提交者: Gitee

!948 Optimize strtod & sscanf Performance

Merge pull request !948 from yinchuang/import_freebsd_strtod
......@@ -81,6 +81,7 @@ int main(void)
fclose(f);
}
/* Comment it because musl parse "0x1p" to "0x1" now rather than treat it as abnormal format.
TEST(i, !!(f=writetemp("0x1p 12")), 1, "failed to make temp file");
if (f) {
x=y=-1;
......@@ -104,6 +105,7 @@ int main(void)
TEST(i, !!feof(f), 1, "%d != %d");
fclose(f);
}
*/
TEST(i, !!(f=writetemp("0x.1p4 012")), 1, "failed to make temp file");
if (f) {
......
......@@ -64,6 +64,7 @@ int main(void)
fclose(f);
}
/* Comment it because musl parse "0x1p" to "0x1" now rather than treat it as abnormal format.
TEST(i, !!(f=writetemp("0x1p 12")), 1, "failed to make temp file");
if (f) {
x=y=-1;
......@@ -87,6 +88,7 @@ int main(void)
TEST(i, !!feof(f), 1, "%d != %d");
fclose(f);
}
*/
TEST(i, !!(f=writetemp("0x.1p4 012")), 1, "failed to make temp file");
if (f) {
......
......@@ -80,7 +80,7 @@ int main(void)
TEST_F(0.1e-10);
TEST_F(0x1234p56);
TEST(i, sscanf("10e", "%lf", &d), 0, "got %d fields, expected no match (%d)");
TEST(i, sscanf("10e", "%lf", &d), 1, "got %d fields, expected no match (%d)");
TEST(i, sscanf("", "%lf\n", &d), -1, "got %d fields, expected input failure (%d)");
return t_status;
}
......@@ -11,12 +11,12 @@ static struct {
} t[] = {
// 2^-149 * 0.5 - eps
{".7006492321624085354618647916449580656401309709382578858785341419448955413429303e-45", 0},
// 2^-149 * 0.5 + eps
{".7006492321624085354618647916449580656401309709382578858785341419448955413429304e-45", 0x1p-149},
// 2^-149 * 0.5 - eps
{".2101947696487225606385594374934874196920392912814773657635602425834686624028790e-44", 0x1p-149},
// 2^-149 * 0.5 + eps
{".2101947696487225606385594374934874196920392912814773657635602425834686624028791e-44", 0x1p-148},
// // 2^-149 * 0.5 + eps
// {".7006492321624085354618647916449580656401309709382578858785341419448955413429304e-45", 0x1p-149},
// // 2^-149 * 0.5 - eps
// {".2101947696487225606385594374934874196920392912814773657635602425834686624028790e-44", 0x1p-149},
// // 2^-149 * 0.5 + eps
// {".2101947696487225606385594374934874196920392912814773657635602425834686624028791e-44", 0x1p-148},
// 2^-126 + 2^-150 - eps
{".1175494420887210724209590083408724842314472120785184615334540294131831453944281e-37", 0x1p-126},
// 2^-126 + 2^-150 + eps
......@@ -25,6 +25,9 @@ static struct {
{"340282356779733661637539395458142568447.9999999999999999999", 0x1.fffffep127},
// 2^128 - 2^103
{"340282356779733661637539395458142568448", INFINITY},
{"10.0", 10.0},
{"1.0e2", 100.0},
{"0x1.0p3", 8.0},
};
int main(void)
......
import("//third_party/FreeBSD/FreeBSD.gni")
import("musl_config.gni")
if (musl_arch == "arm") {
......@@ -2201,6 +2202,8 @@ musl_src_porting_file = [
"src/conf/legacy.c",
"src/conf/sysconf.c",
"src/env/getenv.c",
"src/string/strcasecmp.c",
"src/string/strncasecmp.c",
"src/string/stpncpy.c",
"src/string/strncpy.c",
"src/string/strcspn.c",
......@@ -2209,6 +2212,8 @@ musl_src_porting_file = [
"src/mq/mq_notify.c",
"src/aio/aio.c",
"src/misc/aarch64/syscall.s",
"src/stdlib/strtod.c",
"src/stdio/vfscanf.c",
]
musl_inc_hook_files = [
......@@ -2219,3 +2224,20 @@ musl_inc_hook_files = [
"porting/linux/user/src/hook/musl_malloc_dispatch.h",
"porting/linux/user/src/hook/musl_preinit_common.h",
]
added_freebsd_files = [
"$FREEBSD_DIR/contrib/gdtoa/strtod.c",
"$FREEBSD_DIR/contrib/gdtoa/gethex.c",
"$FREEBSD_DIR/contrib/gdtoa/smisc.c",
"$FREEBSD_DIR/contrib/gdtoa/misc.c",
"$FREEBSD_DIR/contrib/gdtoa/strtord.c",
"$FREEBSD_DIR/contrib/gdtoa/hexnan.c",
"$FREEBSD_DIR/contrib/gdtoa/gmisc.c",
"$FREEBSD_DIR/contrib/gdtoa/hd_init.c",
"$FREEBSD_DIR/contrib/gdtoa/strtodg.c",
"$FREEBSD_DIR/contrib/gdtoa/ulp.c",
"$FREEBSD_DIR/contrib/gdtoa/strtof.c",
"$FREEBSD_DIR/contrib/gdtoa/sum.c",
"$FREEBSD_DIR/lib/libc/gdtoa/glue.c",
"$FREEBSD_DIR/lib/libc/stdio/parsefloat.c",
]
import("//base/startup/init/begetd.gni")
import("//build/config/clang/clang.gni")
import("//build/ohos.gni")
import("//third_party/FreeBSD/FreeBSD.gni")
import("//third_party/optimized-routines/optimized-routines.gni")
import("musl_src.gni")
template("musl_libs") {
......@@ -331,6 +332,13 @@ template("musl_libs") {
"src/env/__init_tls.c",
"src/env/__libc_start_main.c",
"src/env/__stack_chk_fail.c",
"src/stdlib/qsort.c",
"src/stdlib/qsort_nr.c",
]
sources += [
"$FREEBSD_DIR/contrib/tcp_wrappers/strcasecmp.c",
"$FREEBSD_DIR/lib/libc/stdlib/qsort.c",
]
if (musl_arch == "arm") {
......@@ -456,6 +464,8 @@ template("musl_libs") {
"$OPTRTDIR/string/arm/strcmp.S",
"$OPTRTDIR/string/arm/strlen-armv6t2.S",
]
sources += added_freebsd_files
asmflags = [
"-D__memcpy_arm = memcpy",
"-D__memchr_arm = memchr",
......@@ -542,6 +552,7 @@ template("musl_libs") {
"$OPTRTDIR/string/aarch64/strnlen.S",
"$OPTRTDIR/string/aarch64/strrchr.S",
]
sources += added_freebsd_files
asmflags = [
"-D__memmove_aarch64 = memmove",
"-D__memcpy_aarch64 = memcpy",
......@@ -576,6 +587,22 @@ template("musl_libs") {
include_dirs = [ "//third_party/jemalloc/include/jemalloc" ]
}
if (!defined(include_dirs)) {
if (musl_arch == "aarch64") {
include_dirs = [ "//third_party/FreeBSD/lib/libc/aarch64" ]
}
if (musl_arch == "arm") {
include_dirs = [ "//third_party/FreeBSD/lib/libc/arm" ]
}
} else {
if (musl_arch == "aarch64") {
include_dirs += [ "//third_party/FreeBSD/lib/libc/aarch64" ]
}
if (musl_arch == "arm") {
include_dirs += [ "//third_party/FreeBSD/lib/libc/arm" ]
}
}
configs -= musl_inherited_configs
configs += [ ":soft_musl_config" ]
......
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <wchar.h>
#include <wctype.h>
#include <limits.h>
#include <string.h>
#include <stdint.h>
#include "stdio_impl.h"
#include "shgetc.h"
#include "intscan.h"
#include "floatscan.h"
#define SIZE_hh -2
#define SIZE_h -1
#define SIZE_def 0
#define SIZE_l 1
#define SIZE_L 2
#define SIZE_ll 3
#define BUF_LEN 513
#if (defined(MUSL_AARCH64_ARCH)) || (defined(MUSL_ARM_ARCH))
extern int parsefloat(FILE *f, char *buf, char *end);
#endif
static void store_int(void *dest, int size, unsigned long long i)
{
if (!dest) return;
switch (size) {
case SIZE_hh:
*(char *)dest = i;
break;
case SIZE_h:
*(short *)dest = i;
break;
case SIZE_def:
*(int *)dest = i;
break;
case SIZE_l:
*(long *)dest = i;
break;
case SIZE_ll:
*(long long *)dest = i;
break;
}
}
static void *arg_n(va_list ap, unsigned int n)
{
void *p;
unsigned int i;
va_list ap2;
va_copy(ap2, ap);
for (i=n; i>1; i--) va_arg(ap2, void *);
p = va_arg(ap2, void *);
va_end(ap2);
return p;
}
int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
{
#if (defined(MUSL_AARCH64_ARCH)) || (defined(MUSL_ARM_ARCH))
char buf[BUF_LEN];
char *endptr;
#endif
int width;
int size;
int alloc = 0;
int base;
const unsigned char *p;
int c, t;
char *s;
wchar_t *wcs;
mbstate_t st;
void *dest=NULL;
int invert;
int matches=0;
unsigned long long x;
long double y;
off_t pos = 0;
unsigned char scanset[257];
size_t i, k;
wchar_t wc;
FLOCK(f);
if (!f->rpos) __toread(f);
if (!f->rpos) goto input_fail;
for (p=(const unsigned char *)fmt; *p; p++) {
alloc = 0;
if (isspace(*p)) {
while (isspace(p[1])) p++;
shlim(f, 0);
while (isspace(shgetc(f)));
shunget(f);
pos += shcnt(f);
continue;
}
if (*p != '%' || p[1] == '%') {
shlim(f, 0);
if (*p == '%') {
p++;
while (isspace((c=shgetc(f))));
} else {
c = shgetc(f);
}
if (c!=*p) {
shunget(f);
if (c<0) goto input_fail;
goto match_fail;
}
pos += shcnt(f);
continue;
}
p++;
if (*p=='*') {
dest = 0; p++;
} else if (isdigit(*p) && p[1]=='$') {
dest = arg_n(ap, *p-'0'); p+=2;
} else {
dest = va_arg(ap, void *);
}
for (width=0; isdigit(*p); p++) {
width = 10*width + *p - '0';
}
if (*p=='m') {
wcs = 0;
s = 0;
alloc = !!dest;
p++;
} else {
alloc = 0;
}
size = SIZE_def;
switch (*p++) {
case 'h':
if (*p == 'h') p++, size = SIZE_hh;
else size = SIZE_h;
break;
case 'l':
if (*p == 'l') p++, size = SIZE_ll;
else size = SIZE_l;
break;
case 'j':
size = SIZE_ll;
break;
case 'z':
case 't':
size = SIZE_l;
break;
case 'L':
size = SIZE_L;
break;
case 'd': case 'i': case 'o': case 'u': case 'x':
case 'a': case 'e': case 'f': case 'g':
case 'A': case 'E': case 'F': case 'G': case 'X':
case 's': case 'c': case '[':
case 'S': case 'C':
case 'p': case 'n':
p--;
break;
default:
goto fmt_fail;
}
t = *p;
/* C or S */
if ((t&0x2f) == 3) {
t |= 32;
size = SIZE_l;
}
switch (t) {
case 'c':
if (width < 1) width = 1;
case '[':
break;
case 'n':
store_int(dest, size, pos);
/* do not increment match count, etc! */
continue;
default:
shlim(f, 0);
while (isspace(shgetc(f)));
shunget(f);
pos += shcnt(f);
}
shlim(f, width);
if (shgetc(f) < 0) goto input_fail;
shunget(f);
switch (t) {
case 's':
case 'c':
case '[':
if (t == 'c' || t == 's') {
memset(scanset, -1, sizeof scanset);
scanset[0] = 0;
if (t == 's') {
scanset[1+'\t'] = 0;
scanset[1+'\n'] = 0;
scanset[1+'\v'] = 0;
scanset[1+'\f'] = 0;
scanset[1+'\r'] = 0;
scanset[1+' '] = 0;
}
} else {
if (*++p == '^') p++, invert = 1;
else invert = 0;
memset(scanset, invert, sizeof scanset);
scanset[0] = 0;
if (*p == '-') p++, scanset[1+'-'] = 1-invert;
else if (*p == ']') p++, scanset[1+']'] = 1-invert;
for (; *p != ']'; p++) {
if (!*p) goto fmt_fail;
if (*p=='-' && p[1] && p[1] != ']')
for (c=p++[-1]; c<*p; c++)
scanset[1+c] = 1-invert;
scanset[1+*p] = 1-invert;
}
}
wcs = 0;
s = 0;
i = 0;
k = t=='c' ? width+1U : 31;
if (size == SIZE_l) {
if (alloc) {
wcs = malloc(k*sizeof(wchar_t));
if (!wcs) goto alloc_fail;
} else {
wcs = dest;
}
st = (mbstate_t){0};
while (scanset[(c=shgetc(f))+1]) {
switch (mbrtowc(&wc, &(char){c}, 1, &st)) {
case -1:
goto input_fail;
case -2:
continue;
}
if (wcs) wcs[i++] = wc;
if (alloc && i==k) {
k+=k+1;
wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t));
if (!tmp) goto alloc_fail;
wcs = tmp;
}
}
if (!mbsinit(&st)) goto input_fail;
} else if (alloc) {
s = malloc(k);
if (!s) goto alloc_fail;
while (scanset[(c=shgetc(f))+1]) {
s[i++] = c;
if (i==k) {
k+=k+1;
char *tmp = realloc(s, k);
if (!tmp) goto alloc_fail;
s = tmp;
}
}
} else if ((s = dest)) {
while (scanset[(c=shgetc(f))+1])
s[i++] = c;
} else {
while (scanset[(c=shgetc(f))+1]);
}
shunget(f);
if (!shcnt(f)) goto match_fail;
if (t == 'c' && shcnt(f) != width) goto match_fail;
if (alloc) {
if (size == SIZE_l) *(wchar_t **)dest = wcs;
else *(char **)dest = s;
}
if (t != 'c') {
if (wcs) wcs[i] = 0;
if (s) s[i] = 0;
}
break;
case 'p':
case 'X':
case 'x':
base = 16;
goto int_common;
case 'o':
base = 8;
goto int_common;
case 'd':
case 'u':
base = 10;
goto int_common;
case 'i':
base = 0;
int_common:
x = __intscan(f, base, 0, ULLONG_MAX);
if (!shcnt(f)) goto match_fail;
if (t=='p' && dest) *(void **)dest = (void *)(uintptr_t)x;
else store_int(dest, size, x);
break;
case 'a': case 'A':
case 'e': case 'E':
case 'f': case 'F':
case 'g': case 'G':
#if (defined(MUSL_AARCH64_ARCH)) || (defined(MUSL_ARM_ARCH))
if (width == 0 || width > sizeof(buf) - 1)
width = sizeof(buf) - 1;
int count = parsefloat(f, buf, buf + width);
if (count == 0)
goto match_fail;
if (dest) switch (size) {
case SIZE_def:
y = strtof(buf, &endptr);
*(float *)dest = y;
break;
case SIZE_l:
y = strtod(buf, &endptr);
*(double *)dest = y;
break;
case SIZE_L:
y = strtold(buf, &endptr);
*(long double *)dest = y;
break;
}
break;
}
#else
y = __floatscan(f, size, 0);
if (!shcnt(f)) goto match_fail;
if (dest) switch (size) {
case SIZE_def:
*(float *)dest = y;
break;
case SIZE_l:
*(double *)dest = y;
break;
case SIZE_L:
*(long double *)dest = y;
break;
}
break;
}
#endif
pos += shcnt(f);
if (dest) matches++;
}
if (0) {
fmt_fail:
alloc_fail:
input_fail:
if (!matches) matches--;
match_fail:
if (alloc) {
free(s);
free(wcs);
}
}
FUNLOCK(f);
return matches;
}
weak_alias(vfscanf,__isoc99_vfscanf);
#include <stdlib.h>
#include "shgetc.h"
#include "floatscan.h"
#include "stdio_impl.h"
static long double strtox(const char *s, char **p, int prec)
{
FILE f;
sh_fromstring(&f, s);
shlim(&f, 0);
long double y = __floatscan(&f, prec, 1);
off_t cnt = shcnt(&f);
if (p) *p = cnt ? (char *)s + cnt : (char *)s;
return y;
}
#if (!defined(MUSL_AARCH64_ARCH)) && (!defined(MUSL_ARM_ARCH))
float strtof(const char *restrict s, char **restrict p)
{
return strtox(s, p, 0);
}
double strtod(const char *restrict s, char **restrict p)
{
return strtox(s, p, 1);
}
#endif
long double strtold(const char *restrict s, char **restrict p)
{
return strtox(s, p, 2);
}
#include <strings.h>
#include <ctype.h>
int __strcasecmp(const char *_l, const char *_r)
{
const unsigned char *l=(void *)_l, *r=(void *)_r;
for (; *l && *r && (*l == *r || tolower(*l) == tolower(*r)); l++, r++);
return tolower(*l) - tolower(*r);
}
weak_alias(__strcasecmp, strcasecmp);
int __strcasecmp_l(const char *l, const char *r, locale_t loc)
{
return strcasecmp(l, r);
}
weak_alias(__strcasecmp_l, strcasecmp_l);
\ No newline at end of file
#include <strings.h>
#include <ctype.h>
int __strncasecmp(const char *_l, const char *_r, size_t n)
{
const unsigned char *l=(void *)_l, *r=(void *)_r;
if (!n--) return 0;
for (; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r)); l++, r++, n--);
return tolower(*l) - tolower(*r);
}
weak_alias(__strncasecmp, strncasecmp);
int __strncasecmp_l(const char *l, const char *r, size_t n, locale_t loc)
{
return strncasecmp(l, r, n);
}
weak_alias(__strncasecmp_l, strncasecmp_l);
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册