This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 583f335a520 include/cxx/{cstdlib,cmath}: Provide std::abs overloads
583f335a520 is described below

commit 583f335a52061b6f50c409b7eff283dc7427a1f8
Author: Moritz Pflanzer <[email protected]>
AuthorDate: Mon Aug 17 18:49:55 2026 +0200

    include/cxx/{cstdlib,cmath}: Provide std::abs overloads
    
    The overloads of std::abs are defined in cstdlib and cmath according to
    the C++ standard.
    
    Without these new definitions the `int std::abs(int)` function was
    selected for all argument types resulting in a truncation of the return
    values.
    
    Signed-off-by: Moritz Pflanzer <[email protected]>
---
 include/cxx/cmath           |  1 +
 include/cxx/cstdlib         |  1 +
 include/cxx/nuttx/std_abs.h | 95 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 97 insertions(+)

diff --git a/include/cxx/cmath b/include/cxx/cmath
index a2fc0844295..2097fc30e4e 100644
--- a/include/cxx/cmath
+++ b/include/cxx/cmath
@@ -40,6 +40,7 @@
 #else
 
 #  include <math.h>
+#  include <nuttx/std_abs.h>
 
 #  if !defined(FLT_EVAL_METHOD) && defined(__FLT_EVAL_METHOD__)
 #    define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
diff --git a/include/cxx/cstdlib b/include/cxx/cstdlib
index ff6e41bc37d..3c661027ae6 100644
--- a/include/cxx/cstdlib
+++ b/include/cxx/cstdlib
@@ -29,6 +29,7 @@
 
 #include <nuttx/config.h>
 #include <stdlib.h>
+#include <nuttx/std_abs.h>
 
 //***************************************************************************
 // Namespace
diff --git a/include/cxx/nuttx/std_abs.h b/include/cxx/nuttx/std_abs.h
new file mode 100644
index 00000000000..4353a392337
--- /dev/null
+++ b/include/cxx/nuttx/std_abs.h
@@ -0,0 +1,95 @@
+/****************************************************************************
+ * include/cxx/nuttx/std_abs.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_CXX_BITS_STD_ABS
+#define __INCLUDE_CXX_BITS_STD_ABS
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#undef abs
+
+/****************************************************************************
+ * External functions
+ ****************************************************************************/
+
+extern "C"
+{
+  extern int abs(int);
+  extern long labs(long);
+  extern long long llabs(long long);
+
+#ifdef CONFIG_HAVE_FLOAT
+  extern float fabsf(float);
+#endif
+#ifdef CONFIG_HAVE_DOUBLE
+  extern double fabs(double);
+#endif
+#ifdef CONFIG_HAVE_LONG_DOUBLE
+  extern long double fabsl(long double);
+#endif
+}
+
+/****************************************************************************
+ * Namespace
+ ****************************************************************************/
+
+namespace std
+{
+    using ::abs;
+
+    inline long abs(long x)
+    {
+      return ::labs(x);
+    }
+
+    inline long long abs(long long x)
+    {
+      return ::llabs(x);
+    }
+
+#ifdef CONFIG_HAVE_FLOAT
+    inline float abs(float x)
+    {
+      return ::fabsf(x);
+    }
+#endif
+
+#ifdef CONFIG_HAVE_DOUBLE
+    inline double abs(double x)
+    {
+      return ::fabs(x);
+    }
+#endif
+
+#ifdef CONFIG_HAVE_LONG_DOUBLE
+    inline long double abs(long double x)
+    {
+      return ::fabsl(x);
+    }
+#endif
+}
+
+#endif /* __INCLUDE_CXX_BITS_STD_ABS */

Reply via email to