File: PreciseSum.h

package info (click to toggle)
webkit2gtk 2.51.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 455,340 kB
  • sloc: cpp: 3,865,253; javascript: 197,710; ansic: 165,177; python: 49,241; asm: 21,868; ruby: 18,095; perl: 16,926; xml: 4,623; sh: 2,409; yacc: 2,356; java: 2,019; lex: 1,330; pascal: 372; makefile: 210
file content (136 lines) | stat: -rw-r--r-- 4,335 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2024-2025 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include <wtf/Vector.h>

namespace WTF {

namespace Xsum {

struct SmallAccumulator final {
    explicit SmallAccumulator();
    explicit SmallAccumulator(
        Vector<int64_t> &&chunk, const int addsUntilPropagate, const int64_t inf,
        const int64_t nan, const size_t sizeCount, const bool hasPosNumber
    );

    Vector<int64_t> chunk; // Chunks making up small accumulator
    int addsUntilPropagate; // Number of remaining adds before carry
    int64_t inf; // If non-zero, +Inf, -Inf, or NaN
    int64_t nan; // If non-zero, a NaN value with payload
    size_t sizeCount; // number of added values
    bool hasPosNumber; // check if added values have at least one positive number

    int carryPropagate();
    COLD void addInfNan(int64_t ivalue);
    inline void add1NoCarry(double value);
    ALWAYS_INLINE void incrementWhenValueAdded(double value);
};

struct LargeAccumulator final {
    Vector<uint64_t> chunk; // Chunks making up large accumulator
    Vector<int_least16_t> count; // Counts of # adds remaining for chunks, or -1 if not used yet or special
    Vector<uint64_t> chunksUsed; // Bits indicate chunks in use
    uint64_t usedUsed; // Bits indicate chunk_used entries not 0
    SmallAccumulator sacc; // The small accumulator to condense into

    explicit LargeAccumulator();

    void addLchunkToSmall(int_fast16_t ix);
    COLD void largeAddValueInfNan(int_fast16_t ix, uint64_t uintv);
    void transferToSmall();
};

class XsumInterface {
public:
    virtual ~XsumInterface() = default;

    virtual void addList(const std::span<const double> vec) = 0;
    virtual void add(double value) = 0;
    virtual double compute() = 0;
};

class XsumSmall final : public XsumInterface {
public:
    explicit XsumSmall();
    explicit XsumSmall(SmallAccumulator sacc);

    void addList(const std::span<const double> vec) override;
    void add(double value) override;
    double compute() override;

private:
    SmallAccumulator m_smallAccumulator;
};

class XsumLarge final : public XsumInterface {
public:
    explicit XsumLarge();

    void addList(const std::span<const double> vec) override;
    void add(double value) override;
    double compute() override;

private:
    LargeAccumulator m_largeAccumulator;
};

} // namespace Xsum

// Threshold for PreciseSum to determine whether to use XsumSmall or XsumLarge
// If the expected array length is greater than PRECISE_SUM_THRESHOLD, use Xsum::XsumLarge;
// otherwise use Xsum::XsumSmall
constexpr uint64_t PRECISE_SUM_THRESHOLD = 1'000;

template<std::derived_from<Xsum::XsumInterface> T = Xsum::XsumSmall>
class PreciseSum final {
public:
    explicit PreciseSum()
        : m_xsum { T { } } { }

    void addList(const std::span<const double> vec)
    {
        m_xsum.addList(vec);
    }

    void add(double value)
    {
        m_xsum.add(value);
    }

    double compute()
    {
        return m_xsum.compute();
    }

private:
    T m_xsum;
};

} // namespace WTF

using WTF::PreciseSum;