// Generated by EML-lang C++17 backend
// Source module: aerospace_actuator_guard_band
// Kernel digest: sha256:0b0ef5a5baecb59acc7d28dc64bc959d9f3378cfbc278204c737cef5bc755aaa
// Functions:     1
// Constants:     0

#pragma once

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>

/* Contract checks. NOT assert(): -DNDEBUG would delete them, and a release
 * build that silently drops every `requires` and `ensures` is exactly the
 * obligation attenuation this compiler exists to report. */
#define EML_CONTRACT(cond, msg) \
    do { if (!(cond)) { \
        std::fprintf(stderr, "EML contract violated: %s\n", (msg)); \
        std::abort(); } } while (0)

/* UNFUSED BY DEFAULT. `a + b * c` in EML is an add of a ROUNDED product; the fused operation is
 * written `fma(a, b, c)` and is emitted as a call to `fma`, which still contracts. GCC contracts
 * `a + b*c` into one fused multiply-add at -O2 and above for C++ in every dialect and for C in the
 * GNU dialect a bare `gcc file.c` uses, which would change this artifact's last bits. The pragma
 * below turns that off for THIS FILE ONLY -- `pop_options` at the end restores whatever the caller
 * had, so an #include of this header does not change how the includer is compiled.
 *
 * THIS OVERRIDES -ffast-math, ON PURPOSE. Building with -ffast-math does NOT give the fused
 * multiply-adds back: the pragma wins, and everything else -ffast-math does still applies
 * (measured on aarch64 g++/gcc 13.3.0 -- with the pragma the only instructions that change are
 * the fused multiply-adds, which become multiply-then-add). A build flag is not where this
 * decision belongs: -ffast-math is routinely inherited from CFLAGS nobody in this kernel's
 * review chain chose, and a kernel whose last bits move because of an ambient compiler option
 * is the failure this compiler exists to prevent. Ask for the fused operation IN THE SOURCE,
 * by writing fma(a, b, c) -- that still compiles to one fused multiply-add, here, inside this
 * region.
 *
 * The pragma costs no optimization at any -O level, and does not drop the flags you passed:
 * -funroll-loops still unrolls, -fno-strict-aliasing and -ffast-math are otherwise untouched.
 * If your compiler is neither GCC nor clang, build with the equivalent of -ffp-contract=off. */
#if defined(__clang__)
#pragma STDC FP_CONTRACT OFF
#elif defined(__GNUC__)
#pragma GCC push_options
#pragma GCC optimize("fp-contract=off")
#else
#pragma STDC FP_CONTRACT OFF
#endif

namespace forge::aerospace_actuator_guard_band {

[[nodiscard]] constexpr double eml_builtin_max(double a, double b) noexcept { return a != a ? a : b != b ? b : a < b ? b : b < a ? a : (a == 0.0 ? a + b : a); }
[[nodiscard]] constexpr double eml_builtin_min(double a, double b) noexcept { return a != a ? a : b != b ? b : b < a ? b : a < b ? a : (a == 0.0 ? -((-a) + (-b)) : a); }

/**
 * @brief actuator_command_within_band
 * @profile pfaffian_r=0  cost_class=p0-d4-w0-c0
 *          eml_depth=4  drift_risk=LOW
 *
 * @pre  (u_min <= u_max)
 * @post (u_min <= result)
 * @post (result <= u_max)
 *  source obligations for actuator_command_within_band: {O1, O2}
 *    O1 [43d7c3ff9568] -> PRESERVED (equivalent)  EML_CONTRACT before return; survives -DNDEBUG
 *         build: unconditional -- EML_CONTRACT is a self-contained if/abort, NOT assert(), so -DNDEBUG cannot delete it
 *    O2 [ca2e303109b7] -> PRESERVED (equivalent)  EML_CONTRACT before return; survives -DNDEBUG
 *         build: unconditional -- EML_CONTRACT is a self-contained if/abort, NOT assert(), so -DNDEBUG cannot delete it
 * @verify lean theorem=actuator_command_within_band
 */
[[nodiscard]] constexpr double actuator_command_within_band(double error, double integral, double deriv, double kp, double ki, double kd, double u_min, double u_max) noexcept {
  EML_CONTRACT(((u_min <= u_max)), "actuator_command_within_band: requires ((u_min <= u_max))");
  double result = eml_builtin_max(u_min, eml_builtin_min((((kp * error) + (ki * integral)) + (kd * deriv)), u_max));
  EML_CONTRACT(((u_min <= result)), "actuator_command_within_band: ensures violated: (u_min <= result)");
  EML_CONTRACT(((result <= u_max)), "actuator_command_within_band: ensures violated: (result <= u_max)");
  return result;
}

}  // namespace forge::aerospace_actuator_guard_band
#if !defined(__clang__) && defined(__GNUC__)
#pragma GCC pop_options  /* end of the unfused region: see the note at the top of this file */
#endif
