Skip to content

Instantly share code, notes, and snippets.

View tau-dev's full-sized avatar
💭
𒈙

Tau tau-dev

💭
𒈙
  • Department of High Energy Magic
  • Ankh-Morpork
  • 12:48 (UTC +01:00)
View GitHub Profile
@gruebite
gruebite / main.zig
Last active April 26, 2021 22:04
Zig dynamic dispatch with @fieldParentPtr
const print = @import("std").debug.print;
const Animal = struct {
const Self = @This();
speakFn: fn(*const Animal) void,
pub fn speak(self: *const Self) void {
self.speakFn(self);
template <class _Ty>
_NODISCARD /* constexpr */ _Ty _Common_lerp(const _Ty _ArgA, const _Ty _ArgB, const _Ty _ArgT) noexcept {
// on a line intersecting {(0.0, _ArgA), (1.0, _ArgB)}, return the Y value for X == _ArgT
const int _Finite_mask = (int{isfinite(_ArgA)} << 2) | (int{isfinite(_ArgB)} << 1) | int{isfinite(_ArgT)};
if (_Finite_mask == 0b111) {
// 99% case, put it first; this block comes from P0811R3
if ((_ArgA <= 0 && _ArgB >= 0) || (_ArgA >= 0 && _ArgB <= 0)) {
// exact, monotonic, bounded, determinate, and (for _ArgA == _ArgB == 0) consistent:
return _ArgT * _ArgB + (1 - _ArgT) * _ArgA;