Skip to content

Instantly share code, notes, and snippets.

View jimblandy's full-sized avatar

Jim Blandy jimblandy

View GitHub Profile
@jimblandy
jimblandy / gen.rs
Created December 17, 2016 17:57
Simulating generators in Rust
//! Generators in Rust, simulated at great expense using threads
//!
//! Here's a generator which yields the numbers from one to three:
//!
//! let gen = generate(|(), out| {
//! for i in 1..4 {
//! out.yield_(i);
//! }
//! });
//!
@jimblandy
jimblandy / barriers.h
Created September 21, 2016 01:23
Header file from Minor Scheme for memory barriers
/* barriers.h --- interface to memory and code translation barriers */
#ifndef MN__GC_BARRIERS_H
#define MN__GC_BARRIERS_H
/* The following two functions provide the memory-coherence behavior
of mutexes, but without any blocking or communication.
On many modern multi-threaded systems, one thread may see writes to
@jimblandy
jimblandy / gist:7028a3acce8453944ec285f667293b4f
Created July 27, 2016 05:29
fn t(n: usize) -> usize { (0..n+1).fold(0, |s, i| s+i*i*i) }
5828: 49 83 fa ff cmp $0xffffffffffffffff,%r10
582c: 74 5d je 588b <_ZN8triangle4main17h1e5682daf508becbE+0x11b>
582e: 49 8d 42 ff lea -0x1(%r10),%rax
5832: 49 f7 e2 mul %r10
5835: 49 89 c0 mov %rax,%r8
5838: 49 89 d1 mov %rdx,%r9
583b: 49 8d 72 fe lea -0x2(%r10),%rsi
583f: 48 f7 e6 mul %rsi
5842: 48 89 c7 mov %rax,%rdi
5845: 48 89 d3 mov %rdx,%rbx
@jimblandy
jimblandy / gist:f3a60f2ea1e5cc20ca4fa8965e50d186
Created July 27, 2016 05:20
fn t(n: usize) -> usize { (0..n+1).fold(0, |s, i| s+i*i) }
5830: 48 8d 47 ff lea -0x1(%rdi),%rax
5834: 48 f7 e7 mul %rdi
5837: 48 89 c1 mov %rax,%rcx
583a: 48 89 d6 mov %rdx,%rsi
583d: 48 8d 5f fe lea -0x2(%rdi),%rbx
5841: 48 f7 e3 mul %rbx
5844: 0f af de imul %esi,%ebx
5847: 48 0f a4 ce 3f shld $0x3f,%rcx,%rsi
584c: 48 8d 0c 76 lea (%rsi,%rsi,2),%rcx
5850: 01 da add %ebx,%edx
@jimblandy
jimblandy / gist:a94d117a29d4e5031c5ae7084758f932
Created July 27, 2016 05:18
Machine code generated for triangle function
5826: xor %ecx,%ecx
5828: cmp $0xffffffffffffffff,%rsi
582c: je 5840
582e: lea -0x1(%rsi),%rax
5832: mul %rsi
5835: mov %rdx,%rcx
5838: shld $0x3f,%rax,%rcx
583d: add %rsi,%rcx
5840: ... use %rcx
(defun jimb-rust-clean-error ()
"Clean up an error message from rustc to fit nicely in the book.
Run this command with point on the first line of an error message
copied from rustc and pasted into a Markdown file; we'll clean it
up and indent it as a code block."
(interactive)
(save-excursion
(forward-line 0)
(let ((top (point)))
(let ((line-was-interesting t)
fn partition<T: Ord>(a: &mut[T]) -> usize {
assert!(a.len() >= 2);
let mut pivot = 0;
let (mut i, mut j) = (0, a.len());
loop {
loop {
j -= 1;
if a[j] <= a[pivot] { break; }
mod implicit_deref {
use std::ops::Deref;
struct T(i32);
struct S<'a>(&'a T);
impl<'a> Deref for S<'a> {
type Target = T;
fn deref(&self) -> &T { &self.0; }
}
use std::str::FromStr;
use std::fmt::Debug;
use std::io::stdin;
use std::io::stdout;
use std::io::Write;
trait Game : Clone {
type Move : Copy + FromStr;
fn start() -> Self;
fn moves(&self) -> Vec<Self::Move>;
@ changeset: 252017 47888f3efe7c Thu Mar 19 15:39:53 2015 -0700
| bookmarks: mozalloc2
| summary: wip
| phase: draft
|
o changeset: 252016 9c583c9b4d2d Thu Mar 19 01:03:05 2015 -0700
| bookmarks: mozalloc-no-xpcom-config
| summary: Bug 1145051: Don't include xpcom-config.h from mozalloc.h; it's unused.
| phase: draft
|