Skip to content

Instantly share code, notes, and snippets.

View derrickturk's full-sized avatar
💭
(Come in under the shadow of this red rock)

Derrick Turk derrickturk

💭
(Come in under the shadow of this red rock)
View GitHub Profile
#include <iostream>
#include <ios>
#include <iomanip>
#include <cstring>
#include <cstdlib>
int main(int argc, char **argv)
{
if (argc != 2) {
std::cerr << "Usage: " << (argc ? argv[0] : "double") << " <number>\n";
Option Explicit
Public Function arrayAddress(ByRef arr() As Double) As Long
arrayAddress = VarPtr(arr(0))
End Function
Public Sub investigateArrays()
Dim d(0 To 9) As Double
Dim x() As Double
@derrickturk
derrickturk / Makefile
Created March 6, 2014 05:05
Callback functions in VBA: VBA calls DLL calls VBA.
CXX=g++
CXXOPTS=-std=c++11 -pedantic -Wall -Wextra -Werror -static-libgcc -static-libstdc++
OPTOPTS=-O3 -fno-exceptions -fno-rtti -ffast-math -mfpmath=387 -msse3 -mtune=core2 -funroll-loops -s
DLLOPTS=-shared
LINKOPTS=-Wl,--add-stdcall-alias
LOG=
#LOG=-DLOGGING="log.txt"
callback.dll: callback.o
$(CXX) $(CXXOPTS) $(LOG) $(OPTOPTS) $(DLLOPTS) -o callback.dll callback.o $(LINKOPTS)
@derrickturk
derrickturk / noexcept_auto_buf.cpp
Created March 7, 2014 14:41
Sometimes (*cough* linking GNU new (nothrow) still brings in a bunch of exception-handling crap *cough*) you just need an easy malloc'd buffer with automatic lifetime management.
template<class T>
using auto_buf = std::unique_ptr<T[], void(&)(void*) noexcept>;
template<class T>
auto_buf<T> make_auto_buf(std::size_t n) noexcept
{
return auto_buf<T>(static_cast<T*>(std::malloc(n * sizeof(T))), std::free);
}
@derrickturk
derrickturk / pow.cpp
Created March 11, 2014 20:42
Good lord. Can we please have recursive access to function template names in -> decltype(...) and function template partial specialization for C++14? I'm tired of writing stupid traits/impl class templates.
#include <iostream>
#include "tpow.hpp"
int main()
{
static_assert(noexcept(tpow<5>(5)), "tpow<int, N> not noexcept");
std::cout << "5^5 = " << tpow<5>(5) << '\n';
static_assert(noexcept(tpow<4>(2.3)), "tpow<double, N> not noexcept");
std::cout << "2.3^4 = " << tpow<4>(2.3) << '\n';
#hello world
Fun with **R**!
````{r}
rnorm(100)
````
set.seed(555)
pop.1 <- data.frame(x=rnorm(n=25, mean=5, sd=3),
y=rnorm(n=25, mean=3, sd=2))
pop.2 <- data.frame(x=runif(n=25, min=6, max=10),
y=runif(n=25, min=3, max=8))
pop.3 <- data.frame(x=rnorm(n=25, mean=4, sd=1.2),
y=rnorm(n=25, mean=7, sd=2))
pop <- rbind(pop.1, pop.2, pop.3)[sample(nrow(pop.1) * 3),]
@derrickturk
derrickturk / gist:9697487
Created March 21, 2014 22:06
Death by misalignment...
Public service announcement: use extreme caution when compiling under g++ with -msseX and returning a std::pair<double, double>, or the structural equivalent, by value. It will optimize to a movapd, which works wonderfully as long as you're returning into a 16-bit aligned address. But you'll mysteriously explode in a shower of body parts if calling into, say, a DLL from external code (say, Excel VBA) with a less-strictly aligned stack.
No, I didn't just spend 5 hours tracking that one down...
@derrickturk
derrickturk / to_bytes.cpp
Created March 26, 2014 20:18
Dump binary representation of IEEE double.
#include <memory>
#include <cstddef>
#include <algorithm>
#include <iostream>
#include <iomanip>
template<class T>
inline std::unique_ptr<unsigned char[]> to_bytes(const T& item)
{
@derrickturk
derrickturk / Makefile
Last active August 29, 2015 13:57
Multi-threading, callbacks into VBA, DoEvents madness, and the mysterious vba_lock!
CXX=g++
CXXOPTS=-std=c++11 -pedantic -Wall -Wextra -Werror -static -static-libgcc -static-libstdc++
OPTOPTS=-O3 -fno-rtti -ffast-math -mfpmath=387 -msse3 -mtune=core2 -funroll-loops -s
DLLOPTS=-shared -DDLL
LINKOPTS=-Wl,--add-stdcall-alias
LOG=
#LOG=-DLOGGING="log.txt"
thread_engine.dll: thread_engine.cpp
$(CXX) $(CXXOPTS) $(LOG) $(OPTOPTS) $(DLLOPTS) -o thread_engine.dll thread_engine.cpp $(LINKOPTS)