Skip to content

Instantly share code, notes, and snippets.

@kongtomorrow
kongtomorrow / gist:57da9fd8b0fa72cd33cc
Last active August 29, 2015 14:10
playing with autovectorization and __CFBytesInASCII
Compiled with:
~/bin/clang/bin/clang -O3 -Rpass=loop-vectorize -Rpass-missed=loop-vectorize -Rpass-analysis=loop-vectorize -isysroot /Volumes/Data/Users/ken/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -framework Foundation /tmp/testcomp/testcomp/main.m
Interesting part of diagnostic output:
/tmp/testcomp/testcomp/main.m:57:13: remark: vectorized loop (vectorization factor: 16, unrolling interleave factor: 1) [-Rpass=loop-vectorize]
testBuffer[i] = (bytes[i] >= 128);
^
output:
2014-12-03 11:33:33.070 a.out[48363:2396624] shortLen: 6 longLen:2493109
@kongtomorrow
kongtomorrow / gist:9a8530528cac708a7b48
Created December 3, 2014 01:06
Swift options for use with -Xllvm
% swiftc -Xllvm -help-hidden /tmp/foo.swift
USAGE: swift (LLVM option parsing) [options]
OPTIONS:
-a9-754319-workaround - Enable workarounds for A9 HW bugs #754319
-a9-754320-workaround - Enable workarounds for A9 HW bugs #754320
-aarch64-use-tbi - Assume that top byte of an address is ignored
-agg-antidep-debugdiv=<int> - Debug control for aggressive anti-dep breaker
-agg-antidep-debugmod=<int> - Debug control for aggressive anti-dep breaker
-aggressive-ext-opt - Aggressive extension optimization
@kongtomorrow
kongtomorrow / gist:80ff47743d2c63355c42
Last active August 29, 2015 14:09
Swift + Unicode evilness :-D
let é = "precomposed character!"
let é = "decomposed characters!"
println(é) // prints "precomposed character!"
println(é) // prints "decomposed characters!"
@kongtomorrow
kongtomorrow / gist:9d4eb16b02bc8993a162
Created October 22, 2014 01:03
processing a func with arbitrary parameters
func logWrap<Domain,Range>(f : Domain->Range) -> Domain->Range {
return { (args:Domain) -> Range in
println("called me!")
return f(args)
}
}
func foo(a:Int, b:Int)->Int {
return a + b
}
@interface UIView (LayoutConvenience)
- (NSLayoutConstraint *)constraintAligningAttribute:(NSLayoutAttribute)attr withView:(UIView *)otherView;
@end
@implementation UIView (LayoutConvenience)
- (NSLayoutConstraint *)constraintAligningAttribute:(NSLayoutAttribute)attr withView:(UIView *)otherView {
return [NSLayoutConstraint constraintWithItem:self attribute:attr relatedBy:NSLayoutRelationEqual toItem:otherView attribute:attr multiplier:1.0 constant:0.0];
}
@end
2014-09-21 11:21:04.807 ALtest[42953:7217373] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
/// As it says, we need to read the constraints one by one and see if there's one that says something we don't want.
(
"<NSLayoutConstraint:0x7f9e39f344f0 H:[UIButton:0x7f9e39f339d0'1'(20)]>",
/// first button is width 20 – ***I bet you didn't want this and it needs to be deleted***
@kongtomorrow
kongtomorrow / gist:d36550fe7da2e84c8559
Created August 18, 2014 19:34
Swift seed 6: lexical scope nope?
func foo() {
func bar(x:Int)->String {
return "hello"
}
[1,2,3].map({bar($0)}) // "Cannot reference a local function from another local function"
}
func benchmark<T>(label:String, body:()->T) {
var info = mach_timebase_info_data_t(numer: 0, denom: 0)
mach_timebase_info(&info)
let tick = mach_absolute_time()
let res = body()
let tock = mach_absolute_time()
let delta = tock - tick
let nanos = delta * UInt64(info.numer) / UInt64(info.denom)
@kongtomorrow
kongtomorrow / gist:8d1ac5161021451c794c
Last active April 21, 2023 17:14
you're freaking me out, Swift
func foo(f:()->(Int?)) {
println(f())
}
let bar : ()->Int = { 3 }
// foo shouldn't be able to take bar, should it?
foo(bar) // prints Optional(3)
// you're freaking me out, Swift. Did it allocate a wrapping closure?
operator infix !|| { associativity right precedence 100 } // associativity/precedence copied from ternary ? :
@infix func !||<T>(lhs:T?, rhs:@auto_closure ()->T) -> T {
switch lhs {
case nil: return rhs()
case _: return lhs!
}
}