1a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop/*
2a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * Copyright (c) 2011-2015, Intel Corporation
3a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * All rights reserved.
4a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop *
5a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * Redistribution and use in source and binary forms, with or without modification,
6a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * are permitted provided that the following conditions are met:
7a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop *
8a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * 1. Redistributions of source code must retain the above copyright notice, this
9a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * list of conditions and the following disclaimer.
10a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop *
11a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * 2. Redistributions in binary form must reproduce the above copyright notice,
12a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * this list of conditions and the following disclaimer in the documentation and/or
13a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * other materials provided with the distribution.
14a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop *
15a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * 3. Neither the name of the copyright holder nor the names of its contributors
16a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * may be used to endorse or promote products derived from this software without
17a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * specific prior written permission.
18a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop *
19a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop */
30a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop
31a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop#include "LogarithmicParameterAdaptation.h"
329368eea42a1afb01dd44110582f997115b50e742François Gaffie#include <cmath>
339368eea42a1afb01dd44110582f997115b50e742François Gaffie#include <limits>
349368eea42a1afb01dd44110582f997115b50e742François Gaffie#include <algorithm>
35a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop
36a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop#define base CLinearParameterAdaptation
37a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop
389368eea42a1afb01dd44110582f997115b50e742François GaffieCLogarithmicParameterAdaptation::CLogarithmicParameterAdaptation() : base("Logarithmic")
39a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop{
409368eea42a1afb01dd44110582f997115b50e742François Gaffie    static_assert(std::numeric_limits<double>::is_iec559,
419368eea42a1afb01dd44110582f997115b50e742François Gaffie                  "Only double-precision floating points that are compliant with"
429368eea42a1afb01dd44110582f997115b50e742François Gaffie                  " IEC 559 (aka IEEE 754) are supported");
43a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop}
44a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop
45a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop// Element properties
469368eea42a1afb01dd44110582f997115b50e742François Gaffievoid CLogarithmicParameterAdaptation::showProperties(std::string &strResult) const
47a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop{
48a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop    base::showProperties(strResult);
49a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop
50a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop    strResult += " - LogarithmBase: ";
519368eea42a1afb01dd44110582f997115b50e742François Gaffie    strResult += std::to_string(_dLogarithmBase);
52a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop    strResult += "\n";
53a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop    strResult += " - FloorValue: ";
549368eea42a1afb01dd44110582f997115b50e742François Gaffie    strResult += std::to_string(_dFloorValue);
55a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop    strResult += "\n";
56a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop}
57a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop
589368eea42a1afb01dd44110582f997115b50e742François Gaffiebool CLogarithmicParameterAdaptation::fromXml(const CXmlElement &xmlElement,
599368eea42a1afb01dd44110582f997115b50e742François Gaffie                                              CXmlSerializingContext &serializingContext)
60a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop{
619368eea42a1afb01dd44110582f997115b50e742François Gaffie    if (xmlElement.getAttribute("LogarithmBase", _dLogarithmBase) &&
629368eea42a1afb01dd44110582f997115b50e742François Gaffie        (_dLogarithmBase <= 0 || _dLogarithmBase == 1)) {
63a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop        // Avoid negative and 1 values
649368eea42a1afb01dd44110582f997115b50e742François Gaffie        serializingContext.setError("LogarithmBase attribute cannot be negative or 1 on element" +
659368eea42a1afb01dd44110582f997115b50e742François Gaffie                                    xmlElement.getPath());
66a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop
679368eea42a1afb01dd44110582f997115b50e742François Gaffie        return false;
68a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop    }
69a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop
709368eea42a1afb01dd44110582f997115b50e742François Gaffie    xmlElement.getAttribute("FloorValue", _dFloorValue);
719368eea42a1afb01dd44110582f997115b50e742François Gaffie
72a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop    // Base
73a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop    return base::fromXml(xmlElement, serializingContext);
74a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop}
75a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop
769368eea42a1afb01dd44110582f997115b50e742François Gaffieint64_t CLogarithmicParameterAdaptation::fromUserValue(double value) const
77a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop{
789368eea42a1afb01dd44110582f997115b50e742François Gaffie    return std::max(base::fromUserValue(log(value) / log(_dLogarithmBase)),
799368eea42a1afb01dd44110582f997115b50e742François Gaffie                    static_cast<int64_t>(_dFloorValue));
80a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop}
81a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop
82a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diopdouble CLogarithmicParameterAdaptation::toUserValue(int64_t iValue) const
83a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop{
84a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop    return exp(base::toUserValue(iValue) * log(_dLogarithmBase));
85a1fdb2965956d3cbc62e6f8b29405bdeef79dca3Piotr Diop}
86