Skip to content

Instantly share code, notes, and snippets.

View nandor's full-sized avatar

Nandor Licker nandor

  • SiFive
  • Cluj-Napoca, Romania
  • 12:06 (UTC +03:00)
View GitHub Profile
--------------------------------------------------------------------------------
-- Huffman coding
--------------------------------------------------------------------------------
import Data.Maybe
import Data.List (nub, sort)
data Huffman a = Tip Int a
| Node Int (Huffman a) (Huffman a)
--------------------------------------------------------------------------------
-- Checks if there is a high probability of the number being prime using the
-- Miller-Rabin primality test
-- modPow must be used with the Integer type so that the multiplication won't
-- overflow because n could be >= sqrt(maxInt)
--------------------------------------------------------------------------------
isPrimeMR :: Int -> Bool
isPrimeMR n
| n < 2 = False
| n /= 2 && n `mod` 2 == 0 = False
@nandor
nandor / quine.c
Created November 15, 2013 20:55
C quine
#include <stdio.h>
#define S(x) char*src=#x;x
S(void main(){printf("#include <stdio.h>\n#define S(x) char*src=#x;x\nS(%s)\n",src);})
-- |3D Vector
data Vec3 a
= Vec3 { v3x :: a
, v3y :: a
, v3z :: a
}
deriving ( Eq, Ord, Show )
-- |Creates a 3D vector
@nandor
nandor / Main.hs
Last active December 29, 2015 20:19
Quadrocopter viewer
{-# LANGUAGE NamedFieldPuns, RecordWildCards #-}
--------------------------------------------------------------------------------
-- OpenGL quadrocopter viewer
--------------------------------------------------------------------------------
module Main where
import Data.IORef
import Control.Monad
import Control.Applicative
@nandor
nandor / gist:8407239
Created January 13, 2014 20:15
Fast SSE matrix multiplication. It might be three times faster.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <xmmintrin.h>
typedef float __attribute__((aligned(16))) mat4[16];
void
mat4_mul_sse(mat4 d, mat4 a, mat4 b)
{
@nandor
nandor / gist:8505422
Created January 19, 2014 14:03
Boolean expression checker
module Main where
import Control.Monad
table :: [ [ Bool ] ]
table
= sequence $ replicate 4 [False, True]
eval :: [ Bool ] -> [ Bool ]
eval [ d, q2, q1, q0 ]
@nandor
nandor / gist:8509693
Created January 19, 2014 19:20
grammar
/* Lexer for the mini language */
%lex
%%
\s+ /* skip whitespace */
"func" return 'FUNC';
"return" return 'RETURN';
"if" return 'IF';
"else" return 'ELSE';
"while" return 'WHILE';
test:
push %rbp
mov %rsp, %rbp
push %rax # d
push %rbx # b
push %rcx # c
push %rdx # Changed by imul
@nandor
nandor / fstrcmp.s
Created January 23, 2014 17:34
Implementation of strcmp with pcmpistri
.text
.globl fstrcmp
.type fstrcmp, @function
fstrcmp:
# If the address is already aligned, skip
testb $15, %dil
jz 1f
# If the first string is empty, quit
movsbl (%rdi), %edx