From fe26c6a2de1169af5f49866cb5cd17a12f8a831a Mon Sep 17 00:00:00 2001
From: isucan <isucan@253336fb-580f-4252-a368-f3cef5a2a82b>
Date: Thu, 23 Aug 2012 17:01:23 +0000
Subject: [PATCH] checking for sse

git-svn-id: https://kforge.ros.org/fcl/fcl_ros@167 253336fb-580f-4252-a368-f3cef5a2a82b
---
 trunk/fcl/CMakeLists.txt                      | 33 ++++----
 trunk/fcl/CMakeModules/FCLCheckSSE.cmake      | 83 +++++++++++++++++++
 trunk/fcl/include/fcl/CMakeLists.txt          |  2 +
 .../include/fcl/{simd/simd.h => config.h.in}  | 11 ++-
 trunk/fcl/include/fcl/math/vec_3f.h           | 16 ++--
 trunk/fcl/src/CMakeLists.txt                  | 50 +++++++++++
 6 files changed, 169 insertions(+), 26 deletions(-)
 create mode 100644 trunk/fcl/CMakeModules/FCLCheckSSE.cmake
 create mode 100644 trunk/fcl/include/fcl/CMakeLists.txt
 rename trunk/fcl/include/fcl/{simd/simd.h => config.h.in} (91%)
 create mode 100644 trunk/fcl/src/CMakeLists.txt

diff --git a/trunk/fcl/CMakeLists.txt b/trunk/fcl/CMakeLists.txt
index 24d288ac..35019d17 100644
--- a/trunk/fcl/CMakeLists.txt
+++ b/trunk/fcl/CMakeLists.txt
@@ -7,19 +7,25 @@ project(fcl CXX C)
 #  Release        : w/o debug symbols, w/ optimization
 #  RelWithDebInfo : w/ debug symbols, w/ optimization
 #  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
-#set(ROS_BUILD_TYPE RelWithDebInfo)
-set(CMAKE_BUILD_TYPE Release)
 
-set(CMAKE_SKIP_BUILD_RPATH  FALSE)
-set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
-set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
-set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
+set(CMAKE_BUILD_TYPE Release)
 
-#set the default path for built executables to the "bin" directory
-set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
-#set the default path for built libraries to the "lib" directory
-set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
+set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")
+
+# Check for SSE flags
+set(FCL_HAVE_SSE 0)
+include(FCLCheckSSE)
+fcl_check_for_sse()
+if (SSE_FLAGS)
+  message(STATUS "FCL uses SSE")
+  add_definitions(${SSE_FLAGS})
+  set(FCL_HAVE_SSE 0)
+  # waiting for Jia to fix this; not using sse anyway
+else()
+  message(STATUS "FCL does not use SSE")
+endif()
 
+# make sure we know what flag we used for SSE 
 include_directories("include")
 
 find_package(PkgConfig)
@@ -32,16 +38,13 @@ if(OCTOMAP_FOUND)
     include_directories(${OCTOMAP_INCLUDE_DIRS})
     link_directories(${OCTOMAP_LIBRARY_DIRS})
 endif()
-add_definitions(-DUSE_SVMLIGHT=0)
-
-add_library(${PROJECT_NAME} SHARED src/BV/AABB.cpp src/BV/OBB.cpp src/BV/RSS.cpp src/BV/kIOS.cpp src/BV/OBBRSS.cpp src/BV/kDOP.cpp src/traversal/traversal_node_base.cpp src/traversal/traversal_node_bvhs.cpp src/intersect.cpp src/ccd/motion.cpp src/BVH/BV_fitter.cpp src/BVH/BV_splitter.cpp src/BVH/BVH_model.cpp src/BVH/BVH_utility.cpp src/math/transform.cpp src/traversal/traversal_node_setup.cpp src/shape/geometric_shapes.cpp src/shape/geometric_shapes_utility.cpp src/collision_node.cpp src/traversal/traversal_recurse.cpp src/broadphase/broadphase_bruteforce.cpp src/broadphase/broadphase_spatialhash.cpp src/broadphase/broadphase_SaP.cpp src/broadphase/broadphase_SSaP.cpp src/broadphase/broadphase_interval_tree.cpp src/broadphase/broadphase_dynamic_AABB_tree.cpp src/broadphase/broadphase_dynamic_AABB_tree_array.cpp src/collision.cpp src/collision_func_matrix.cpp src/broadphase/interval_tree.cpp src/ccd/conservative_advancement.cpp src/ccd/interval.cpp src/ccd/interval_vector.cpp src/ccd/interval_matrix.cpp src/ccd/taylor_model.cpp src/ccd/taylor_vector.cpp src/ccd/taylor_matrix.cpp src/distance_func_matrix.cpp src/distance.cpp src/narrowphase/gjk.cpp src/narrowphase/gjk_libccd.cpp src/narrowphase/narrowphase.cpp src/broadphase/hierarchy_tree.cpp src/profile.cpp src/collision_data.cpp)
 
 
-target_link_libraries(${PROJECT_NAME} ${CCD_LIBRARIES} ${OCTOMAP_LIBRARIES})
+add_subdirectory(include/fcl)
+add_subdirectory(src)
 
 set(pkg_conf_file "${CMAKE_CURRENT_SOURCE_DIR}/fcl.pc")
 configure_file("${pkg_conf_file}.in" "${pkg_conf_file}" @ONLY)
 
