1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
25392f7a3SLiteSpeed Tech#ifndef LSQUIC_MINMAX_H
35392f7a3SLiteSpeed Tech#define LSQUIC_MINMAX_H
45392f7a3SLiteSpeed Tech
55392f7a3SLiteSpeed Tech/* Windowed min/max tracker by Kathleen Nichols.
65392f7a3SLiteSpeed Tech *
75392f7a3SLiteSpeed Tech * Based on Google code released under BSD license here:
85392f7a3SLiteSpeed Tech *  https://groups.google.com/forum/#!topic/bbr-dev/3RTgkzi5ZD8
95392f7a3SLiteSpeed Tech */
105392f7a3SLiteSpeed Tech
115392f7a3SLiteSpeed Tech
125392f7a3SLiteSpeed Techstruct minmax_sample
135392f7a3SLiteSpeed Tech{
145392f7a3SLiteSpeed Tech    uint64_t    time;
155392f7a3SLiteSpeed Tech    uint64_t    value;
165392f7a3SLiteSpeed Tech};
175392f7a3SLiteSpeed Tech
185392f7a3SLiteSpeed Techstruct minmax
195392f7a3SLiteSpeed Tech{
205392f7a3SLiteSpeed Tech    uint64_t                window;
215392f7a3SLiteSpeed Tech    struct minmax_sample    samples[3];
225392f7a3SLiteSpeed Tech};
235392f7a3SLiteSpeed Tech
245392f7a3SLiteSpeed Tech#define minmax_get_idx(minmax_, idx_) ((minmax_)->samples[idx_].value)
255392f7a3SLiteSpeed Tech
265392f7a3SLiteSpeed Tech#define minmax_get(minmax_) minmax_get_idx(minmax_, 0)
275392f7a3SLiteSpeed Tech
285392f7a3SLiteSpeed Tech#define minmax_reset(minmax_, sample_) do {                             \
295392f7a3SLiteSpeed Tech    (minmax_)->samples[0] = (minmax_)->samples[1]                       \
305392f7a3SLiteSpeed Tech        = (minmax_)->samples[2] = (sample_);                            \
315392f7a3SLiteSpeed Tech} while (0)
325392f7a3SLiteSpeed Tech
335392f7a3SLiteSpeed Tech#define minmax_init(minmax_, window_) do {                              \
345392f7a3SLiteSpeed Tech    (minmax_)->window = (window_);                                      \
355392f7a3SLiteSpeed Tech    minmax_reset(minmax_, ((struct minmax_sample) { 0, 0, }));          \
365392f7a3SLiteSpeed Tech} while (0)
375392f7a3SLiteSpeed Tech
385392f7a3SLiteSpeed Techvoid lsquic_minmax_update_min(struct minmax *, uint64_t now, uint64_t meas);
395392f7a3SLiteSpeed Techvoid lsquic_minmax_update_max(struct minmax *, uint64_t now, uint64_t meas);
405392f7a3SLiteSpeed Tech
415392f7a3SLiteSpeed Tech#define minmax_upmin lsquic_minmax_update_min
425392f7a3SLiteSpeed Tech#define minmax_upmax lsquic_minmax_update_max
435392f7a3SLiteSpeed Tech
445392f7a3SLiteSpeed Tech#endif
45