Remove include of <math.h> from fx_system.h

Put implementation of FXSYS_sqrt2() requiring math.h into .cpp file.
Call other C-library functions directly rather than using FX wrappers.

Change-Id: Ib05204908e5b4d11555bce41144b5dd648b9cdd8
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/82792
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_colorspace.cpp b/core/fpdfapi/page/cpdf_colorspace.cpp
index 49c687a..aa530b5 100644
--- a/core/fpdfapi/page/cpdf_colorspace.cpp
+++ b/core/fpdfapi/page/cpdf_colorspace.cpp
@@ -6,6 +6,8 @@
 
 #include "core/fpdfapi/page/cpdf_colorspace.h"
 
+#include <math.h>
+
 #include <algorithm>
 #include <limits>
 #include <memory>
@@ -771,9 +773,9 @@
   float B_ = pBuf[1];
   float C_ = pBuf[2];
   if (m_bHasGamma) {
-    A_ = FXSYS_pow(A_, m_Gamma[0]);
-    B_ = FXSYS_pow(B_, m_Gamma[1]);
-    C_ = FXSYS_pow(C_, m_Gamma[2]);
+    A_ = powf(A_, m_Gamma[0]);
+    B_ = powf(B_, m_Gamma[1]);
+    C_ = powf(C_, m_Gamma[2]);
   }
 
   float X;
diff --git a/core/fpdfapi/page/cpdf_expintfunc.cpp b/core/fpdfapi/page/cpdf_expintfunc.cpp
index 08afb91..467f26b 100644
--- a/core/fpdfapi/page/cpdf_expintfunc.cpp
+++ b/core/fpdfapi/page/cpdf_expintfunc.cpp
@@ -6,6 +6,8 @@
 
 #include "core/fpdfapi/page/cpdf_expintfunc.h"
 
+#include <math.h>
+
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_number.h"
@@ -58,8 +60,8 @@
   for (uint32_t i = 0; i < m_nInputs; i++) {
     for (uint32_t j = 0; j < m_nOrigOutputs; j++) {
       results[i * m_nOrigOutputs + j] =
-          m_BeginValues[j] + FXSYS_pow(inputs[i], m_Exponent) *
-                                 (m_EndValues[j] - m_BeginValues[j]);
+          m_BeginValues[j] +
+          powf(inputs[i], m_Exponent) * (m_EndValues[j] - m_BeginValues[j]);
     }
   }
   return true;
diff --git a/core/fpdfapi/page/cpdf_psengine.cpp b/core/fpdfapi/page/cpdf_psengine.cpp
index 93194dc..68e23b6 100644
--- a/core/fpdfapi/page/cpdf_psengine.cpp
+++ b/core/fpdfapi/page/cpdf_psengine.cpp
@@ -306,7 +306,7 @@
     case PSOP_EXP:
       d2 = Pop();
       d1 = Pop();
-      Push(FXSYS_pow(d1, d2));
+      Push(powf(d1, d2));
       break;
     case PSOP_LN:
       d1 = Pop();
diff --git a/core/fxcrt/css/cfx_cssnumbervalue.cpp b/core/fxcrt/css/cfx_cssnumbervalue.cpp
index 78d53ea..8653283 100644
--- a/core/fxcrt/css/cfx_cssnumbervalue.cpp
+++ b/core/fxcrt/css/cfx_cssnumbervalue.cpp
@@ -6,6 +6,8 @@
 
 #include "core/fxcrt/css/cfx_cssnumbervalue.h"
 
+#include <math.h>
+
 CFX_CSSNumberValue::CFX_CSSNumberValue(Unit unit, float value)
     : CFX_CSSValue(PrimitiveType::kNumber), unit_(unit), value_(value) {
   if (unit_ == Unit::kNumber && fabs(value_) < 0.001f)
diff --git a/core/fxcrt/fx_system.cpp b/core/fxcrt/fx_system.cpp
index 7358b75..52da8e0 100644
--- a/core/fxcrt/fx_system.cpp
+++ b/core/fxcrt/fx_system.cpp
@@ -265,3 +265,7 @@
   return g_last_error;
 }
 #endif  // defined(OS_WIN)
