MACRO (FindSSE)

    IF(CMAKE_SYSTEM_NAME MATCHES "Linux")
        EXEC_PROGRAM(cat ARGS "/proc/cpuinfo" OUTPUT_VARIABLE CPUINFO)

        IF (CPUINFO MATCHES "sse2")
            set(SSE2_FOUND ON CACHE BOOL "SSE2 available on host")
        ELSE (CPUINFO MATCHES "sse2")
            set(SSE2_FOUND OFF CACHE BOOL "SSE2 available on host")
        ENDIF (CPUINFO MATCHES "sse2")

        # /proc/cpuinfo apparently omits sse3 :(
        IF (CPUINFO MATCHES "ssse3")
            set(SSE3_FOUND ON CACHE BOOL "SSE3 available on host")
            set(SSSE3_FOUND ON CACHE BOOL "SSSE3 available on host")
        ELSE (CPUINFO MATCHES "ssse3")
            set(SSE3_FOUND OFF CACHE BOOL "SSE3 available on host")
            set(SSSE3_FOUND OFF CACHE BOOL "SSSE3 available on host")
        ENDIF (CPUINFO MATCHES "ssse3")

        IF (CPUINFO MATCHES "sse4_1")
            set(SSE4_1_FOUND ON CACHE BOOL "SSE4.1 available on host")
        ELSE (CPUINFO MATCHES "sse4_1")
            set(SSE4_1_FOUND OFF CACHE BOOL "SSE4.1 available on host")
        ENDIF (CPUINFO MATCHES "sse4_1")

        IF (CPUINFO MATCHES "sse4_2")
            set(SSE4_2_FOUND ON CACHE BOOL "SSE4.2 available on host")
        ELSE (CPUINFO MATCHES "sse4_2")
            set(SSE4_2_FOUND OFF CACHE BOOL "SSE4.2 available on host")
        ENDIF (CPUINFO MATCHES "sse4_2")

    ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin")
        EXEC_PROGRAM("/usr/sbin/sysctl -n machdep.cpu.features" OUTPUT_VARIABLE
                CPUINFO)

        IF (CPUINFO MATCHES "SSE2")
            set(SSE2_FOUND ON CACHE BOOL "SSE3 available on host")
        ELSE (CPUINFO MATCHES "SSE2")
            set(SSE2_FOUND OFF CACHE BOOL "SSE3 available on host")
        ENDIF (CPUINFO MATCHES "SSE2")

        IF (CPUINFO MATCHES "SSE3")
            set(SSE3_FOUND ON CACHE BOOL "SSE3 available on host")
        ELSE (CPUINFO MATCHES "SSE3")
            set(SSE3_FOUND OFF CACHE BOOL "SSE3 available on host")
        ENDIF (CPUINFO MATCHES "SSE3")

        IF (CPUINFO MATCHES "SSSE3")
            set(SSSE3_FOUND ON CACHE BOOL "SSSE3 available on host")
        ELSE (CPUINFO MATCHES "SSSE3")
            set(SSSE3_FOUND OFF CACHE BOOL "SSSE3 available on host")
        ENDIF (CPUINFO MATCHES "SSSE3")

        IF (CPUINFO MATCHES "SSE4.1")
            set(SSE4_1_FOUND ON CACHE BOOL "SSE4.1 available on host")
        ELSE (CPUINFO MATCHES "SSE4.1")
            set(SSE4_1_FOUND OFF CACHE BOOL "SSE4.1 available on host")
        ENDIF (CPUINFO MATCHES "SSE4.1")

        IF (CPUINFO MATCHES "SSE4.2")
            set(SSE4_2_FOUND ON CACHE BOOL "SSE4.2 available on host")
        ELSE (CPUINFO MATCHES "SSE4.2")
            set(SSE4_2_FOUND OFF CACHE BOOL "SSE4.2 available on host")
        ENDIF (CPUINFO MATCHES "SSE4.2")

    ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Windows")
        # TODO
        set(SSE2_FOUND   ON  CACHE BOOL "SSE2 available on host")
        set(SSE3_FOUND   OFF CACHE BOOL "SSE3 available on host")
        set(SSSE3_FOUND  OFF CACHE BOOL "SSSE3 available on host")
        set(SSE4_1_FOUND OFF CACHE BOOL "SSE4.1 available on host")
        set(SSE4_2_FOUND OFF CACHE BOOL "SSE4.2 available on host")
    ELSE(CMAKE_SYSTEM_NAME MATCHES "Linux")
        set(SSE2_FOUND   ON  CACHE BOOL "SSE2 available on host")
        set(SSE3_FOUND   OFF CACHE BOOL "SSE3 available on host")
        set(SSSE3_FOUND  OFF CACHE BOOL "SSSE3 available on host")
        set(SSE4_1_FOUND OFF CACHE BOOL "SSE4.1 available on host")
        set(SSE4_2_FOUND OFF CACHE BOOL "SSE4.2 available on host")
    ENDIF(CMAKE_SYSTEM_NAME MATCHES "Linux")

    IF(CMAKE_COMPILER_IS_GNUCXX)
        EXECUTE_PROCESS(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
        IF(GCC_VERSION VERSION_LESS 4.2)
            set(SSE4_1_FOUND OFF CACHE BOOL "SSE4.1 available on host" FORCE)
            set(SSE4_2_FOUND OFF CACHE BOOL "SSE4.2 available on host" FORCE)
        ENDIF()
    ENDIF(CMAKE_COMPILER_IS_GNUCXX)

    if(NOT SSE2_FOUND)
        MESSAGE(STATUS "Could not find support for SSE2 on this machine.")
    endif(NOT SSE2_FOUND)
    if(NOT SSE3_FOUND)
        MESSAGE(STATUS "Could not find support for SSE3 on this machine.")
    endif(NOT SSE3_FOUND)
    if(NOT SSSE3_FOUND)
        MESSAGE(STATUS "Could not find support for SSSE3 on this machine.")
    endif(NOT SSSE3_FOUND)
    if(NOT SSE4_1_FOUND)
        MESSAGE(STATUS "Could not find support for SSE4.1 on this machine.")
    endif(NOT SSE4_1_FOUND)
    if(NOT SSE4_2_FOUND)
        MESSAGE(STATUS "Could not find support for SSE4.2 on this machine.")
    endif(NOT SSE4_2_FOUND)

    mark_as_advanced(SSE2_FOUND SSE3_FOUND SSSE3_FOUND SSE4_1_FOUND SSE4_2_FOUND)

ENDMACRO(FindSSE)