-install(TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION lib/ LIBRARY DESTINATION lib/)
 install(DIRECTORY include/ DESTINATION include FILES_MATCHING PATTERN "*.h")
 install(FILES "${pkg_conf_file}" DESTINATION lib/pkgconfig/ COMPONENT pkgconfig)
diff --git a/trunk/fcl/CMakeModules/FCLCheckSSE.cmake b/trunk/fcl/CMakeModules/FCLCheckSSE.cmake
new file mode 100644
index 00000000..f03d5cfd
--- /dev/null
+++ b/trunk/fcl/CMakeModules/FCLCheckSSE.cmake
@@ -0,0 +1,83 @@
+macro(fcl_check_for_sse)
+  # check for SSE extensions
+  include(CheckCXXSourceRuns)
+  if( CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX )
+   set(SSE_FLAGS)
+
+   set(CMAKE_REQUIRED_FLAGS "-msse3")
+   check_cxx_source_runs("
+    #include <pmmintrin.h>
+
+    int main()
+    {
+       __m128d a, b;
+       double vals[2] = {0};
+       a = _mm_loadu_pd(vals);
+       b = _mm_hadd_pd(a,a);
+       _mm_storeu_pd(vals, b);
+       return 0;
+    }"
+    HAS_SSE3_EXTENSIONS)
+
+   set(CMAKE_REQUIRED_FLAGS "-msse2")
+   check_cxx_source_runs("
+    #include <emmintrin.h>
+
+    int main()
+    {
+        __m128d a, b;
+        double vals[2] = {0};
+        a = _mm_loadu_pd(vals);
+        b = _mm_add_pd(a,a);
+        _mm_storeu_pd(vals,b);
+        return 0;
+     }"
+     HAS_SSE2_EXTENSIONS)
+
+   set(CMAKE_REQUIRED_FLAGS "-msse")
+   check_cxx_source_runs("
+    #include <xmmintrin.h>
+    int main()
+    {
+        __m128 a, b;
+        float vals[4] = {0};
+        a = _mm_loadu_ps(vals);
+        b = a;
+        b = _mm_add_ps(a,b);
+        _mm_storeu_ps(vals,b);
+        return 0;
+    }"
+    HAS_SSE_EXTENSIONS)
+
+   set(CMAKE_REQUIRED_FLAGS)
+
+   if(HAS_SSE3_EXTENSIONS)
+    set(SSE_FLAGS "-msse3 -mfpmath=sse")
+    message(STATUS " Found SSE3 extensions, using flags: ${SSE_FLAGS}")
+   elseif(HAS_SSE2_EXTENSIONS)
+    set(SSE_FLAGS "-msse2 -mfpmath=sse")
+    message(STATUS " Found SSE2 extensions, using flags: ${SSE_FLAGS}")
+   elseif(HAS_SSE_EXTENSIONS)
+    set(SSE_FLAGS "-msse -mfpmath=sse")
+    message(STATUS " Found SSE extensions, using flags: ${SSE_FLAGS}")
+   endif()
+  elseif(MSVC)
+   check_cxx_source_runs("
+    #include <emmintrin.h>
+
+    int main()
+    {
+        __m128d a, b;
+        double vals[2] = {0};
+        a = _mm_loadu_pd(vals);
+        b = _mm_add_pd(a,a);
+        _mm_storeu_pd(vals,b);
+        return 0;
+     }"
+     HAS_SSE2_EXTENSIONS)
+   if( HAS_SSE2_EXTENSIONS )
+    message(STATUS " Found SSE2 extensions")
+    set(SSE_FLAGS "/arch:SSE2 /fp:fast -D__SSE__ -D__SSE2__" )
+   endif()
+  endif()
+endmacro(fcl_check_for_sse)
diff --git a/trunk/fcl/include/fcl/CMakeLists.txt b/trunk/fcl/include/fcl/CMakeLists.txt
new file mode 100644
index 00000000..00022861
--- /dev/null
+++ b/trunk/fcl/include/fcl/CMakeLists.txt
@@ -0,0 +1,2 @@
+file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" VERSION_DIR)
+configure_file("${VERSION_DIR}/config.h.in" "${VERSION_DIR}/config.h")
diff --git a/trunk/fcl/include/fcl/simd/simd.h b/trunk/fcl/include/fcl/config.h.in
similarity index 91%
rename from trunk/fcl/include/fcl/simd/simd.h
rename to trunk/fcl/include/fcl/config.h.in
index f63fd5e3..bc8b3c83 100644
--- a/trunk/fcl/include/fcl/simd/simd.h
+++ b/trunk/fcl/include/fcl/config.h.in
@@ -1,7 +1,7 @@
 /*
  * Software License Agreement (BSD License)
  *
- *  Copyright (c) 2011, Willow Garage, Inc.
+ *  Copyright (c) 2012, Willow Garage, Inc.
  *  All rights reserved.
  *
  *  Redistribution and use in source and binary forms, with or without
@@ -32,11 +32,10 @@
  *  POSSIBILITY OF SUCH DAMAGE.
  */
 
