Skip to content

Instantly share code, notes, and snippets.

View folkertdev's full-sized avatar

Folkert de Vries folkertdev

View GitHub Profile
@folkertdev
folkertdev / Smallest.hs
Last active February 19, 2018 19:42
n-th smallest number in a matrix where the row and column are monotonically increasing
{-# LANGUAGE ScopedTypeVariables #-}
module Smallest where
type List a = [ a]
merge :: Ord a => List a -> List a -> List a
merge as bs =
case (as, bs) of
([], _) -> bs
(_, []) -> as
@folkertdev
folkertdev / FixedRadius.elm
Created June 8, 2018 14:56
Fixed radius near neighbours in elm
module FixedRadius exposing (Buckets, Point, fromList, search, searchReflexive, map)
{-| A module for fixed-radious near neighbours
@docs Point, Buckets, fromList, search, searchReflexive, map
-}
import Dict exposing (Dict)
@folkertdev
folkertdev / RTree.elm
Created August 4, 2018 21:01
rtree in elm
module RTree exposing (..)
import BoundingBox2d exposing (BoundingBox2d)
import Point2d exposing (Point2d)
import List.Extra as List
-- bounding boxes can go to infintiy
-- minNode <= maxNode // 2
-- the root node has at least 2 children, unless it is a leaf
@folkertdev
folkertdev / RTree.elm
Created August 8, 2018 21:01
A more optimized/less readable RTree
module RTree exposing (..)
import BoundingBox2d exposing (BoundingBox2d)
import Point2d exposing (Point2d)
import List.Extra as List
-- bounding boxes can go to infintiy
-- minNode <= maxNode // 2
-- the root node has at least 2 children, unless it is a leaf
@folkertdev
folkertdev / Main.elm
Created August 19, 2018 16:35
Sutherland-Hodgman polygon clipping
module Main exposing (..)
import Point2d exposing (Point2d)
import Svg exposing (Svg)
import Svg.Attributes exposing (fill, strokeWidth, stroke, width, height, viewBox, cx, cy, r, strokeOpacity)
import Html exposing (text)
import Geometry.Svg
import Polygon2d
import Html.Attributes exposing (style, defaultValue)
import BoundingBox2d
@folkertdev
folkertdev / villages.smv
Last active January 7, 2019 10:47
nusmv villages
MODULE main
VAR
truck : 0..320;
location : { a, b, s,c };
nextLocation : { a, b, s,c};
stockAtA : -1..120;
stockAtB : -1..120;
stockAtC : -1..200;
DEFINE
@folkertdev
folkertdev / Issue4.elm
Created December 30, 2019 19:00
bug in elm-image
module Issue4 exposing (suite)
import Bytes exposing (Bytes)
import Bytes.Encode as Encode
import Expect
import Image
import Test exposing (test)
suite =
@folkertdev
folkertdev / setjmplongjmp.md
Created August 29, 2021 10:38
setjmp/longjmp in LLVM IR

Using setjmp/longjmp in LLVM IR

Using the setjmp and longjmp LLVM instrinsics did not work as expected. There is an undocumented difference between the C implementation and the LLVM intrinsics.

setjmp/longjmp in C

setjmp/longjmp (sjlj for short) is a mechanism in C that is used to handle errors, potentially deep in the call stack.

With setjmp, the current stack frame is saved to some location in memory. Then down the line a lngjmp can jump back to where setjmp was called. The stack is then restored, and the return value of the setjmp call is different between the first call, when normal execution brought use there, and a subsequent call that is the result of a jump. The different return value allows us to distinguish how we got to the setjmp and act accordingly.

@folkertdev
folkertdev / bilinear.c
Created September 2, 2021 13:42
bilinear interpolation in C
/// Sources:
//
// - https://chao-ji.github.io/jekyll/update/2018/07/19/BilinearResize.html
void bilinear_interpolation(float *data, uint32_t input_width,
uint32_t input_height, uint32_t output_width,
uint32_t output_height, float *output) {
float x_ratio, y_ratio;
if (output_width > 1) {
x_ratio = ((float)input_width - 1.0) / ((float)output_width - 1.0);
@folkertdev
folkertdev / bicubic.c
Created September 2, 2021 13:46
bicubic interpolation in C
float cubic_interpolate(
float p0, float p1, float p2, float p3, float ratio, float cubic_coeff_a) {
float cca = cubic_coeff_a;
float coeffects_0 = ((cca * (ratio + 1) - 5 * cca) * (ratio + 1) + 8 * cca) * (ratio + 1) - 4 * cca;
float coeffects_1 = ((cca + 2) * ratio - (cca + 3)) * ratio * ratio + 1;
float coeffects_2 = ((cca + 2) * (1 - ratio) - (cca + 3)) * (1 - ratio) * (1 - ratio) + 1;
float coeffects_3 = ((cca * ((1 - ratio) + 1) - 5 * cca) * ((1 - ratio) + 1) + 8 * cca) * ((1 - ratio) + 1) - 4 * cca;