From 992187a3806975e8f23616d8542c901603ffb01c Mon Sep 17 00:00:00 2001
From: James Zern <jzern@google.com>
Date: Wed, 4 Jan 2012 18:38:07 -0800
Subject: [PATCH] improve log2 test

- add check for native log2 to configure
- use a common define (NOT_HAVE_LOG2) to enable use of local library
  version for non-autoconf platforms without their own version,
  currently msvc and android

This uses a negative (NOT_HAVE_) to simplify the ifdef

Change-Id: Id0610eed507f8bb9c5da338918112853d5c8127a
---
 Android.mk         | 3 ++-
 Makefile.vc        | 2 +-
 configure.ac       | 5 +++++
 src/utils/tcoder.c | 5 ++++-
 4 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/Android.mk b/Android.mk
index 4e90eb09..04632d57 100644
--- a/Android.mk
+++ b/Android.mk
@@ -38,7 +38,8 @@ LOCAL_SRC_FILES := \
 	src/utils/tcoder.c \
 	src/utils/thread.c \
 
-LOCAL_CFLAGS := -Wall -DANDROID -DHAVE_MALLOC_H -DHAVE_PTHREAD -DWEBP_USE_THREAD \
+LOCAL_CFLAGS := -Wall -DANDROID -DHAVE_MALLOC_H -DHAVE_PTHREAD \
+                -DNOT_HAVE_LOG2 -DWEBP_USE_THREAD \
                 -finline-functions -frename-registers -ffast-math \
                 -s -fomit-frame-pointer -Isrc/webp
 
diff --git a/Makefile.vc b/Makefile.vc
index 76ea1424..1f80a22d 100644
--- a/Makefile.vc
+++ b/Makefile.vc
@@ -37,7 +37,7 @@ CCNODBG    = cl.exe /nologo /O2 /DNDEBUG
 CCDEBUG    = cl.exe /nologo /Od /Gm /Zi /D_DEBUG /RTC1
 CFLAGS     = /Isrc /nologo /W3 /EHsc /FD /c /GS
 CFLAGS     = $(CFLAGS) /DWIN32 /D_CRT_SECURE_NO_WARNINGS /DWIN32_LEAN_AND_MEAN
-CFLAGS     = $(CFLAGS) /DHAVE_WINCODEC_H /DWEBP_USE_THREAD
+CFLAGS     = $(CFLAGS) /DHAVE_WINCODEC_H /DWEBP_USE_THREAD /DNOT_HAVE_LOG2
 LDFLAGS    = /LARGEADDRESSAWARE /MANIFEST /NXCOMPAT /DYNAMICBASE
 LDFLAGS    = $(LDFLAGS) $(PLATFORM_LDFLAGS)
 LNKDLL     = link.exe /DLL
diff --git a/configure.ac b/configure.ac
index 4aac268f..aa2dc567 100644
--- a/configure.ac
+++ b/configure.ac
@@ -11,6 +11,11 @@ AC_ARG_WITH([pkgconfigdir], AS_HELP_STRING([--with-pkgconfigdir=PATH],
 	[pkgconfigdir="$withval"], [pkgconfigdir='${libdir}/pkgconfig'])
 AC_SUBST([pkgconfigdir])
 
+dnl === Check for native log2
+AC_SEARCH_LIBS([log2], [m],,
+               [AC_DEFINE([NOT_HAVE_LOG2], [1],
+                          [Undefine this if you have log2().])])
+
 dnl === Check libz is present
 
 if test "$enable_experimental" = "yes"; then
diff --git a/src/utils/tcoder.c b/src/utils/tcoder.c
index 1136648c..d3707e00 100644
--- a/src/utils/tcoder.c
+++ b/src/utils/tcoder.c
@@ -78,6 +78,9 @@
 // Using this simple maintenance, we observed a typical 10-20% reduction
 // in the number of calls to VP8PutBit(), leading to 3-5% speed gain.
 //
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 #include "./tcoderi.h"
 
@@ -85,7 +88,7 @@
 extern "C" {
 #endif
 
-#ifdef _MSC_VER
+#ifdef NOT_HAVE_LOG2
 static double log2(double d) {
   const double kLog2Reciprocal = 1.442695040888963;
   return log(d) * kLog2Reciprocal;