Build: Detect whether compiler supports DSPr2

This is basically the same test that was performed in acinclude.m4 in
the old autotools-based build system.  It was not ported to the
CMake-based build system because I previously had no way of testing
a non-DSPr2 build environment.

Fixes #248
diff --git a/ChangeLog.md b/ChangeLog.md
index e18a282..03f28da 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -44,6 +44,9 @@
 a 4:2:2 or 4:2:0 JPEG image using the merged (non-fancy) upsampling algorithms
 (that is, when setting `cinfo.do_fancy_upsampling` to `FALSE`.)
 
+7. The new CMake-based build system will now disable the MIPS DSPr2 SIMD
+extensions if it detects that the compiler does not support DSPr2 instructions.
+
 
 1.5.90 (2.0 beta1)
 ==================
diff --git a/simd/CMakeLists.txt b/simd/CMakeLists.txt
index 2468b1b..346994c 100755
--- a/simd/CMakeLists.txt
+++ b/simd/CMakeLists.txt
@@ -270,6 +270,29 @@
 set(EFFECTIVE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${CMAKE_ASM_FLAGS_${CMAKE_BUILD_TYPE_UC}}")
 message(STATUS "CMAKE_ASM_FLAGS = ${EFFECTIVE_ASM_FLAGS}")
 
+set(CMAKE_REQUIRED_FLAGS -mdspr2)
+
+check_c_source_compiles("
+  #if !(defined(__mips__) && __mips_isa_rev >= 2)
+  #error MIPS DSPr2 is currently only available on MIPS32r2 platforms.
+  #endif
+  int main(void) {
+    int c = 0, a = 0, b = 0;
+    __asm__ __volatile__ (
+      \"precr.qb.ph %[c], %[a], %[b]\"
+      : [c] \"=r\" (c)
+      : [a] \"r\" (a), [b] \"r\" (b)
+    );
+    return c;
+  }" HAVE_DSPR2)
+
+unset(CMAKE_REQUIRED_FLAGS)
+
+if(NOT HAVE_DSPR2)
+  simd_fail("SIMD extensions not available for this CPU")
+  return()
+endif()
+
 add_library(simd OBJECT ${CPU_TYPE}/jsimd_dspr2.S ${CPU_TYPE}/jsimd.c)
 
 if(CMAKE_POSITION_INDEPENDENT_CODE OR ENABLE_SHARED)