/* Generated by EML-lang AUTOSAR backend */
/* Source module: aerospace_actuator_guard_band */
/* Kernel digest: sha256:0b0ef5a5baecb59acc7d28dc64bc959d9f3378cfbc278204c737cef5bc755aaa */
/* SWC:           AerospaceActuatorGuardBand */
/* Runnable:      Run_actuator_command_within_band */

#include "Rte_AerospaceActuatorGuardBand.h"  /* AUTOSAR RTE header for this SWC */

/* ── Embedded controller (from EML-lang C backend) ───── */

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

#include "libmonogate.h"
#include <stdint.h>
#include <math.h>

/* 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
#include <stdio.h>
#include <stdlib.h>

/* 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)) { \
        fprintf(stderr, "EML contract violated: %s\n", (msg)); \
        abort(); } } while (0)

/*
 * actuator_command_within_band
 * Pfaffian chain count (eml-cost pfaffian_r): 0     Cost class: p0-d4-w0-c0
 * EML depth:   4  Symbolic band: LOW (from pfaffian_r only)
 * Numerical:   cancellation exposure NONE  (no mixed-sign subtraction)
 * Dynamics:    0 osc, 0 decay  (predicted_r=0)
 * 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
 * FPGA est:   3 variable multiply 12 DSP48E1 [uncalibrated] -> 8 cy @ 32-bit
 */
double actuator_command_within_band(double error, double integral, double deriv, double kp, double ki, double kd, double u_min, double u_max) {
    EML_CONTRACT(((u_min <= u_max)), "actuator_command_within_band: requires ((u_min <= u_max))");
    double result = mg_clamp((((kp * error) + (ki * integral)) + (kd * deriv)), u_min, 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;
}

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

/* ── AUTOSAR runnable wrapper ───────────────────────── */

FUNC(void, RTE_CODE) Run_actuator_command_within_band(void) {
    /* read error via RTE sender-receiver port */
    float64 error;
    Rte_Read_error_error(&error);
    /* read integral via RTE sender-receiver port */
    float64 integral;
    Rte_Read_integral_integral(&integral);
    /* read deriv via RTE sender-receiver port */
    float64 deriv;
    Rte_Read_deriv_deriv(&deriv);
    /* read kp via RTE sender-receiver port */
    float64 kp;
    Rte_Read_kp_kp(&kp);
    /* read ki via RTE sender-receiver port */
    float64 ki;
    Rte_Read_ki_ki(&ki);
    /* read kd via RTE sender-receiver port */
    float64 kd;
    Rte_Read_kd_kd(&kd);
    /* read u_min via RTE sender-receiver port */
    float64 u_min;
    Rte_Read_u_min_u_min(&u_min);
    /* read u_max via RTE sender-receiver port */
    float64 u_max;
    Rte_Read_u_max_u_max(&u_max);

    /* Call the Forge-emitted controller */
    const float64 result = actuator_command_within_band(error, integral, deriv, kp, ki, kd, u_min, u_max);

    /* Publish the result via the RTE sender port */
    Rte_Write_result_result(result);
}