+
+float FXSYS_sqrt2(float a, float b) {
+  return sqrtf(a * a + b * b);
+}
diff --git a/core/fxcrt/fx_system.h b/core/fxcrt/fx_system.h
index 5b5d0b5..9582028 100644
--- a/core/fxcrt/fx_system.h
+++ b/core/fxcrt/fx_system.h
@@ -7,7 +7,6 @@
 #ifndef CORE_FXCRT_FX_SYSTEM_H_
 #define CORE_FXCRT_FX_SYSTEM_H_
 
-#include <math.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -100,7 +99,6 @@
 #define FXSYS_wcsicmp _wcsicmp
 #define FXSYS_wcslwr _wcslwr
 #define FXSYS_wcsupr _wcsupr
-#define FXSYS_pow(a, b) (float)powf(a, b)
 size_t FXSYS_wcsftime(wchar_t* strDest,
                       size_t maxsize,
                       const wchar_t* format,
@@ -130,7 +128,6 @@
 int FXSYS_wcsicmp(const wchar_t* str1, const wchar_t* str2);
 wchar_t* FXSYS_wcslwr(wchar_t* str);
 wchar_t* FXSYS_wcsupr(wchar_t* str);
-#define FXSYS_pow(a, b) (float)pow(a, b)
 #define FXSYS_wcsftime wcsftime
 void FXSYS_SetLastError(uint32_t err);
 uint32_t FXSYS_GetLastError();
@@ -155,7 +152,7 @@
 const char* FXSYS_i64toa(int64_t value, char* str, int radix);
 int FXSYS_roundf(float f);
 int FXSYS_round(double d);
-#define FXSYS_sqrt2(a, b) (float)sqrt((a) * (a) + (b) * (b))
+float FXSYS_sqrt2(float a, float b);
 #ifdef __cplusplus
 }  // extern C
 #endif  // __cplusplus
diff --git a/core/fxcrt/fx_system_unittest.cpp b/core/fxcrt/fx_system_unittest.cpp
index 5a44aad..6e109b6 100644
--- a/core/fxcrt/fx_system_unittest.cpp
+++ b/core/fxcrt/fx_system_unittest.cpp
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#include <math.h>
+
 #include <limits>
 
 #include "build/build_config.h"
diff --git a/fxbarcode/pdf417/BC_PDF417.cpp b/fxbarcode/pdf417/BC_PDF417.cpp
index 3b528ac..ef91627 100644
--- a/fxbarcode/pdf417/BC_PDF417.cpp
+++ b/fxbarcode/pdf417/BC_PDF417.cpp
@@ -22,6 +22,8 @@
 
 #include "fxbarcode/pdf417/BC_PDF417.h"
 
+#include <math.h>
+
 #include "fxbarcode/pdf417/BC_PDF417BarcodeMatrix.h"
 #include "fxbarcode/pdf417/BC_PDF417BarcodeRow.h"
 #include "fxbarcode/pdf417/BC_PDF417ErrorCorrection.h"
diff --git a/fxjs/cjs_publicmethods.cpp b/fxjs/cjs_publicmethods.cpp
index 3a73a4c..db650e2 100644
--- a/fxjs/cjs_publicmethods.cpp
+++ b/fxjs/cjs_publicmethods.cpp
@@ -1367,7 +1367,7 @@
   if (wcscmp(sFunction.c_str(), L"AVG") == 0 && nFieldsCount > 0)
     dValue /= nFieldsCount;
 
-  dValue = floor(dValue * FXSYS_pow(10, 6) + 0.49) / FXSYS_pow(10, 6);
+  dValue = floor(dValue * powf(10, 6) + 0.49) / powf(10, 6);
 
   CJS_EventContext* pContext = pRuntime->GetCurrentEventContext();
   if (pContext->GetEventRecorder()->HasValue()) {
diff --git a/fxjs/xfa/cfxjse_formcalc_context.cpp b/fxjs/xfa/cfxjse_formcalc_context.cpp
index 2669d41..0a6f6a1 100644
--- a/fxjs/xfa/cfxjse_formcalc_context.cpp
+++ b/fxjs/xfa/cfxjse_formcalc_context.cpp
@@ -6,6 +6,7 @@
 
 #include "fxjs/xfa/cfxjse_formcalc_context.h"
 
+#include <math.h>
 #include <stdlib.h>
 
 #include <algorithm>
@@ -3062,8 +3063,8 @@
     return;
   }
 
