Skip to content

Commit 12f8edb

Browse files
committed
libm
1 parent 531ac9e commit 12f8edb

5 files changed

Lines changed: 117 additions & 18 deletions

File tree

k+/l.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
enum fn {
2+
#ifdef BLAS
3+
nrm2, asum, imax,
4+
#endif
5+
};
16
void reg(int64_t id, int64_t name, int32_t arity){
27
int64_t l=ti(14,(uint32_t)l2(id, Ku(name)));
38
SetI32((int32_t)l-12,arity);
49
dx(Asn(sc(Ku(name)),l));
510
}
611
void libs(void){ //encode strings with: https://play.golang.org/p/4ethx6OEVCR
712
#ifdef BLAS
8-
reg(0, 846033518ull,1); //nrm2
9-
reg(1,1836413793ull,1); //asum
10-
reg(2,2019650921ull,1); //imax
13+
reg(nrm2, 846033518ull,1); //id k-name arity (name is same as id-enum)
14+
reg(asum,1836413793ull,1);
15+
reg(imax,2019650921ull,1);
1116
#endif
12-
//reg(1,...);
13-
//reg(2,...);
1417
}
18+
1519
int64_t fzvec(int64_t x){
1620
int32_t xt=tp(x);
1721
if(xt<16){x=Enl(x);xt=tp(x);}
@@ -29,18 +33,18 @@ int cblas_izamax(int,double*,int);
2933

3034
#define fp(x) ((double*)(M_+(int32_t)x))
3135

32-
int64_t cnative(int64_t x, int64_t y){
36+
int64_t cnative(int64_t x, int64_t y){ //function-id, list of args(length arity)
3337
int32_t xt,xn,i;
3438
double r;
3539
switch(x){ //switch registered function id
3640
#ifdef BLAS
37-
case 0:x=fzvec(Fst(y));xn=nn(x);if(xt==Zt)xn*=2;
41+
case nrm2:x=fzvec(Fst(y));xn=nn(x);if(xt==Zt)xn*=2;
3842
r=cblas_dnrm2(xn,fp(x),1);
3943
dx(x);return Kf(r);
40-
case 1:x=fzvec(Fst(y));xn=nn(x);if(xt==Zt)xn*=2;
44+
case asum:x=fzvec(Fst(y));xn=nn(x);if(xt==Zt)xn*=2;
4145
r=cblas_dasum(xn,fp(x),1);
4246
dx(x);return Kf(r);
43-
case 2:x=fzvec(Fst(y));xn=nn(x);
47+
case imax:x=fzvec(Fst(y));xn=nn(x);
4448
i=tp(x)==Ft?cblas_idamax(xn,fp(x),1):cblas_izamax(xn,fp(x),1);
4549
dx(x);return Ki(i);
4650
#endif

k+/l.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
#define NATIVE 1
22
void libs(void);
33
int64_t cnative(int64_t,int64_t);
4+
static uint64_t se(double);
5+
void sincos(double,double*,double*);
6+
static void sin1(int32_t,int32_t,int32_t);
7+
static void cos1(int32_t,int32_t,int32_t);
8+
#define cosin_(x,r,_) sincos(x,(double*)(M_+8+r),(double*)(M_+r));

k+/m.awk

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# patch out built-in math library, replace with -lm */
2+
3+
BEGIN {x=1; }
4+
/double hypot_\(/ {x=match($0,/;/);next} #cut both declaration&function
5+
/void cosin_\(/ {x=match($0,/;/);next}
6+
/void sin1\(/ {x=match($0,/;/);next}
7+
/void cos1\(/ {x=match($0,/;/);next}
8+
/double atan2_\(/ {x=match($0,/;/);next}
9+
/double atan_\(/ {x=match($0,/;/);next}
10+
/double satan\(/ {x=match($0,/;/);next}
11+
/double xatan\(/ {x=match($0,/;/);next}
12+
/double exp_\(/ {x=match($0,/;/);next}
13+
/double expmulti\(/ {x=match($0,/;/);next}
14+
/double ldexp_\(/ {x=match($0,/;/);next}
15+
/int32_t frexp1\(/ {x=match($0,/;/);next}
16+
/double frexp2\(/ {x=match($0,/;/);next}
17+
/int64_t frexp3\(/ {x=match($0,/;/);next}
18+
/double normalize\(/ {x=match($0,/;/);next}
19+
/double log_\(/ {x=match($0,/;/);next}
20+
/double modabsfi\(/ {x=match($0,/;/);next}
21+
/double pow_\(/ {x=match($0,/;/);next}
22+
/uint64_t se\(/ {x=match($0,/;/);next}
23+
/exp_/ { sub(/exp_/ ,"exp" )}
24+
/log_/ { sub(/log_/ ,"log" )}
25+
/pow_/ { sub(/pow_/ ,"pow" )}
26+
/atan2_/{ sub(/atan2_/,"atan2")}
27+
/hypot_/{gsub(/hypot_/,"hypot")}
28+
{ if (x) print}
29+
/^}/ {x=1; }

k+/m.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//reinsert lost functions implemented with libm
2+
3+
/*
4+
static uint64_t se(double x){
5+
int32_t ei;
6+
double f;
7+
int64_t e;
8+
f=x;
9+
e=(int64_t)(0ll);
10+
if(frexp1(x)){
11+
f=frexp2(x);
12+
e=frexp3(x);
13+
}
14+
x=(0.3010299956639812*(double)(e));
15+
ei=(int32_t)(F64floor(x));
16+
x=(x-(double)(ei));
17+
return ucat(cat1(sf((f*pow_(10.,x))),Kc(101)),si(ei));
18+
}
19+
static void sin1(int32_t xp, int32_t yp, int32_t rp){
20+
cosin_(F64(xp),rp,1);
21+
}
22+
static void cos1(int32_t xp, int32_t yp, int32_t rp){
23+
cosin_(F64(xp),rp,2);
24+
}
25+
*/
26+
static uint64_t se(double x){
27+
int32_t ei;
28+
double f;
29+
int e;
30+
f=frexp(x,&e);
31+
x=(0.3010299956639812*(double)(e));
32+
ei=(int32_t)(F64floor(x));
33+
x=(x-(double)(ei));
34+
return ucat(cat1(sf((f*pow(10,x))),Kc(101)),si(ei));
35+
}
36+
static void sin1(int32_t xp, int32_t yp, int32_t rp){
37+
SetF64(rp,sin(F64(xp)));
38+
}
39+
static void cos1(int32_t xp, int32_t yp, int32_t rp){
40+
SetF64(rp,cos(F64(xp)));
41+
}
42+

k+/readme

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
1-
# extend k.c with some c-libraries using the native function type
2-
# blas lapack fftw lp_solve ..
1+
# k+ extends k.c with some c-libraries using the native function type.
2+
# blas lapack fftw lp_solve ode specfun ..
3+
#
4+
# m.awk also replaces the builtin math functions with libm.
5+
#
6+
# example gemm C := a*op(A)*op(B) + beta*C
7+
# gemm[a;A;rA;B;rB;op;b;C] C is reused only if refcount 1, op is 012 notrans/trans/herm
8+
# matrices(A, B) are passed as vectors, e.g. ,/3^?12
9+
# rA is the number of rows, if negative A is stored in column major format.
10+
# e.g. A:3^?12 is a list of length 3 containing length-4 vectors.
11+
# if these represent A matrix with 3 columns, rA is -4.
12+
# if they are a 3 rows of a matrix, rA is 3
13+
14+
# BLAS /usr/include/x86_64-linux-gnu/cblas-netlib.h
15+
# nrm2[x] FZ l2 vector norm
16+
# asum[x] FZ l1 vector norm
17+
# imax[x] FZ index of l1 max element
318

419
set -e
520
set -x
621
if [ "$1" = "get" ];then
722
wget -Okc.orig https://github.com/ktye/i/releases/download/latest/k.c
823
fi
924

10-
sed -e 's/math.h>$/math.h>\n#include"l.h"/' -e 's/kinit()/kinit();libs()/' kc.orig>k.c
11-
cat l.c >>k.c
12-
13-
CC=cc
14-
DD="-D BLAS"
15-
25+
awk -f m.awk kc.orig |\
26+
sed -e 's/math.h>$/math.h>\n#include"l.h"/' \
27+
-e 's/kinit()/kinit();libs()/' \
28+
>k.c
29+
cat m.c>>k.c
30+
cat l.c>>k.c
1631

17-
$CC -O3 -march=native -ok+ $DD k.c -lopenblas -lm
32+
C=cc
33+
O="-O0 -march=native"
34+
D="-D BLAS "
35+
L="-lopenblas"
36+
$C $O $D -ok+ k.c $L -lm

0 commit comments

Comments
 (0)