panini4b / panini_micro_kernel.h
18kansal's picture
Deploy Panini Bare-Metal Embedded Micro-Kernel (C89/MISRA-C) for 8-bit/16-bit/32-bit Microprocessors
d49f074 verified
Raw History Blame Contribute Delete
3.27 kB
/**
* panini_micro_kernel.h
*
* =============================================================================
* PANINI BARE-METAL EMBEDDED MICRO-KERNEL (C89 / MISRA-C COMPLIANT)
* =============================================================================
*
* Target Architectures:
* - 8-bit Microcontrollers (AVR / ATmega328P / Arduino: 2 KB SRAM)
* - 16-bit Microcontrollers (MSP430 / PIC24: 4 KB - 16 KB SRAM)
* - 32-bit Microprocessors (ARM Cortex-M0/M3/M4/M7, ESP32, RISC-V, STM32)
* - 64-bit Application CPUs (x86_64, AArch64)
*
* Invariants & Constraints:
* 1. ZERO dynamic memory allocations (No malloc / free / heap).
* 2. Strict O(1) stack memory consumption (< 128 bytes total).
* 3. 100% deterministic integer arithmetic (No floating-point unit required).
*/
#ifndef PANINI_MICRO_KERNEL_H
#define PANINI_MICRO_KERNEL_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/* --- SŪTRA 3: ŪRDHVA-TIRYAGBHYĀM (Cross-Convolution 1D Tensor Multiply) --- */
/* Computes polynomial multiplication in-place without heap allocations. */
static inline void panini_urdhva_conv(
const int16_t* polyA, uint8_t lenA,
const int16_t* polyB, uint8_t lenB,
int32_t* outResult
) {
uint8_t i, j;
uint8_t outLen = lenA + lenB - 1;
for (i = 0; i < outLen; ++i) {
outResult[i] = 0;
}
for (i = 0; i < lenA; ++i) {
int32_t a_val = polyA[i];
for (j = 0; j < lenB; ++j) {
outResult[i + j] += a_val * polyB[j];
}
}
}
/* --- SŪTRA 10: YĀVADŪNAM (Base Deficiency Squaring) --- */
/* Computes square of numbers near power-of-10 bases: (B - d)^2 = (B - 2d)B + d^2 */
static inline int32_t panini_yavadunam_square(int16_t num, int16_t base) {
int16_t diff = base - num;
return ((int32_t)(num - diff) * base) + ((int32_t)diff * diff);
}
/* --- SŪTRA 1: EKĀDHIKENA PŪRVEṆA (Fast Squaring of numbers ending in 5) --- */
/* Example: 35^2 -> prefix 3 * (3+1) = 12 concat 25 = 1225 */
static inline int32_t panini_ekadhikena_square_five(int16_t prefix) {
int32_t part1 = (int32_t)prefix * (prefix + 1);
return (part1 * 100) + 25;
}
/* --- PĀṆINI 1.4.2: VIPRATIṢEDHE PARAṀ KĀRYAM (Conflict Arbiter) --- */
/* Priority: Authority Tier (High > Low). If equal, Recency (Rule B > Rule A). */
typedef struct {
uint8_t ruleId;
uint8_t authorityTier;
uint8_t actionPayload;
} PaniniRule;
static inline PaniniRule panini_vipratisedha_resolve(PaniniRule ruleA, PaniniRule ruleB) {
if (ruleA.authorityTier > ruleB.authorityTier) {
return ruleA;
}
return ruleB; /* Later rule / higher authority wins */
}
/* --- SŪRYA SIDDHĀNTA: INTEGER FIXED-POINT SOLAR LONGITUDE (Q16.16) --- */
/* Evaluates true solar longitude on microcontrollers without FPU. */
static inline int32_t panini_surya_integer_longitude(int32_t dayOffset) {
/* Fixed-point scale factor: 2^16 = 65536 */
/* Mean daily motion = 360 / 365.258756 ≈ 0.9856 degrees -> 64592 in Q16 */
int32_t meanLong = (dayOffset * 64592) % (360 * 65536);
if (meanLong < 0) meanLong += (360 * 65536);
return meanLong >> 16; /* Returns integer degrees [0, 359] */
}
#ifdef __cplusplus
}
#endif
#endif /* PANINI_MICRO_KERNEL_H */