From 5a8173860d65182af022be88fed0c5d8d5dcb69d Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 12 Mar 2014 16:12:57 -0700 Subject: [PATCH] Ensure we always have symbols for atof, strtof, strtold. We'll need a better implementation of strtold for LP64, but all our long double functions are currently broken for LP64 anyway so this isn't a regression. Change-Id: I2bdebac11245d31521d5fa09a16331c03dc4339c --- libc/Android.mk | 7 +++++-- libc/bionic/atof.cpp | 35 +++++++++++++++++++++++++++++++++++ libc/bionic/strtof.cpp | 34 ++++++++++++++++++++++++++++++++++ libc/bionic/strtold.cpp | 34 ++++++++++++++++++++++++++++++++++ libc/include/stdlib.h | 20 +++++++------------- tests/stdlib_test.cpp | 16 ++++++++++++++++ 6 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 libc/bionic/atof.cpp create mode 100644 libc/bionic/strtof.cpp create mode 100644 libc/bionic/strtold.cpp diff --git a/libc/Android.mk b/libc/Android.mk index 11aac7ffd..df9ad210e 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -114,6 +114,7 @@ libc_bionic_src_files := \ bionic/abort.cpp \ bionic/access.cpp \ bionic/assert.cpp \ + bionic/atof.cpp \ bionic/__bionic_name_mem.cpp \ bionic/bionic_time_conversions.cpp \ bionic/brk.cpp \ @@ -188,8 +189,8 @@ libc_bionic_src_files := \ bionic/setegid.cpp \ bionic/__set_errno.cpp \ bionic/seteuid.cpp \ - bionic/setpgrp.cpp \ bionic/setlocale.cpp \ + bionic/setpgrp.cpp \ bionic/sigaction.cpp \ bionic/sigaddset.cpp \ bionic/sigdelset.cpp \ @@ -207,12 +208,14 @@ libc_bionic_src_files := \ bionic/strerror.cpp \ bionic/strerror_r.cpp \ bionic/strsignal.cpp \ + bionic/strtof.cpp \ + bionic/strtold.cpp \ bionic/stubs.cpp \ bionic/symlink.cpp \ bionic/sysconf.cpp \ - bionic/system_properties.cpp \ bionic/sys_siglist.c \ bionic/sys_signame.c \ + bionic/system_properties.cpp \ bionic/tdestroy.cpp \ bionic/termios.cpp \ bionic/thread_atexit.cpp \ diff --git a/libc/bionic/atof.cpp b/libc/bionic/atof.cpp new file mode 100644 index 000000000..e77ccdd5e --- /dev/null +++ b/libc/bionic/atof.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +double atof(const char* s) { + // Despite the 'f' in the name, this returns a double and is + // specified to be equivalent to strtod. + return strtod(s, NULL); +} diff --git a/libc/bionic/strtof.cpp b/libc/bionic/strtof.cpp new file mode 100644 index 000000000..a41e4d421 --- /dev/null +++ b/libc/bionic/strtof.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +float strtof(const char* s, char** end_ptr) { + // TODO: upgrade to a non-hack implementation. + return strtod(s, end_ptr); +} diff --git a/libc/bionic/strtold.cpp b/libc/bionic/strtold.cpp new file mode 100644 index 000000000..079f393d2 --- /dev/null +++ b/libc/bionic/strtold.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +long double strtold(const char* s, char** end_ptr) { + // TODO: this is fine for LP32 where double == long double, but is broken on LP64. + return strtod(s, end_ptr); +} diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index 9595170a3..9b7e6d16a 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -60,23 +60,17 @@ extern long strtol(const char *, char **, int); extern long long strtoll(const char *, char **, int); extern unsigned long strtoul(const char *, char **, int); extern unsigned long long strtoull(const char *, char **, int); -extern double strtod(const char *nptr, char **endptr); extern int posix_memalign(void **memptr, size_t alignment, size_t size); -static __inline__ float strtof(const char *nptr, char **endptr) -{ - return (float)strtod(nptr, endptr); -} +extern double atof(const char*); +extern double strtod(const char*, char**); +extern float strtof(const char*, char**); +extern long double strtold(const char*, char**); -extern int atoi(const char *) __purefunc; -extern long atol(const char *) __purefunc; -extern long long atoll(const char *) __purefunc; - -static __inline__ double atof(const char *nptr) -{ - return (strtod(nptr, NULL)); -} +extern int atoi(const char*) __purefunc; +extern long atol(const char*) __purefunc; +extern long long atoll(const char*) __purefunc; extern int abs(int) __pure2; extern long labs(long) __pure2; diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp index 083e86a63..7bdb685d5 100644 --- a/tests/stdlib_test.cpp +++ b/tests/stdlib_test.cpp @@ -186,3 +186,19 @@ TEST(stdlib, system) { ASSERT_TRUE(WIFEXITED(status)); ASSERT_EQ(1, WEXITSTATUS(status)); } + +TEST(stdlib, atof) { + ASSERT_EQ(1.23, atof("1.23")); +} + +TEST(stdlib, strtod) { + ASSERT_EQ(1.23, strtod("1.23", NULL)); +} + +TEST(stdlib, strtof) { + ASSERT_EQ(1.23, strtod("1.23", NULL)); +} + +TEST(stdlib, strtold) { + ASSERT_EQ(1.23, strtold("1.23", NULL)); +}