Skip to content

Instantly share code, notes, and snippets.

View lovely-error's full-sized avatar
😆
Don't forget that you will die some day

Enigma Lav lovely-error

😆
Don't forget that you will die some day
View GitHub Profile
@lovely-error
lovely-error / vect_as_vv.agda
Last active May 29, 2024 00:25
Vect as W type
{-# OPTIONS --type-in-type #-}
open import Agda.Builtin.Nat
open import Agda.Builtin.Bool
data Prod (L R : Set) : Set where
pair : L -> R -> Prod L R
data Unit : Set where
pt : Unit
@lovely-error
lovely-error / mod_eqn_solver.rs
Last active May 23, 2024 10:43
Solve (N * K) mod 2^64 = 1
// for given number N, this function finds another number K such that
// (N * K) mod 2^64 = 1
fn mult_mod_inv(n: u64) -> u64 {
assert!(n & 1 == 1, "Doesnt work for even numbers");
assert!(n != 0, "Equation 0 mod N == 1 does not have a solution");
let mut k = 1u64;
let mut i = 1u64;
for _ in 0 .. 64 {
i <<= 1;
let c = k.wrapping_mul(n);
@lovely-error
lovely-error / ocl_enable.md
Last active May 17, 2024 17:35
Get OpenCL working on RX580 amdgpu using APT
  1. Create this file sudo touch /etc/apt/sources.list.d/rocm.list
  2. put this line in there deb [arch=amd64] https://repo.radeon.com/rocm/apt/3.5.1/ xenial main
  3. sudo apt update
  4. install ocl sudo apt install rocm-opencl3.5.0
  5. this script doesnt write icd path to /etc/OpenCL/vendors/ so that's on us
  6. touch /etc/OpenCL/vendors/amdocl64.icd
  7. echo "/opt/rocm-3.5.1/opencl/lib/libamdocl64.so" > /etc/OpenCL/vendors/amdocl64.icd
  8. done. run clinfo. thing should work at this point
@lovely-error
lovely-error / kern.cl
Last active March 6, 2024 07:52
Conditional execution without branches on gpu
// this kernel writes a value only if invoaction index is even *_*
// when _flag is true, the store adress gets computed to be beyound (1 << 48) byte location .
// apparently stores beyound valid adress space on gpu... just become noops!
#define store_if_true(Ty, addr, val, flag) \
*((__global Ty*)(((unsigned long)addr) + (((unsigned long)(flag == 0)) * ((1LU << 48) - ((unsigned long)addr))))) = val;
__kernel void kern(__global unsigned int* buffer) {
@lovely-error
lovely-error / crapy_integ.hs
Last active February 21, 2024 07:21
Computing definite integral over sinus in range [0;pi] (with terrible precision :3 ~2.0097% deviation from analytic solution)
-- basically this in wolfram alpha terms
-- 2*(Integrate[sin\(40)x\(41),{x,0,Divide[pi,4]}]+Integrate[sin\(40)x\(41),{x,Divide[pi,4],Divide[pi,2]}])
-- https://www.desmos.com/calculator/1fokxfyphz
-- https://www.wolframalpha.com/input?i2d=true&i=Integrate%5Bsin%5C%2840%29x%5C%2841%29%2C%7Bx%2C0%2Cpi%7D%5D
-- https://www.wolframalpha.com/input?i2d=true&i=2*%5C%2840%29Integrate%5Bsin%5C%2840%29x%5C%2841%29%2C%7Bx%2C0%2CDivide%5Bpi%2C4%5D%7D%5D%2BIntegrate%5Bsin%5C%2840%29x%5C%2841%29%2C%7Bx%2CDivide%5Bpi%2C4%5D%2CDivide%5Bpi%2C2%5D%7D%5D%5C%2841%29
import Text.Printf (printf)
@lovely-error
lovely-error / ptrs.zig
Created January 13, 2024 10:06
zig pointer utils
const std = @import("std");
pub const @"1KiB of bytes": comptime_int = 1024;
pub const @"1MiB of bytes": comptime_int = 1024 * 1024;
pub const @"1GiB of bytes": comptime_int = 1 << 30;
pub const @"Byte amount fitting within 48 bit address space": comptime_int = (1 << 48) - 1;
pub fn Ptr(comptime Pointee: type, comptime is_mutable: bool) type {
const Ty = if (is_mutable) *Pointee else *const Pointee;
return struct {
@lovely-error
lovely-error / erase.zig
Last active January 15, 2024 12:48
Easified type erasure in zig
pub fn main() !void {
const AbstractedObject = Opaque(&.{
.{"f1", fn (*anyopaque) u1},
.{"f2", fn (*anyopaque, u8, u8, u8, u8) L},
.{"ferry", fn (*anyopaque)error{boo}!void},
});
var a = std.heap.ArenaAllocator.init(std.heap.page_allocator);
var T = K {};
@lovely-error
lovely-error / lol.agda
Last active January 13, 2024 10:03
automorphisms of Z2
{-# OPTIONS --type-in-type #-}
open import Agda.Builtin.Equality
open import Agda.Builtin.Bool
open import Agda.Builtin.Nat
open import Agda.Builtin.Sigma
open import Agda.Builtin.List
data Void : Set where
@lovely-error
lovely-error / kal.hs
Created August 3, 2023 08:26
The world simplest calculator with infix expressions and parens
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PartialTypeSignatures #-}
import Data.Char (isNumber, isSpace)
infixl |>
v |> f = f v
main = do
print "Hey! I am ready to accept input :)"
@lovely-error
lovely-error / hs.hs
Created April 20, 2023 14:24
Stochastic bin packing algorithm, kinda. Was made for one of my lovers
import Data.List
infixl ///
class LaxDivision a where
(///) :: a -> a -> a
instance LaxDivision Int where
(///) = div