Skip to content

Instantly share code, notes, and snippets.

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

Chen Zeren owlfox

🏠
Working from home
View GitHub Profile
@owlfox
owlfox / gist:f01fcd8e057d68822835
Created July 7, 2015 18:06
optimisation problem can be solved by volatile(中文)
x=0;
//thread 1
{
lock();
x++;
unlock();
}
//thread 2
{
lock();
@owlfox
owlfox / DETECT.c
Created July 30, 2015 09:31
summer 2015 quiz
#if LONG_MAX == 2147483647L
#define DETECT(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
#else
#if LONG_MAX == 9223372036854775807L
#define DETECT(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
#else
#error long int is not a 32bit or 64bit type.
#endif
#endif
@owlfox
owlfox / quiz1.c
Created July 30, 2015 12:51
summer2015 quiz1
#include <stdio.h>
int main(void)
{
int i, result;
for(i=2147483647;i>-2147483648;i--)
{
result = i&(i-1);
if(result==0)
printf("%d: %d\n",i,result);
}
@owlfox
owlfox / quiz2.c
Created July 30, 2015 13:19
summer2015 quiz2
#include <stdio.h>
//#define MAX 2147483647
//#define MIN -MAX+1
#define MAX 15
#define MIN -16
#define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d"
#define BYTETOBINARY(byte) \
(byte & 0x80 ? 1 : 0), \
(byte & 0x40 ? 1 : 0), \
(byte & 0x20 ? 1 : 0), \
@owlfox
owlfox / quiz4.c
Last active August 29, 2015 14:26
summer2015 quiz4, 題目在comment 那邊
int square(volatile int *ptr) //make sure each call to this function the CPU move data form MEM to REG
{
int rtn = *ptr; //fetch the value pointer points to only once..
return rtn*rtn;
}
//避免原來例子兩次從記憶體搬運資料時*ptr遭thread其他修改
//或是compiler 最佳時跳過部分指令, 像是下面的例子如果沒有volatile,
// compile with O2 得到的assembly 是 rep ret
void callsquare(volatile int *ptr)
{
@owlfox
owlfox / 83_Quiz2.c
Last active August 29, 2015 14:26
8/3_Quiz2.c
#include <stdint.h>
#include <stdio.h>
#include <limits.h>
//code from Jserv
//int max(int32_t a, int32_t b) { return a * sign(a - b) + b * (1 ^ sign(a - b)); }
int incline sign(int32_t a) { return 1 ^ ((a >> 31) & 0x1); }
//end of code from Jserv
int max(int32_t a, int32_t b) {
@owlfox
owlfox / 83_Quiz1.md
Last active August 29, 2015 14:27
Weak/Weired proof of Finding a number from 2^n number...

##Binary Search. ###Question: We are going to find a number A in a sequence of numbers N_1, ..., N_2^n N_i != N_j for all i!=j, i,j >=1 <=n The only way we interact with the numbers is a guessing function F(), which takes argument of integer and returns 1 for A>the parameter, -1 for A< the parameter and 0 for correct guess.

Show that we can find A in less than n guess, call F() in less/equal to n times.

@owlfox
owlfox / clz2.c
Last active August 29, 2015 14:27
Summer2015Day3_Quiz1.2
#define u 0 /* don't care */
#include <stdint.h>
#include <stdio.h>
static uint8_t table[64] = {
32, 31, u, 16, u, 30, 3, u,
15, u, u, u, 29, 10, 2, u,
u, u, 12, 14, 21, u, 19, u,
u, 28, u, 25, u, 9, 1, u,
17, u, 4, u, u, u, 11, u,
13, 22, 20, u, 26, u, u, 18,
@owlfox
owlfox / quiz1.1.c
Created August 18, 2015 08:19
Summer2015 Day3 Quiz1.1
@prologue...
@move x to r0
@r2=n=1
cmp r0, #0
beq .L6
LSR r1, r0, LSR #16
cmp r1 #0
beq .L1;
LSR r1, r0, LSR #24
cmp r1 #0
@owlfox
owlfox / clz3.c
Last active August 29, 2015 14:27
summer2015_p2_d3_quiz1.2
#include <stdio.h>
#include <stdint.h>
int clz(uint32_t v) {
uint32_t index;
static const uint8_t pos[32] = {32, 31, 4, 30, 3, 18, 8, 29,
2, 10, 12, 17, 7, 15, 28, 24, 1, 5, 19, 9, 11, 13,
16, 25, 6, 20, 14, 26, 21, 27, 22, 23};
v |= v >> 1;//start
v |= v >> 2;
v |= v >> 4;