Skip to content

Commit 733885d

Browse files
committed
curl
1 parent 8100fcb commit 733885d

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

k+/l.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ enum fn {
22
#ifdef BLAS
33
nrm2, asum, imax, rot, mv,
44
#endif
5+
#ifdef CURL
6+
curl,
7+
#endif
58
};
69
void reg(int64_t id, int64_t name, int32_t arity){
710
int64_t l=ti(14,(uint32_t)l2(id, Ku(name)));
@@ -14,7 +17,10 @@ void libs(void){ //encode strings with: https://play.golang.org/p/4ethx6OEVCR
1417
reg(asum,1836413793ull,1);
1518
reg(imax,2019650921ull,1);
1619
reg(rot, 7630706ull,4);
17-
reg(mv, 30317ull, 7);
20+
reg(mv, 30317ull,7);
21+
#endif
22+
#ifdef CURL
23+
reg(curl,1819440483ull,1);
1824
#endif
1925
}
2026

@@ -41,6 +47,16 @@ void cblas_dgemv(int,int,int,int,double,double*,int,double*,int,double,double*,i
4147
#define trans(x) (111+x)
4248
#endif
4349

50+
#ifdef CURL
51+
void*curl_easy_init(void);
52+
int curl_easy_perform(void*);
53+
void curl_easy_setopt(void*,int,void*);
54+
void curl_easy_cleanup(void*);
55+
static size_t curlback(void *c,size_t n,size_t m,uint64_t*r){
56+
uint64_t x=*r;int32_t xn=nn(x);n*=m;x=uspc(x,Ct,n);*r=x;
57+
memcpy(M_+(int32_t)x+xn,c,n);return n;}
58+
#endif
59+
4460
double*FK(uint64_t x){return (double*)(M_+(int32_t)x); }
4561
double f0(uint64_t x){return F64((int32_t)I64((int32_t)x ));}
4662
double f1(uint64_t x){return F64((int32_t)I64((int32_t)x+8));}
@@ -49,6 +65,7 @@ int32_t i2(uint64_t x){return I32((int32_t)I64((int32_t)x+16));}
4965
int32_t i3(uint64_t x){return I32((int32_t)I64((int32_t)x+24));}
5066
void dim(uint64_t A,int32_t rA,int32_t*m,int32_t*n){rA<0?(*m=-rA,*n=-rA/nn(A)):(*m=rA,*n=rA/nn(A));}
5167

68+
5269
//x:function-id(not refcounted), y:list of args(length arity)
5370
int64_t cnative(int64_t x, int64_t y){
5471
int32_t m,n,i;
@@ -77,7 +94,20 @@ int64_t cnative(int64_t x, int64_t y){
7794
printf("todo mv complex\n");
7895
}
7996
};
97+
#endif
98+
#ifdef CURL
99+
case curl:{x=Fst(y);if(tp(x)!=Ct)trap();x=cat1(x,Kc(0));
100+
void*c=curl_easy_init();
101+
curl_easy_setopt(c,10002,M_+(int32_t)x);
102+
uint64_t r=mk(Ct,0);
103+
curl_easy_setopt(c,20011,curlback);
104+
curl_easy_setopt(c,10001,(void*)&r);
105+
curl_easy_setopt(c,10018,"libcurl-agent/1.0");
106+
if(curl_easy_perform(c))trap();
107+
curl_easy_cleanup(c);
108+
dx(x);return r;}
80109
#endif
81110
default: return y;
82111
}}
83112

113+

k+/readme

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# k+ extends k.c with some c-libraries using the native function type.
2-
# blas lapack fftw lp_solve ode specfun ..
2+
# blas lapack fftw lp_solve ode specfun curl sqlite..
33
#
44
# m.awk also replaces the builtin math functions with libm.
55
#
@@ -17,6 +17,9 @@
1717
# imax[x] FZ index of l1 max element
1818
# rot[c;s;x;y] c:f s:f x:FZ y:FZ plane rotation
1919
# mv[a;A;rA;op;b;x;y] c:fz A:FZ b:fz y:FZ a*A*x+b*y
20+
#
21+
# CURL
22+
# curl"https://ktye.github.io/k.png"
2023

2124
set -e
2225
set -x
@@ -33,6 +36,6 @@ cat l.c>>k.c
3336

3437
C=cc
3538
O="-O0 -march=native"
36-
D="-D BLAS "
37-
L="-lopenblas"
39+
D="-D BLAS -DCURL"
40+
L="-lopenblas -lcurl"
3841
$C $O $D -ok+ k.c $L -lm

0 commit comments

Comments
 (0)