diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-05 16:21:43 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-05 16:21:43 +0100 |
commit | 0ad36c46c7a521760e5696ba9d6e9362a1a3777c (patch) | |
tree | c14a8f4b28d3445df5daf802c16a6ddef463bf5c | |
parent | 24fb2cd62fcc2c422692df4138e0f1e333a52c42 (diff) | |
download | busybox-w32-0ad36c46c7a521760e5696ba9d6e9362a1a3777c.tar.gz busybox-w32-0ad36c46c7a521760e5696ba9d6e9362a1a3777c.tar.bz2 busybox-w32-0ad36c46c7a521760e5696ba9d6e9362a1a3777c.zip |
bc: do not check for errors when parsing/running internal library
function old new delta
bc_lib_name 4 - -4
bc_vm_run 1921 1900 -21
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 0/1 up/down: 0/-25) Total: -25 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/bc.c | 370 |
1 files changed, 186 insertions, 184 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 6f2606c79..fd9e31cc5 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
@@ -915,185 +915,6 @@ static const BcNumBinaryOp bc_program_ops[] = { | |||
915 | 915 | ||
916 | static const char bc_program_stdin_name[] = "<stdin>"; | 916 | static const char bc_program_stdin_name[] = "<stdin>"; |
917 | 917 | ||
918 | #if ENABLE_BC | ||
919 | static const char *bc_lib_name = "gen/lib.bc"; | ||
920 | |||
921 | static const char bc_lib[] = { | ||
922 | "scale=20" | ||
923 | "\n" "define e(x){" | ||
924 | "\n" "auto b,s,n,r,d,i,p,f,v" | ||
925 | "\n" "b=ibase" | ||
926 | "\n" "ibase=A" | ||
927 | "\n" "if(x<0){" | ||
928 | "\n" "n=1" | ||
929 | "\n" "x=-x" | ||
930 | "\n" "}" | ||
931 | "\n" "s=scale" | ||
932 | "\n" "r=6+s+0.44*x" | ||
933 | "\n" "scale=scale(x)+1" | ||
934 | "\n" "while(x>1){" | ||
935 | "\n" "d+=1" | ||
936 | "\n" "x/=2" | ||
937 | "\n" "scale+=1" | ||
938 | "\n" "}" | ||
939 | "\n" "scale=r" | ||
940 | "\n" "r=x+1" | ||
941 | "\n" "p=x" | ||
942 | "\n" "f=v=1" | ||
943 | "\n" "for(i=2;v!=0;++i){" | ||
944 | "\n" "p*=x" | ||
945 | "\n" "f*=i" | ||
946 | "\n" "v=p/f" | ||
947 | "\n" "r+=v" | ||
948 | "\n" "}" | ||
949 | "\n" "while((d--)!=0)r*=r" | ||
950 | "\n" "scale=s" | ||
951 | "\n" "ibase=b" | ||
952 | "\n" "if(n!=0)return(1/r)" | ||
953 | "\n" "return(r/1)" | ||
954 | "\n" "}" | ||
955 | "\n" "define l(x){" | ||
956 | "\n" "auto b,s,r,p,a,q,i,v" | ||
957 | "\n" "b=ibase" | ||
958 | "\n" "ibase=A" | ||
959 | "\n" "if(x<=0){" | ||
960 | "\n" "r=(1-10^scale)/1" | ||
961 | "\n" "ibase=b" | ||
962 | "\n" "return(r)" | ||
963 | "\n" "}" | ||
964 | "\n" "s=scale" | ||
965 | "\n" "scale+=6" | ||
966 | "\n" "p=2" | ||
967 | "\n" "while(x>=2){" | ||
968 | "\n" "p*=2" | ||
969 | "\n" "x=sqrt(x)" | ||
970 | "\n" "}" | ||
971 | "\n" "while(x<=0.5){" | ||
972 | "\n" "p*=2" | ||
973 | "\n" "x=sqrt(x)" | ||
974 | "\n" "}" | ||
975 | "\n" "r=a=(x-1)/(x+1)" | ||
976 | "\n" "q=a*a" | ||
977 | "\n" "v=1" | ||
978 | "\n" "for(i=3;v!=0;i+=2){" | ||
979 | "\n" "a*=q" | ||
980 | "\n" "v=a/i" | ||
981 | "\n" "r+=v" | ||
982 | "\n" "}" | ||
983 | "\n" "r*=p" | ||
984 | "\n" "scale=s" | ||
985 | "\n" "ibase=b" | ||
986 | "\n" "return(r/1)" | ||
987 | "\n" "}" | ||
988 | "\n" "define s(x){" | ||
989 | "\n" "auto b,s,r,n,a,q,i" | ||
990 | "\n" "b=ibase" | ||
991 | "\n" "ibase=A" | ||
992 | "\n" "s=scale" | ||
993 | "\n" "scale=1.1*s+2" | ||
994 | "\n" "a=a(1)" | ||
995 | "\n" "if(x<0){" | ||
996 | "\n" "n=1" | ||
997 | "\n" "x=-x" | ||
998 | "\n" "}" | ||
999 | "\n" "scale=0" | ||
1000 | "\n" "q=(x/a+2)/4" | ||
1001 | "\n" "x=x-4*q*a" | ||
1002 | "\n" "if(q%2!=0)x=-x" | ||
1003 | "\n" "scale=s+2" | ||
1004 | "\n" "r=a=x" | ||
1005 | "\n" "q=-x*x" | ||
1006 | "\n" "for(i=3;a!=0;i+=2){" | ||
1007 | "\n" "a*=q/(i*(i-1))" | ||
1008 | "\n" "r+=a" | ||
1009 | "\n" "}" | ||
1010 | "\n" "scale=s" | ||
1011 | "\n" "ibase=b" | ||
1012 | "\n" "if(n!=0)return(-r/1)" | ||
1013 | "\n" "return(r/1)" | ||
1014 | "\n" "}" | ||
1015 | "\n" "define c(x){" | ||
1016 | "\n" "auto b,s" | ||
1017 | "\n" "b=ibase" | ||
1018 | "\n" "ibase=A" | ||
1019 | "\n" "s=scale" | ||
1020 | "\n" "scale*=1.2" | ||
1021 | "\n" "x=s(2*a(1)+x)" | ||
1022 | "\n" "scale=s" | ||
1023 | "\n" "ibase=b" | ||
1024 | "\n" "return(x/1)" | ||
1025 | "\n" "}" | ||
1026 | "\n" "define a(x){" | ||
1027 | "\n" "auto b,s,r,n,a,m,t,f,i,u" | ||
1028 | "\n" "b=ibase" | ||
1029 | "\n" "ibase=A" | ||
1030 | "\n" "n=1" | ||
1031 | "\n" "if(x<0){" | ||
1032 | "\n" "n=-1" | ||
1033 | "\n" "x=-x" | ||
1034 | "\n" "}" | ||
1035 | "\n" "if(x==1){" | ||
1036 | "\n" "if(scale<65){" | ||
1037 | "\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)" | ||
1038 | "\n" "}" | ||
1039 | "\n" "}" | ||
1040 | "\n" "if(x==.2){" | ||
1041 | "\n" "if(scale<65){" | ||
1042 | "\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)" | ||
1043 | "\n" "}" | ||
1044 | "\n" "}" | ||
1045 | "\n" "s=scale" | ||
1046 | "\n" "if(x>.2){" | ||
1047 | "\n" "scale+=5" | ||
1048 | "\n" "a=a(.2)" | ||
1049 | "\n" "}" | ||
1050 | "\n" "scale=s+3" | ||
1051 | "\n" "while(x>.2){" | ||
1052 | "\n" "m+=1" | ||
1053 | "\n" "x=(x-.2)/(1+.2*x)" | ||
1054 | "\n" "}" | ||
1055 | "\n" "r=u=x" | ||
1056 | "\n" "f=-x*x" | ||
1057 | "\n" "t=1" | ||
1058 | "\n" "for(i=3;t!=0;i+=2){" | ||
1059 | "\n" "u*=f" | ||
1060 | "\n" "t=u/i" | ||
1061 | "\n" "r+=t" | ||
1062 | "\n" "}" | ||
1063 | "\n" "scale=s" | ||
1064 | "\n" "ibase=b" | ||
1065 | "\n" "return((m*a+r)/n)" | ||
1066 | "\n" "}" | ||
1067 | "\n" "define j(n,x){" | ||
1068 | "\n" "auto b,s,o,a,i,v,f" | ||
1069 | "\n" "b=ibase" | ||
1070 | "\n" "ibase=A" | ||
1071 | "\n" "s=scale" | ||
1072 | "\n" "scale=0" | ||
1073 | "\n" "n/=1" | ||
1074 | "\n" "if(n<0){" | ||
1075 | "\n" "n=-n" | ||
1076 | "\n" "if(n%2==1)o=1" | ||
1077 | "\n" "}" | ||
1078 | "\n" "a=1" | ||
1079 | "\n" "for(i=2;i<=n;++i)a*=i" | ||
1080 | "\n" "scale=1.5*s" | ||
1081 | "\n" "a=(x^n)/2^n/a" | ||
1082 | "\n" "r=v=1" | ||
1083 | "\n" "f=-x*x/4" | ||
1084 | "\n" "scale=scale+length(a)-scale(a)" | ||
1085 | "\n" "for(i=1;v!=0;++i){" | ||
1086 | "\n" "v=v*f/i/(n+i)" | ||
1087 | "\n" "r+=v" | ||
1088 | "\n" "}" | ||
1089 | "\n" "scale=s" | ||
1090 | "\n" "ibase=b" | ||
1091 | "\n" "if(o!=0)a=-a" | ||
1092 | "\n" "return(a*r/1)" | ||
1093 | "\n" "}" | ||
1094 | }; | ||
1095 | #endif // ENABLE_BC | ||
1096 | |||
1097 | static void fflush_and_check(void) | 918 | static void fflush_and_check(void) |
1098 | { | 919 | { |
1099 | fflush_all(); | 920 | fflush_all(); |
@@ -7147,6 +6968,183 @@ static BcStatus bc_vm_stdin(void) | |||
7147 | return s; | 6968 | return s; |
7148 | } | 6969 | } |
7149 | 6970 | ||
6971 | #if ENABLE_BC | ||
6972 | static const char bc_lib[] = { | ||
6973 | "scale=20" | ||
6974 | "\n" "define e(x){" | ||
6975 | "\n" "auto b,s,n,r,d,i,p,f,v" | ||
6976 | "\n" "b=ibase" | ||
6977 | "\n" "ibase=A" | ||
6978 | "\n" "if(x<0){" | ||
6979 | "\n" "n=1" | ||
6980 | "\n" "x=-x" | ||
6981 | "\n" "}" | ||
6982 | "\n" "s=scale" | ||
6983 | "\n" "r=6+s+0.44*x" | ||
6984 | "\n" "scale=scale(x)+1" | ||
6985 | "\n" "while(x>1){" | ||
6986 | "\n" "d+=1" | ||
6987 | "\n" "x/=2" | ||
6988 | "\n" "scale+=1" | ||
6989 | "\n" "}" | ||
6990 | "\n" "scale=r" | ||
6991 | "\n" "r=x+1" | ||
6992 | "\n" "p=x" | ||
6993 | "\n" "f=v=1" | ||
6994 | "\n" "for(i=2;v!=0;++i){" | ||
6995 | "\n" "p*=x" | ||
6996 | "\n" "f*=i" | ||
6997 | "\n" "v=p/f" | ||
6998 | "\n" "r+=v" | ||
6999 | "\n" "}" | ||
7000 | "\n" "while((d--)!=0)r*=r" | ||
7001 | "\n" "scale=s" | ||
7002 | "\n" "ibase=b" | ||
7003 | "\n" "if(n!=0)return(1/r)" | ||
7004 | "\n" "return(r/1)" | ||
7005 | "\n" "}" | ||
7006 | "\n" "define l(x){" | ||
7007 | "\n" "auto b,s,r,p,a,q,i,v" | ||
7008 | "\n" "b=ibase" | ||
7009 | "\n" "ibase=A" | ||
7010 | "\n" "if(x<=0){" | ||
7011 | "\n" "r=(1-10^scale)/1" | ||
7012 | "\n" "ibase=b" | ||
7013 | "\n" "return(r)" | ||
7014 | "\n" "}" | ||
7015 | "\n" "s=scale" | ||
7016 | "\n" "scale+=6" | ||
7017 | "\n" "p=2" | ||
7018 | "\n" "while(x>=2){" | ||
7019 | "\n" "p*=2" | ||
7020 | "\n" "x=sqrt(x)" | ||
7021 | "\n" "}" | ||
7022 | "\n" "while(x<=0.5){" | ||
7023 | "\n" "p*=2" | ||
7024 | "\n" "x=sqrt(x)" | ||
7025 | "\n" "}" | ||
7026 | "\n" "r=a=(x-1)/(x+1)" | ||
7027 | "\n" "q=a*a" | ||
7028 | "\n" "v=1" | ||
7029 | "\n" "for(i=3;v!=0;i+=2){" | ||
7030 | "\n" "a*=q" | ||
7031 | "\n" "v=a/i" | ||
7032 | "\n" "r+=v" | ||
7033 | "\n" "}" | ||
7034 | "\n" "r*=p" | ||
7035 | "\n" "scale=s" | ||
7036 | "\n" "ibase=b" | ||
7037 | "\n" "return(r/1)" | ||
7038 | "\n" "}" | ||
7039 | "\n" "define s(x){" | ||
7040 | "\n" "auto b,s,r,n,a,q,i" | ||
7041 | "\n" "b=ibase" | ||
7042 | "\n" "ibase=A" | ||
7043 | "\n" "s=scale" | ||
7044 | "\n" "scale=1.1*s+2" | ||
7045 | "\n" "a=a(1)" | ||
7046 | "\n" "if(x<0){" | ||
7047 | "\n" "n=1" | ||
7048 | "\n" "x=-x" | ||
7049 | "\n" "}" | ||
7050 | "\n" "scale=0" | ||
7051 | "\n" "q=(x/a+2)/4" | ||
7052 | "\n" "x=x-4*q*a" | ||
7053 | "\n" "if(q%2!=0)x=-x" | ||
7054 | "\n" "scale=s+2" | ||
7055 | "\n" "r=a=x" | ||
7056 | "\n" "q=-x*x" | ||
7057 | "\n" "for(i=3;a!=0;i+=2){" | ||
7058 | "\n" "a*=q/(i*(i-1))" | ||
7059 | "\n" "r+=a" | ||
7060 | "\n" "}" | ||
7061 | "\n" "scale=s" | ||
7062 | "\n" "ibase=b" | ||
7063 | "\n" "if(n!=0)return(-r/1)" | ||
7064 | "\n" "return(r/1)" | ||
7065 | "\n" "}" | ||
7066 | "\n" "define c(x){" | ||
7067 | "\n" "auto b,s" | ||
7068 | "\n" "b=ibase" | ||
7069 | "\n" "ibase=A" | ||
7070 | "\n" "s=scale" | ||
7071 | "\n" "scale*=1.2" | ||
7072 | "\n" "x=s(2*a(1)+x)" | ||
7073 | "\n" "scale=s" | ||
7074 | "\n" "ibase=b" | ||
7075 | "\n" "return(x/1)" | ||
7076 | "\n" "}" | ||
7077 | "\n" "define a(x){" | ||
7078 | "\n" "auto b,s,r,n,a,m,t,f,i,u" | ||
7079 | "\n" "b=ibase" | ||
7080 | "\n" "ibase=A" | ||
7081 | "\n" "n=1" | ||
7082 | "\n" "if(x<0){" | ||
7083 | "\n" "n=-1" | ||
7084 | "\n" "x=-x" | ||
7085 | "\n" "}" | ||
7086 | "\n" "if(x==1){" | ||
7087 | "\n" "if(scale<65){" | ||
7088 | "\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)" | ||
7089 | "\n" "}" | ||
7090 | "\n" "}" | ||
7091 | "\n" "if(x==.2){" | ||
7092 | "\n" "if(scale<65){" | ||
7093 | "\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)" | ||
7094 | "\n" "}" | ||
7095 | "\n" "}" | ||
7096 | "\n" "s=scale" | ||
7097 | "\n" "if(x>.2){" | ||
7098 | "\n" "scale+=5" | ||
7099 | "\n" "a=a(.2)" | ||
7100 | "\n" "}" | ||
7101 | "\n" "scale=s+3" | ||
7102 | "\n" "while(x>.2){" | ||
7103 | "\n" "m+=1" | ||
7104 | "\n" "x=(x-.2)/(1+.2*x)" | ||
7105 | "\n" "}" | ||
7106 | "\n" "r=u=x" | ||
7107 | "\n" "f=-x*x" | ||
7108 | "\n" "t=1" | ||
7109 | "\n" "for(i=3;t!=0;i+=2){" | ||
7110 | "\n" "u*=f" | ||
7111 | "\n" "t=u/i" | ||
7112 | "\n" "r+=t" | ||
7113 | "\n" "}" | ||
7114 | "\n" "scale=s" | ||
7115 | "\n" "ibase=b" | ||
7116 | "\n" "return((m*a+r)/n)" | ||
7117 | "\n" "}" | ||
7118 | "\n" "define j(n,x){" | ||
7119 | "\n" "auto b,s,o,a,i,v,f" | ||
7120 | "\n" "b=ibase" | ||
7121 | "\n" "ibase=A" | ||
7122 | "\n" "s=scale" | ||
7123 | "\n" "scale=0" | ||
7124 | "\n" "n/=1" | ||
7125 | "\n" "if(n<0){" | ||
7126 | "\n" "n=-n" | ||
7127 | "\n" "if(n%2==1)o=1" | ||
7128 | "\n" "}" | ||
7129 | "\n" "a=1" | ||
7130 | "\n" "for(i=2;i<=n;++i)a*=i" | ||
7131 | "\n" "scale=1.5*s" | ||
7132 | "\n" "a=(x^n)/2^n/a" | ||
7133 | "\n" "r=v=1" | ||
7134 | "\n" "f=-x*x/4" | ||
7135 | "\n" "scale=scale+length(a)-scale(a)" | ||
7136 | "\n" "for(i=1;v!=0;++i){" | ||
7137 | "\n" "v=v*f/i/(n+i)" | ||
7138 | "\n" "r+=v" | ||
7139 | "\n" "}" | ||
7140 | "\n" "scale=s" | ||
7141 | "\n" "ibase=b" | ||
7142 | "\n" "if(o!=0)a=-a" | ||
7143 | "\n" "return(a*r/1)" | ||
7144 | "\n" "}" | ||
7145 | }; | ||
7146 | #endif // ENABLE_BC | ||
7147 | |||
7150 | static BcStatus bc_vm_exec(void) | 7148 | static BcStatus bc_vm_exec(void) |
7151 | { | 7149 | { |
7152 | BcStatus s = BC_STATUS_SUCCESS; | 7150 | BcStatus s = BC_STATUS_SUCCESS; |
@@ -7155,15 +7153,19 @@ static BcStatus bc_vm_exec(void) | |||
7155 | #if ENABLE_BC | 7153 | #if ENABLE_BC |
7156 | if (option_mask32 & BC_FLAG_L) { | 7154 | if (option_mask32 & BC_FLAG_L) { |
7157 | 7155 | ||
7158 | bc_lex_file(&G.prs.l, bc_lib_name); | 7156 | // We know that internal library is not buggy, |
7157 | // thus error checking is normally disabled. | ||
7158 | # define DEBUG_LIB 0 | ||
7159 | bc_lex_file(&G.prs.l, ""); | ||
7159 | s = bc_parse_text(&G.prs, bc_lib); | 7160 | s = bc_parse_text(&G.prs, bc_lib); |
7161 | if (DEBUG_LIB && s) return s; | ||
7160 | 7162 | ||
7161 | while (!s && G.prs.l.t.t != BC_LEX_EOF) | 7163 | while (G.prs.l.t.t != BC_LEX_EOF) { |
7162 | s = G.prs.parse(&G.prs); | 7164 | s = G.prs.parse(&G.prs); |
7163 | 7165 | if (DEBUG_LIB && s) return s; | |
7164 | if (s) return s; | 7166 | } |
7165 | s = bc_program_exec(); | 7167 | s = bc_program_exec(); |
7166 | if (s) return s; | 7168 | if (DEBUG_LIB && s) return s; |
7167 | } | 7169 | } |
7168 | #endif | 7170 | #endif |
7169 | 7171 | ||