Skip to content

Instantly share code, notes, and snippets.

View merryhime's full-sized avatar

merry merryhime

  • UK
  • 02:03 (UTC +01:00)
View GitHub Profile
@merryhime
merryhime / text.md
Last active April 11, 2025 01:16
Playing with segment registers fs and gs on x64

GSBASE and FSBASE

When you're running out of registers while writing a JIT, you might resort to more unconventional methods for memory access. You might choose to resort to segment registers if you need a fixed register for memory offsets.

Instructions such as:

lea    rax,gs:[rcx+rdx*8]
mov    rax,gs:[rcx+rdx*8]

would then be available for your use.

// Note that our hearing is logarithmic.
// 0 dB = 1.0
// For the below equations, ratio refers to the amplitude ratio.
// 0 dB = same power = same amplitude = same psycho-acoustic volume
// -3 dB = half power
// -6 dB = half amplitude
// -10 dB = half psycho-acoustic volume = 32% amplitude
double dB_to_ratio(double dB) {
template <typename T>
constexpr T pext(T x, T mask) {
T ret = 0;
for (T bb = 1; mask != 0; bb += bb) {
if (x & mask & -mask) {
ret |= bb;
}
mask &= (mask - 1);
}
return ret;
#include <array>
#include <cstddef>
#include <type_traits>
template <typename T, std::size_t N, std::size_t... Ns>
struct multi_array
{
using type = std::array<typename multi_array<T, Ns...>::type, N>;
};
#include <array>
#include <functional>
template <typename T, size_t N, size_t M, size_t... iota_N, size_t... iota_M>
auto append_impl(
std::index_sequence<iota_N...>,
std::index_sequence<iota_M...>,
const std::array<T, N>& a,
const std::array<T, M>& b
) -> std::array<T, N+M> {
Was writing a tuple class for fun.
template <typename... Ts>
class tuple {
template <typename T>
T& get();
template <typename T>
T const& get() const;
};
BbAa contains BBBBbbbbAAAAaaaa
DdCc contains DDDDddddCCCCcccc
we want: DDDDCCCCBBBBAAAA
we want: ddddccccbbbbaaaa
const auto process_pair = [](const __m256i BbAa, const __m256i DdCc) {
const __m256i DbCa = _mm256_blend_epi32(BbAa, DdCc, 0b11001100);
const __m256i dBcA = _mm256_blend_epi32(BbAa, DdCc, 0b00110011);
const __m256i DCba = _mm256_permute4x64_epi64(DbCa, 0b11011000);
// As with usual x64 ABI, rdi contains pointer to string
strlen:
xor eax, eax
test dil, 7
je .skip_align
// rax contains our return value, as per usual x64 ABI
xor eax, eax
// Line up to 8-byte alignment
.align_loop:
#include <type_traits>
/// Internal implementation of this header.
namespace mp_impl {
template<class... Ls>
struct mp_append_impl;
template<> struct mp_append_impl<> {
using type = mp_list<>;

Merry's very quick guide to elliptic curve crypto

An elliptic curve is made up of the following:

  1. A field, F_p.
  • What this means is all arithmetic on scalars is modulo p.
  • Modern ECC have p as some large prime.
  • For curve25519, p = 2^255 - 19, a prime.
  1. An equation and it's parameters: