Any thoughts on this? The proposed patch has been applied to newlib's math.h.

-- Jeff J.

-------- Original Message --------
Subject: OpenMP vs. <math.h>
Date: Fri, 08 Jan 2010 12:42:35 -0600
From: Yaakov (Cygwin/X) <yselkow...@users.sourceforge.net>
To: new...@sources.redhat.com

POSIX allows for <math.h> functions to also be defined as macros.
Currently, only log2 and log2f are so defined.

These macros pose problems with a few projects which define their own
static/inline/template log2() (off the top of my head, I can think of 2:
the CRAN rgl module, and OpenCV; both are C++).  Of course, those can be
fixed with an #undef log2 after the #include's.

However, I just encountered tonight a much larger conflict: OpenMP/C++
and <math.h> are incompatible.  STC attached:

$ g++ -D_GLIBCXX_PARALLEL -fopenmp openmp.cxx -lgomp
In file included from
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/parallel/algobase.h:46,
                   from
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algobase.h:1137,
                   from
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/char_traits.h:46,
                   from
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/ios:46,
                   from
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/ostream:45,
                   from
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/iostream:45,
                   from openmp.cxx:7:
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/parallel/base.h:112:
error: expected `)' before ‘/’ token

It gets even worse if you #include <algorithm> as well, and no, it
doesn't help to #include <cmath>, although moving the math include after
all other includes does work.

As this would seem to affect all Newlib platforms, I think the most
plausible solution is to make the log2 macros dependent on !__cplusplus.
  Patch attached.


Yaakov
Cygwin/X


// http://en.wikipedia.org/wiki/OpenMP#Hello_World
// with an #include <math.h>
// if you also #include <algorithm>, this *really* blows up

#include <math.h>
#include <omp.h>
#include <iostream>

int main (int argc, char *argv[]) {
        int th_id, nthreads;
#pragma omp parallel private(th_id)
        {
                th_id = omp_get_thread_num();
                std::cout << "Hello World from thread" << th_id << "\n";
#pragma omp barrier
                if ( th_id == 0 ) {
                        nthreads = omp_get_num_threads();
                        std::cout << "There are " << nthreads << " threads\n";
                }
        }
        return 0;
}

2010-01-08  Yaakov Selkowitz  <yselkow...@users.sourceforge.net>

	* libc/include/math.h (log2, log2f): Disable macro versions for C++,
	as they are incompatible with OpenMP/C++ headers.

Index: libc/include/math.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/math.h,v
retrieving revision 1.44
diff -u -r1.44 math.h
--- libc/include/math.h	17 Nov 2009 22:35:46 -0000	1.44
+++ libc/include/math.h	8 Jan 2010 06:49:14 -0000
@@ -275,7 +275,9 @@
 extern double erf _PARAMS((double));
 extern double erfc _PARAMS((double));
 extern double log2 _PARAMS((double));
+#if !defined(__cplusplus)
 #define log2(x) (log (x) / _M_LOG2_E)
+#endif
 
 #ifndef __math_68881
 extern double hypot _PARAMS((double, double));
@@ -353,7 +355,9 @@
 extern float erff _PARAMS((float));
 extern float erfcf _PARAMS((float));
 extern float log2f _PARAMS((float));
+#if !defined(__cplusplus)
 #define log2f(x) (logf (x) / (float) _M_LOG2_E)
+#endif
 extern float hypotf _PARAMS((float, float));
 #endif /* ! defined (_REENT_ONLY) */
 

Reply via email to