-  info.GetReturnValue().Set(
-      FXSYS_pow((float)(nFuture / nPresent), (float)(1 / nTotalNumber)) - 1);
+  info.GetReturnValue().Set(powf(nFuture / nPresent, 1.0f / nTotalNumber) -
+                            1.0f);
 }
 
 // static
diff --git a/third_party/agg23/0010-math.patch b/third_party/agg23/0010-math.patch
new file mode 100644
index 0000000..ae5d6a3
--- /dev/null
+++ b/third_party/agg23/0010-math.patch
@@ -0,0 +1,12 @@
+diff --git a/third_party/agg23/agg_basics.h b/third_party/agg23/agg_basics.h
+index eb6f35686..e7583e308 100644
+--- a/third_party/agg23/agg_basics.h
++++ b/third_party/agg23/agg_basics.h
+@@ -41,6 +41,7 @@
+ #endif
+ #define AGG_INLINE inline
+ 
++#include <math.h>
+ #include "core/fxcrt/fx_system.h"
+ 
+ namespace pdfium
diff --git a/third_party/agg23/README.pdfium b/third_party/agg23/README.pdfium
index d9e3bf5..e241f94 100644
--- a/third_party/agg23/README.pdfium
+++ b/third_party/agg23/README.pdfium
@@ -25,4 +25,5 @@
 0007-unused-struct.patch: Remove unused struct point_type_flag, which has a
 shadow variable.
 0008-namespace.patch: Wrap all AGG code in namespace pdfium.
-0009-infinite-loop.patch: avoid hang in agg_math_stroke.h
\ No newline at end of file
+0009-infinite-loop.patch: avoid hang in agg_math_stroke.h
+0010-math.patch: includes <math.h>
diff --git a/third_party/agg23/agg_basics.h b/third_party/agg23/agg_basics.h
index eb6f356..e7583e3 100644
--- a/third_party/agg23/agg_basics.h
+++ b/third_party/agg23/agg_basics.h
@@ -41,6 +41,7 @@
 #endif
 #define AGG_INLINE inline
 
+#include <math.h>
 #include "core/fxcrt/fx_system.h"
 
 namespace pdfium
diff --git a/xfa/fgas/crt/cfgas_decimal_unittest.cpp b/xfa/fgas/crt/cfgas_decimal_unittest.cpp
index 816045e..e59ae66 100644
--- a/xfa/fgas/crt/cfgas_decimal_unittest.cpp
+++ b/xfa/fgas/crt/cfgas_decimal_unittest.cpp
@@ -4,6 +4,8 @@
 
 #include "xfa/fgas/crt/cfgas_decimal.h"
 
+#include <math.h>
+
 #include <limits>
 
 #include "testing/gtest/include/gtest/gtest.h"
diff --git a/xfa/fgas/crt/cfgas_stringformatter.cpp b/xfa/fgas/crt/cfgas_stringformatter.cpp
index 6cf9ca2..d0a284c 100644
--- a/xfa/fgas/crt/cfgas_stringformatter.cpp
+++ b/xfa/fgas/crt/cfgas_stringformatter.cpp
@@ -6,6 +6,8 @@
 
 #include "xfa/fgas/crt/cfgas_stringformatter.h"
 
+#include <math.h>
+
 #include <algorithm>
 #include <limits>
 #include <utility>
@@ -1557,13 +1559,10 @@
   }
   if (iExponent || bHavePercentSymbol) {
     CFGAS_Decimal decimal = CFGAS_Decimal(wsValue->AsStringView());
-    if (iExponent) {
-      decimal = decimal *
-                CFGAS_Decimal(FXSYS_pow(10, static_cast<float>(iExponent)), 3);
-    }
+    if (iExponent)
+      decimal = decimal * CFGAS_Decimal(powf(10, iExponent), 3);
     if (bHavePercentSymbol)
       decimal = decimal / CFGAS_Decimal(100);
-
     *wsValue = decimal.ToWideString();
   }
   if (bNeg)