Skip to content

Instantly share code, notes, and snippets.

View erikcorry's full-sized avatar
🏠
Working from home

Erik Corry erikcorry

🏠
Working from home
View GitHub Profile
" Tab completion in vim using the tab key.
" Turns into two spaces if there is no alphanumerics on the left of the cursor.
function! CleverTab()
if strpart( getline('.'), col('.')-2, 1) =~ '^\s*$'
return " "
else
return "\<C-P>"
endfunction
inoremap <Tab> <C-R>=CleverTab()<CR>
@erikcorry
erikcorry / test.v
Created January 24, 2018 06:42
Chained FSM
module stage(clock, reset, in, out);
input wire clock;
input wire reset;
input wire in;
output reg out;
always @(posedge clock) begin
if (reset) begin
out <= 1;
end else begin
out <= !in;
" Looks at the character to the left, and does a shell-style autocomplete
" (ctrl-P) if we are in the middle of a word. If there is white-space
" or beginning-of-line, it does two spaces to indent.
function! CleverTab()
if strpart( getline('.'), col('.')-2, 1) =~ '^\s*$'
return " "
else
return "\<C-P>"
endfunction
inoremap <Tab> <C-R>=CleverTab()<CR>
@erikcorry
erikcorry / mbedtls-errors.txt
Created October 23, 2020 14:35
Mbed TLS error codes
High level error codes
0x1080 PEM - No PEM header or footer found
0x1100 PEM - PEM string is not as expected
0x1180 PEM - Failed to allocate memory
0x1200 PEM - RSA IV is not in hex-format
0x1280 PEM - Unsupported key encryption algorithm
0x1300 PEM - Private key password can't be empty
0x1380 PEM - Given private key password does not allow for correct decryption
0x1400 PEM - Unavailable feature, e.g. hashing/encryption combination

Signed overflow detection on anti-80

Motivation

Rust would like to enable overflow checks (and does in debug mode) but many CPUs don't have great support. JS and many other languages need overflow checking.

For adding or subtracting a constant the overflow check is already fairly simple, but it's a bit longwinded for adding

@erikcorry
erikcorry / aliasing-slices.go
Created March 24, 2021 09:43
Golang slices sometimes alias their underlying array, and sometimes don't.
// Prints false, false, false, true, false, true, true, false, true.
package main
func main() {
for i := 0; i < 10; i++ {
println(doesItAlias(i))
}
}
@erikcorry
erikcorry / pass-by.go
Created March 30, 2021 15:25
Go passes maps and some interfaces by reference, most other things by value.
package main
func main() {
myMap := map[string]bool{}
populateMap(myMap)
println(len(myMap)) // Prints 1 because maps are passed by reference.
mySlice := []int{}
populateSlice(mySlice)
println(len(mySlice)) // Prints 0 because slices are passed by value.
package main
// Demonstrates how taking the address of an embedded struct
// leaks the embedding struct's memory.
type Inner struct {
x int
}
type Outer struct {
package main
// Existing classes we can't extend because they are
// in a different package.
type Foo struct {
Name string
}
type Bar struct {
Name string
Ainos-MBP:~ erik$ ./toit/build/release64/bin/toitvm fly.toit
2 iterations, 7.0us per operation
4 iterations, 4.0us per operation
8 iterations, 3.375us per operation
16 iterations, 2.8125us per operation
32 iterations, 2.6875us per operation
64 iterations, 2.25us per operation
128 iterations, 2.046875us per operation
256 iterations, 1.91015625us per operation
512 iterations, 2.25us per operation