Skip to content

Instantly share code, notes, and snippets.

View airglow923's full-sized avatar
✒️
Studying

Hyundeok Park airglow923

✒️
Studying
View GitHub Profile
@airglow923
airglow923 / gen-js-integrity.sh
Created May 7, 2021 10:14
A shell script that generates an integrity for a JavaScript file
#!/bin/sh
usage() {
# additional error messages
if [ "$#" -ne 0 ]; then
printf "$@\n" >&2;
fi
printf "Usage: $0 ALGORITHM JS_FILE\n" >&2;
}
@airglow923
airglow923 / deno-to-latex-uri.js
Last active June 10, 2021 09:55
Convert LaTeX code into URI so as to embed it in websites
const latex = Deno.args[0];
const latexRenderer = 'https://latex.codecogs.com/svg.latex?';
if (latex === undefined || typeof latex !== 'string') {
const errMsg = 'Invalid argument';
writeStdStream(errMsg, Deno.stderr);
Deno.exit(1);
}
async function writeStdStream(str, stream = Deno.stdout) {
@airglow923
airglow923 / instructions.md
Created July 22, 2021 05:46
AMD64 instructions

I don't have a reliable tool to measure the time taken to execute one or more instructions.

This is just to keep track of the assembly output for different C expressions.

The assembly is in AT&T syntax, but the comments will be written in Intel syntax as GitHub Markdown does not support AT&T syntax.

Arithmetic

Multiplication

@airglow923
airglow923 / binary.py
Last active October 13, 2021 03:26
Binary-related code written in Python
# Return a binary representation of an integer with space every 4 bits
def get_binary(x):
binary = bin(x)[2:]
for i in range(len(binary) - 4, 0, -4):
binary = binary[:i] + ' ' + binary[i:]
return binary
# Convert binary floating-point number into decimal
@airglow923
airglow923 / parity_flag.c
Last active October 17, 2021 07:49
Different ways to get the parity flag in C
/*
* As shown below, this code is not meant for getting the parity of a number;
* rather, it checks the parity flag. The reason is that the parity flag only
* represents whether the least significant byte has odd or even number of set
* bits. So, it should not be used to check the parity of a number in practice
* unless the number is one byte long.
*
* Having said that, an implementation-defined function by Clang and GCC for
* parity computation, __builtin_parity(x), returns 0 if x has even set bits and
* 1 if odd, which is the opposite of the parity flag. That is the reason why
@airglow923
airglow923 / floating-point-binary-1.cpp
Last active October 30, 2021 19:23
Floating-point number to binary in C++
#include <concepts> // integral
#include <cstddef> // size_t
#include <iostream> // cout
#include <ranges> // iota, reverse
namespace {
constexpr auto
is_bit_set(long long i, std::size_t n) -> bool {
return (i & (1LL << n)) != 0;
@airglow923
airglow923 / custom-unordered-set.cpp
Created October 30, 2021 19:13
Custom hash for unordered_set
#include <concepts> // integral
#include <unordered_set>
#include <utility> // pair
template <std::integral First, std::integral Second>
struct std::hash<std::pair<First, Second>> {
using argument_type = std::pair<First, Second>;
using result_type = std::size_t;
auto
@airglow923
airglow923 / print-range.cpp
Created October 30, 2021 19:20
Print range in C++
#include <iostream> // cout
#include <ranges> // range, subrange, cbegin, cend
#include <vector>
namespace {
auto print_range(const std::ranges::range auto &range, bool newline = true)
-> void;
namespace detail {
@airglow923
airglow923 / bubble-sort.cpp
Created November 10, 2021 16:31
Bubble sort
#include <iterator> // bidirectional_iterator, iterator_traits
#include <utility> // move, swap
template <std::bidirectional_iterator BidirectionalIterator, typename Compare,
typename ValueType =
typename std::iterator_traits<BidirectionalIterator>::value_type>
constexpr auto bubble_sort1(BidirectionalIterator begin,
BidirectionalIterator end, Compare compare)
-> void {
if (begin == end) {
@airglow923
airglow923 / sfinae-concept.cpp
Last active November 23, 2021 02:34
SFINAE and concept comparison
#include <concepts>
#include <iostream>
#include <type_traits>
struct BothAssignable {
constexpr auto operator=(const BothAssignable&) noexcept -> BothAssignable& = default;
constexpr auto operator=(BothAssignable&&) noexcept -> BothAssignable& = default;
};
struct CopyAssignable {