-/** \author Jia Pan */
+#ifndef FCL_CONFIG_
+#define FCL_CONFIG_
 
-#ifndef FCL_SIMD_H
-#define FCL_SIMD_H
-
-#include "fcl/simd/math_simd_details.h"
+#define FCL_VERSION "@FCL_VERSION@"
+#cmakedefine01 FCL_HAVE_SSE
 
 #endif
diff --git a/trunk/fcl/include/fcl/math/vec_3f.h b/trunk/fcl/include/fcl/math/vec_3f.h
index 37e0c596..ba60e0e9 100644
--- a/trunk/fcl/include/fcl/math/vec_3f.h
+++ b/trunk/fcl/include/fcl/math/vec_3f.h
@@ -37,9 +37,14 @@
 #ifndef FCL_VEC_3F_H
 #define FCL_VEC_3F_H
 
+#include "fcl/config.h"
 #include "fcl/data_types.h"
 #include "fcl/math/math_details.h"
-#include "fcl/simd/math_simd_details.h"
+
+#if FCL_HAVE_SSE
+#  include "fcl/simd/math_simd_details.h"
+#endif
+
 #include <cmath>
 #include <iostream>
 #include <limits>
@@ -213,10 +218,11 @@ void generateCoordinateSystem(const Vec3fX<T>& w, Vec3fX<T>& u, Vec3fX<T>& v)
   }
 }
 
-
-typedef Vec3fX<details::Vec3Data<FCL_REAL> > Vec3f;
-//typedef Vec3fX<details::sse_meta_f4> Vec3f;
-
+#if FCL_HAVE_SSE
+  typedef Vec3fX<details::sse_meta_f4> Vec3f;
+#else
+  typedef Vec3fX<details::Vec3Data<FCL_REAL> > Vec3f;
+#endif
 
 static inline std::ostream& operator << (std::ostream& o, const Vec3f& v)
 {
diff --git a/trunk/fcl/src/CMakeLists.txt b/trunk/fcl/src/CMakeLists.txt
new file mode 100644
index 00000000..abe55237
--- /dev/null
+++ b/trunk/fcl/src/CMakeLists.txt
@@ -0,0 +1,50 @@
+add_library(${PROJECT_NAME} SHARED
+  BV/AABB.cpp
+  BV/OBB.cpp
+  BV/RSS.cpp
+  BV/kIOS.cpp
+  BV/OBBRSS.cpp
+  BV/kDOP.cpp
+  traversal/traversal_node_base.cpp
+  traversal/traversal_node_bvhs.cpp
+  intersect.cpp
+  ccd/motion.cpp
+  BVH/BV_fitter.cpp
+  BVH/BV_splitter.cpp
+  BVH/BVH_model.cpp
+  BVH/BVH_utility.cpp
+  math/transform.cpp
+  traversal/traversal_node_setup.cpp
+  shape/geometric_shapes.cpp
+  shape/geometric_shapes_utility.cpp
+  collision_node.cpp
+  traversal/traversal_recurse.cpp
+  broadphase/broadphase_bruteforce.cpp
+  broadphase/broadphase_spatialhash.cpp
+  broadphase/broadphase_SaP.cpp
+  broadphase/broadphase_SSaP.cpp
+  broadphase/broadphase_interval_tree.cpp
+  broadphase/broadphase_dynamic_AABB_tree.cpp
+  broadphase/broadphase_dynamic_AABB_tree_array.cpp 
+  collision.cpp
+  collision_func_matrix.cpp
+  broadphase/interval_tree.cpp
+  ccd/conservative_advancement.cpp
+  ccd/interval.cpp
+  ccd/interval_vector.cpp
+  ccd/interval_matrix.cpp
+  ccd/taylor_model.cpp
+  ccd/taylor_vector.cpp
+  ccd/taylor_matrix.cpp
+  distance_func_matrix.cpp
+  distance.cpp
+  narrowphase/gjk.cpp
+  narrowphase/gjk_libccd.cpp
+  narrowphase/narrowphase.cpp
+  broadphase/hierarchy_tree.cpp
+  profile.cpp
+  collision_data.cpp)
+
+target_link_libraries(${PROJECT_NAME} ${CCD_LIBRARIES} ${OCTOMAP_LIBRARIES})
+
+install(TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION lib/ LIBRARY DESTINATION lib/)
-- 
GitLab