Skip to content

Instantly share code, notes, and snippets.

View Youka's full-sized avatar

Youka Youka

  • Germany
  • 07:49 (UTC +01:00)
View GitHub Profile
@Youka
Youka / color_picker.lua
Last active July 15, 2022 03:36
Picking colors at mouse position with LuaJIT
-- Load FFI
local ffi = require("ffi")
-- Define FFI functions & structures by OS
local x11
if ffi.os == "Windows" then
ffi.cdef([[
typedef int BOOL;
typedef long LONG;
typedef struct{
@Youka
Youka / windows_nanosleep.c
Created November 22, 2014 13:44
Windows nanosleep
#include <windows.h> /* WinAPI */
/* Windows sleep in 100ns units */
BOOLEAN nanosleep(LONGLONG ns){
/* Declarations */
HANDLE timer; /* Timer handle */
LARGE_INTEGER li; /* Time defintion */
/* Create timer */
if(!(timer = CreateWaitableTimer(NULL, TRUE, NULL)))
return FALSE;
@Youka
Youka / main.c
Last active June 30, 2024 16:23
Windows SAPI (MinGW) test
#include "sapi.h"
#include <stdio.h>
// Program entry
int main(){
// Initialize COM
if(CoInitializeEx(NULL, COINIT_MULTITHREADED))
return 1;
puts("COM initialized.");
// Create SAPI voice
@Youka
Youka / ruby_interpreter.lua
Created December 30, 2014 11:25
Execute & bind Ruby by LuaJIT
-- Load foreign functions interface
local ffi = require("ffi")
-- Load Ruby library & define function headers
local ruby = ffi.load("msvcrt-ruby210")
ffi.cdef([[
typedef uintptr_t RUBY_VALUE;
enum{
RUBY_Qfalse = 0,
RUBY_Qtrue = 2,
@Youka
Youka / fiddle_test.rb
Created December 30, 2014 13:21
Simple use of ruby's fiddle library for windows messageboxes
# Load importer part of fiddle (ffi) library
require 'fiddle/import'
# Create module as body for an importer instance
module MessageBox
# Extend this module to an importer
extend Fiddle::Importer
# Load 'user32' dynamic library into this importer
dlload 'user32'
# Set C aliases to this importer for further understanding of function signatures
@Youka
Youka / mem2func.lua
Last active January 30, 2023 07:33
Run machine code with LuaJIT (Windows x86)
-- Load foreign-function-interface handle
local ffi = require("ffi")
-- Shortcut FFI C namespace
local C = ffi.C
-- Load Windows kernel32 DLL
local kernel32 = ffi.load("kernel32")
-- Add C definitions to FFI for usage descriptions of components
ffi.cdef([[
// Redefinitions for WinAPI conventions
typedef void VOID;
@Youka
Youka / main.cpp
Created May 18, 2015 18:47
MinGW wifstream
#include <ext/stdio_filebuf.h> // fstream (all sorts of IO stuff) + stdio_filebuf (=streambuf)
#include <fcntl.h> // _O_RDONLY
#include <iostream> // cout
int main(){
__gnu_cxx::stdio_filebuf<char> wfile_buf(_wopen(L"D:\\...\\の.txt", _O_RDONLY), std::ios_base::in);
std::istream wfile_stream(&wfile_buf);
wfile_stream.seekg(0, std::ios_base::end);
std::cout << wfile_stream.tellg();
return 0;
@Youka
Youka / java_env.bat
Created June 13, 2015 17:02
Small java environment setup in console
@echo off
rem Set console appearance
title Java environment (%cd%)
color 0C
rem Set console environment
if "%~1"=="" (
set JAVA_BIN=C:\Program Files\Java\jdk1.8.0_45\bin
) else (
set JAVA_BIN=%~1
)
#pragma once
#ifdef _MSC_VER
#include <intrin.h>
#define bit_SSE2 (1 << 26)
#define bit_SSE3 (1 << 0)
#define bit_AVX (1 << 28)
#else
#include <cpuid.h>
#endif // _MSC_VER
@Youka
Youka / main.c
Last active August 29, 2015 14:25
Performance test of vector division by unsigned short integers - x86 vs. SSE
#include <emmintrin.h> // SSE2
static unsigned short x[8] = {0, 55, 2, 62003, 786, 5555, 123, 32111}; // Dividend
__attribute__((noinline)) static void test_div_x86(unsigned i){
for(; i; --i)
x[0] /= i,
x[1] /= i,
x[2] /= i,
x[3] /= i,