[Libreoffice-commits] core.git: Branch 'libreoffice-4-4' - formula/Library_for.mk formula/README formula/source include/formula scaddins/Library_analysis.mk scaddins/source sc/source
Tor Lillqvist
tml at collabora.com
Tue Jan 20 23:21:52 PST 2015
formula/Library_for.mk | 1
formula/README | 6 ++-
formula/source/core/api/random.cxx | 56 +++++++++++++++++++++++++++++++
include/formula/random.hxx | 31 +++++++++++++++++
sc/source/core/opencl/formulagroupcl.cxx | 4 +-
sc/source/core/tool/interpr1.cxx | 3 +
scaddins/Library_analysis.mk | 1
scaddins/source/analysis/analysis.cxx | 4 +-
8 files changed, 99 insertions(+), 7 deletions(-)
New commits:
commit dad7161cc30b335a108c7850f453877a161969ae
Author: Tor Lillqvist <tml at collabora.com>
Date: Tue Jan 20 16:24:14 2015 +0200
fdo88632: Make the Calc random functions non-random when requested
We don't want such a mode to affect other uses of randomness, though. Thus use
a separate random number generator object for these two functions, and use a
fixed seed for it if the SC_RAND_REPEATABLE environment variable is set.
As RAND() is implemented in sc, and RANDBETWEEN() is implemented in scaddins,
it was a bit hard to figure out where to add the new functions needed, without
having to over-engineer things with UNO. (This functionality is totally
Calc-specific, but neither sc nor scaddins has any public (non-UNO) API.)
Caolan suggested the formula module, which seems like a good enough place to
me.
Change-Id: Ic1c9ca165278d53036598b03b13b4ffbdc98a75e
Reviewed-on: https://gerrit.libreoffice.org/14053
Reviewed-by: Michael Meeks <michael.meeks at collabora.com>
Tested-by: Michael Meeks <michael.meeks at collabora.com>
diff --git a/formula/Library_for.mk b/formula/Library_for.mk
index d2a6671..85cf5e8 100644
--- a/formula/Library_for.mk
+++ b/formula/Library_for.mk
@@ -41,6 +41,7 @@ $(eval $(call gb_Library_set_componentfile,for,formula/util/for))
$(eval $(call gb_Library_add_exception_objects,for,\
formula/source/core/api/FormulaCompiler \
formula/source/core/api/FormulaOpCodeMapperObj \
+ formula/source/core/api/random \
formula/source/core/api/services \
formula/source/core/api/token \
formula/source/core/api/vectortoken \
diff --git a/formula/README b/formula/README
index da0d528..ab2c28a 100644
--- a/formula/README
+++ b/formula/README
@@ -1,3 +1,5 @@
-Contains parts of the formula parser used outside Calc code.
+Contains parts of the formula parser used outside Calc code that has
+been pulled out from Calc's formula parser code.
-Has been pulled out from Calc's formula parser code.
+Also contains some functions that are needed by code in both sc and
+scaddins. Located here just for convenience. So sue me.
diff --git a/formula/source/core/api/random.cxx b/formula/source/core/api/random.cxx
new file mode 100644
index 0000000..727262f
--- /dev/null
+++ b/formula/source/core/api/random.cxx
@@ -0,0 +1,56 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <time.h>
+
+#include <random>
+
+#include <formula/random.hxx>
+#include <rtl/instance.hxx>
+
+namespace {
+
+struct CalcFormulaRandomGenerator
+{
+ std::mt19937 aRng;
+ CalcFormulaRandomGenerator()
+ {
+ // initialises the state of this RNG.
+ // should only be called once.
+ bool bRepeatable = (getenv("SC_RAND_REPEATABLE") != 0);
+ aRng.seed(bRepeatable ? 42 : time(NULL));
+ }
+};
+
+class theCalcFormulaRandomGenerator : public rtl::Static<CalcFormulaRandomGenerator, theCalcFormulaRandomGenerator> {};
+
+}
+
+namespace formula
+{
+
+namespace rng
+{
+
+double fRandom(double a, double b)
+{
+ std::uniform_real_distribution<double> dist(a, b);
+ return dist(theCalcFormulaRandomGenerator::get().aRng);
+}
+
+sal_Int32 nRandom(sal_Int32 a, sal_Int32 b)
+{
+ std::uniform_int_distribution<sal_Int32> dist(a, b);
+ return dist(theCalcFormulaRandomGenerator::get().aRng);
+}
+
+} // rng
+} // formula
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/formula/random.hxx b/include/formula/random.hxx
new file mode 100644
index 0000000..d5d14f5
--- /dev/null
+++ b/include/formula/random.hxx
@@ -0,0 +1,31 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef INCLUDED_FORMULA_RANDOM_HXX
+#define INCLUDED_FORMULA_RANDOM_HXX
+
+#include <formula/formuladllapi.h>
+
+namespace formula
+{
+
+namespace rng
+{
+
+// These two functions obey the SC_RAND_REPEATABLE environment
+// variable: If it is set, use a fixed seed.
+double FORMULA_DLLPUBLIC fRandom(double a, double b);
+sal_Int32 FORMULA_DLLPUBLIC nRandom(sal_Int32 a, sal_Int32 b);
+
+} // rng
+} // formula
+
+#endif // INCLUDED_FORMULA_RANDOM_HXX
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/opencl/formulagroupcl.cxx b/sc/source/core/opencl/formulagroupcl.cxx
index a5648e3..948b4ab 100644
--- a/sc/source/core/opencl/formulagroupcl.cxx
+++ b/sc/source/core/opencl/formulagroupcl.cxx
@@ -16,7 +16,7 @@
#include "tokenarray.hxx"
#include "compiler.hxx"
#include "interpre.hxx"
-#include <comphelper/random.hxx>
+#include <formula/random.hxx>
#include <formula/vectortoken.hxx>
#include "scmatrix.hxx"
@@ -654,7 +654,7 @@ threefry2x32 (threefry2x32_ctr_t in, threefry2x32_key_t k)\n\
/// Create buffer and pass the buffer to a given kernel
virtual size_t Marshal( cl_kernel k, int argno, int, cl_program ) SAL_OVERRIDE
{
- cl_int seed = comphelper::rng::uniform_int_distribution(0, SAL_MAX_INT32);
+ cl_int seed = formula::rng::nRandom(0, SAL_MAX_INT32);
// Pass the scalar result back to the rest of the formula kernel
cl_int err = clSetKernelArg(k, argno, sizeof(cl_int), (void*)&seed);
if (CL_SUCCESS != err)
diff --git a/sc/source/core/tool/interpr1.cxx b/sc/source/core/tool/interpr1.cxx
index 01343a5..f01137a 100644
--- a/sc/source/core/tool/interpr1.cxx
+++ b/sc/source/core/tool/interpr1.cxx
@@ -22,6 +22,7 @@
#include "scitems.hxx"
#include <editeng/langitem.hxx>
#include <editeng/justifyitem.hxx>
+#include <formula/random.hxx>
#include <osl/thread.h>
#include <svx/algitem.hxx>
#include <unotools/textsearch.hxx>
@@ -1662,7 +1663,7 @@ void ScInterpreter::ScPi()
void ScInterpreter::ScRandom()
{
- PushDouble(::comphelper::rng::uniform_real_distribution());
+ PushDouble(formula::rng::fRandom(0, 1));
}
void ScInterpreter::ScTrue()
diff --git a/scaddins/Library_analysis.mk b/scaddins/Library_analysis.mk
index cafce60..4946c5f 100644
--- a/scaddins/Library_analysis.mk
+++ b/scaddins/Library_analysis.mk
@@ -33,6 +33,7 @@ $(eval $(call gb_Library_use_libraries,analysis,\
comphelper \
cppu \
cppuhelper \
+ for \
sal \
tl \
i18nlangtag \
diff --git a/scaddins/source/analysis/analysis.cxx b/scaddins/source/analysis/analysis.cxx
index c0137f3..ddd5c53 100644
--- a/scaddins/source/analysis/analysis.cxx
+++ b/scaddins/source/analysis/analysis.cxx
@@ -22,8 +22,8 @@
#include "bessel.hxx"
#include <cppuhelper/factory.hxx>
#include <comphelper/processfactory.hxx>
-#include <comphelper/random.hxx>
#include <cppuhelper/supportsservice.hxx>
+#include <formula/random.hxx>
#include <osl/diagnose.h>
#include <rtl/ustrbuf.hxx>
#include <rtl/math.hxx>
@@ -702,7 +702,7 @@ double SAL_CALL AnalysisAddIn::getRandbetween( double fMin, double fMax ) throw(
if( fMin > fMax )
throw lang::IllegalArgumentException();
- double fRet = floor(comphelper::rng::uniform_real_distribution(fMin, boost::math::nextafter(fMax+1, -DBL_MAX)));
+ double fRet = floor(formula::rng::fRandom(fMin, boost::math::nextafter(fMax+1, -DBL_MAX)));
RETURN_FINITE( fRet );
}
More information about the Libreoffice-commits
mailing list