|
1 | | -// -nostdlib -ffreestanding |
2 | | - |
3 | | -//gcc -nostdlib -static |
4 | | - |
5 | | -#include<stdint.h> |
6 | | -asm(".global _start;_start:;xorl %ebp,%ebp;movq 0(%rsp),%rdi;lea 8(%rsp),%rsi;call main;"); |
7 | | -asm("Exit:;movq %rax,%rdi;movq $60,%rax;syscall;\n"); |
8 | | -asm("F64sqrt:sqrtsd %xmm0,%xmm0;ret;\n"); |
9 | | - |
10 | | -void Exit(int x); |
11 | | -double F64sqrt(double); |
12 | | - |
13 | | -/* |
14 | | -asm("write:;movq $1,%rax;syscall;ret;\n"); |
15 | | -asm("F64abs:xor %eax,%eax;dec %rax;shr %rax;movq %rax,%xmm1;andpd %xmm1,%xmm0;ret;"); |
16 | | -asm("F64min:minsd %xmm0,%xmm0;ret;\n"); |
17 | | -asm("F64max:maxsd %xmm0,%xmm0;ret;\n"); |
18 | | -
|
19 | | -double F64abs(double); |
20 | | -double F64min(double); |
21 | | -double F64max(double); |
22 | | -double F64copysign(double x,double y){union{double f;uint64_t i;}ux={x},uy={y};ux.i&=-1ull/2;ux.i|=uy.i&1ull<<63;return ux.f;} |
23 | | -double F64floor(double x){int i=(int)x;return(double)i;} |
24 | | -
|
25 | | -int write(int fd, const void *buf, unsigned count); |
26 | | -int ns(char*c){char *p;for(p=c;*p;++p);return p-c;} |
27 | | -*/ |
28 | | - |
29 | | -void main(int args,char*argv[]){ |
30 | | - /* |
31 | | - for(int i=0;i<args;++i){ |
32 | | - int n=ns(argv[i]); |
33 | | - write(1,argv[i],n); |
34 | | - write(1,"\n",1); |
35 | | - } |
36 | | - */ |
37 | | - Exit((int64_t)F64sqrt((double)args)); |
38 | | -} |
| 1 | + |
| 2 | +//gcc -Os -s -nostdlib -static -Wl,--nmagic -Wl,--build-id=none -fno-asynchronous-unwind-tables k.c |
| 3 | +//(objcopy --remove-section .comment a.out b.out) |
| 4 | + |
| 5 | +typedef uint unsigned int; |
| 6 | +typedef ulong unsigned long; |
| 7 | + |
| 8 | +asm(".globl _start;_start:mov %rsp,%rdi;call cmain"); |
| 9 | + |
| 10 | + |
| 11 | + static double F64copysign(double x,double y){union{double f;uint64_t i;}ux={x},uy={y};ux.i&=-1ull/2;ux.i|=uy.i&1ull<<63;return ux.f;} |
| 12 | + static double F64floor(double x){int i=(int)x;return(double)i;} |
| 13 | +__attribute((naked))static double F64abs(double x){ asm("xor %eax,%eax;dec %rax;shr %rax;movq %rax,%xmm1;andpd %xmm1,%xmm0;ret;");} |
| 14 | +__attribute((naked))static double F64sqrt(double x){ asm("sqrtsd %xmm0,%xmm0;ret");} |
| 15 | +__attribute((naked))static double F64min(double x,double y){asm("minsd %xmm0,%xmm0;ret;\n");} |
| 16 | +__attribute((naked))static double F64max(double x,double y){asm("maxsd %xmm0,%xmm0;ret;\n");} |
| 17 | + |
| 18 | +__attribute((naked))long Write(long fd,char*b,int n){asm("mov $1,%rax;syscall;ret");} |
| 19 | +__attribute((naked))void Exit(int x){asm("mov $60,%rax;syscall");} |
| 20 | + |
| 21 | + |
| 22 | +static int Memorysize(void){ .. } |
| 23 | +static int Memorygrow(int32_t delta){ .. } |
| 24 | + |
| 25 | +#define I8(x) (int8_t)(M_[x]) //? |
| 26 | +#define I32(x) (I_[(x)>>2]) |
| 27 | +#define I64(x) (long)(U_[(x)>>3]) |
| 28 | +#define F64(x) ((double*)U_)[(x)>>3] |
| 29 | +static void SetI8( int32_t x,int32_t y){M_[x]=(char)(y);} |
| 30 | +static void SetI32(int32_t x,int32_t y){I_[(x)>>2]=(y);} |
| 31 | +static void SetI64(int32_t x,int64_t y){U_[(x)>>3]=(uint64_t)(y);} |
| 32 | +static void SetF64(int32_t x,double y){((double*)U_)[(x)>>3]=(y);} |
| 33 | + |
| 34 | +//int ns(char*c){char *p;for(p=c;*p;++p);return p-c;} //strlen? |
| 35 | + |
| 36 | +#define I32B(x) (int)(x) |
| 37 | +static void Memorycopy(int dst,int src,int n){memcpy(M_ +dst, M_+src,(ulong)n); } |
| 38 | +static void Memoryfill(int p,int v,int n){memset(M_+p,(int)v,(ulong)n);} |
| 39 | +static int I32clz(int32_t x){ return(int)__builtin_clz((uint)x);} |
| 40 | +static double F64reinterpret_i64(ulong x){union{ulong i;double f;}u;u.i=x;return u.f;} |
| 41 | +static ulong I64reinterpret_f64(double x){union{ulong i;double f;}u;u.f=x;return u.i;} |
| 42 | + |
| 43 | +char**argv; |
| 44 | +static int Args(void){return((int*)argv)[0]}; |
| 45 | +static int Arg(int i,int r){if(!r)return ns(argv[1+i]);memcpy(M_+r,argv[1+i],strlen(argv[i]));return 0;} |
| 46 | +static int Read( int f,int nf,int d){...} |
| 47 | +static int Write(int f,int nf,int s,int n){if(!nf){write(1,M_+s,n);return 0;} //todo file} |
| 48 | +static int ReadIn(int dst,int n){return(int)read(1,_M+dst,n);} |
| 49 | +static long Native(long x,long y){return(x+y)*0;} |
| 50 | +static void panic(int x){Exit(1);} |
| 51 | + |
| 52 | +void cmain(char**a){argv=a;main_();} |
0 commit comments