From 7f9f7dad00ea5f025578c491005835740f9ffd90 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Sun, 6 Oct 2019 17:17:34 +0000 Subject: asy 2.56 sources git-svn-id: svn://tug.org/texlive/trunk@52300 c570f23f-e606-0410-a88d-b1316a301751 --- Build/source/utils/asymptote/runmath.cc | 267 ++++++++++++++++---------------- 1 file changed, 131 insertions(+), 136 deletions(-) (limited to 'Build/source/utils/asymptote/runmath.cc') diff --git a/Build/source/utils/asymptote/runmath.cc b/Build/source/utils/asymptote/runmath.cc index d9686dc80c7..73854167c49 100644 --- a/Build/source/utils/asymptote/runmath.cc +++ b/Build/source/utils/asymptote/runmath.cc @@ -185,21 +185,16 @@ unsigned long long bitreverse64(unsigned long long a) ((unsigned long long) BitReverseTable8[(a >> 56)]); } -// From Warren, Jr., Henry S. (2013) [2002]. Hacker's Delight (2 ed.). -// Addison Wesley - Pearson Education, Inc. pp. 81-96. -Int popcount(Int a) +// https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel +#define T unsignedInt +Int popcount(T a) { - const uint64_t m1 = 0x5555555555555555; //binary: 0101... - const uint64_t m2 = 0x3333333333333333; //binary: 00110011.. - const uint64_t m4 = 0x0f0f0f0f0f0f0f0f; //binary: 4 zeros, 4 ones ... - const uint64_t h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3... - -// This algorithm uses 12 arithmetic operations, one of which is a multiply. - a -= (a >> 1) & m1; //put count of each 2 bits into those 2 bits - a=(a & m2)+((a >> 2) & m2); //put count of each 4 bits into those 4 bits - a=(a+(a >> 4)) & m4; //put count of each 8 bits into those 8 bits - return (a * h01) >> 56; //returns left 8 bits of a+(a << 8)+(a << 16)+(a << 24)+... + a=a-((a >> 1) & (T)~(T)0/3); + a=(a & (T)~(T)0/15*3)+((a >> 2) & (T)~(T)0/15*3); + a=(a+(a >> 4)) & (T)~(T)0/255*15; +return (T)(a*((T)~(T)0/255)) >> (sizeof(T)-1)*CHAR_BIT; } +#undef T // Return the factorial of a non-negative integer using a lookup table. Int factorial(Int n) @@ -251,233 +246,233 @@ void Srand(Int seed) #endif namespace run { -#line 195 "runmath.in" +#line 190 "runmath.in" // real ^(real x, Int y); void gen_runmath0(stack *Stack) { Int y=vm::pop(Stack); real x=vm::pop(Stack); -#line 196 "runmath.in" +#line 191 "runmath.in" {Stack->push(pow(x,y)); return;} } -#line 200 "runmath.in" +#line 195 "runmath.in" // pair ^(pair z, Int y); void gen_runmath1(stack *Stack) { Int y=vm::pop(Stack); pair z=vm::pop(Stack); -#line 201 "runmath.in" +#line 196 "runmath.in" {Stack->push(pow(z,y)); return;} } -#line 205 "runmath.in" +#line 200 "runmath.in" // Int quotient(Int x, Int y); void gen_runmath2(stack *Stack) { Int y=vm::pop(Stack); Int x=vm::pop(Stack); -#line 206 "runmath.in" +#line 201 "runmath.in" {Stack->push(quotient()(x,y)); return;} } -#line 210 "runmath.in" +#line 205 "runmath.in" // Int abs(Int x); void gen_runmath3(stack *Stack) { Int x=vm::pop(Stack); -#line 211 "runmath.in" +#line 206 "runmath.in" {Stack->push(Abs(x)); return;} } -#line 215 "runmath.in" +#line 210 "runmath.in" // Int sgn(real x); void gen_runmath4(stack *Stack) { real x=vm::pop(Stack); -#line 216 "runmath.in" +#line 211 "runmath.in" {Stack->push(sgn(x)); return;} } -#line 220 "runmath.in" +#line 215 "runmath.in" // Int rand(); void gen_runmath5(stack *Stack) { -#line 221 "runmath.in" +#line 216 "runmath.in" if(initializeRandom) Srand(1); {Stack->push(random()); return;} } -#line 227 "runmath.in" +#line 222 "runmath.in" // void srand(Int seed); void gen_runmath6(stack *Stack) { Int seed=vm::pop(Stack); -#line 228 "runmath.in" +#line 223 "runmath.in" Srand(seed); } // a random number uniformly distributed in the interval [0,1] -#line 233 "runmath.in" +#line 228 "runmath.in" // real unitrand(); void gen_runmath7(stack *Stack) { -#line 234 "runmath.in" +#line 229 "runmath.in" {Stack->push(((real) random())/RANDOM_MAX); return;} } -#line 238 "runmath.in" +#line 233 "runmath.in" // Int ceil(real x); void gen_runmath8(stack *Stack) { real x=vm::pop(Stack); -#line 239 "runmath.in" +#line 234 "runmath.in" {Stack->push(Intcast(ceil(x))); return;} } -#line 243 "runmath.in" +#line 238 "runmath.in" // Int floor(real x); void gen_runmath9(stack *Stack) { real x=vm::pop(Stack); -#line 244 "runmath.in" +#line 239 "runmath.in" {Stack->push(Intcast(floor(x))); return;} } -#line 248 "runmath.in" +#line 243 "runmath.in" // Int round(real x); void gen_runmath10(stack *Stack) { real x=vm::pop(Stack); -#line 249 "runmath.in" +#line 244 "runmath.in" if(validInt(x)) {Stack->push(Round(x)); return;} integeroverflow(0); } -#line 254 "runmath.in" +#line 249 "runmath.in" // Int Ceil(real x); void gen_runmath11(stack *Stack) { real x=vm::pop(Stack); -#line 255 "runmath.in" +#line 250 "runmath.in" {Stack->push(Ceil(x)); return;} } -#line 259 "runmath.in" +#line 254 "runmath.in" // Int Floor(real x); void gen_runmath12(stack *Stack) { real x=vm::pop(Stack); -#line 260 "runmath.in" +#line 255 "runmath.in" {Stack->push(Floor(x)); return;} } -#line 264 "runmath.in" +#line 259 "runmath.in" // Int Round(real x); void gen_runmath13(stack *Stack) { real x=vm::pop(Stack); -#line 265 "runmath.in" +#line 260 "runmath.in" {Stack->push(Round(Intcap(x))); return;} } -#line 269 "runmath.in" +#line 264 "runmath.in" // real fmod(real x, real y); void gen_runmath14(stack *Stack) { real y=vm::pop(Stack); real x=vm::pop(Stack); -#line 270 "runmath.in" +#line 265 "runmath.in" if (y == 0.0) dividebyzero(); {Stack->push(fmod(x,y)); return;} } -#line 275 "runmath.in" +#line 270 "runmath.in" // real atan2(real y, real x); void gen_runmath15(stack *Stack) { real x=vm::pop(Stack); real y=vm::pop(Stack); -#line 276 "runmath.in" +#line 271 "runmath.in" {Stack->push(atan2(y,x)); return;} } -#line 280 "runmath.in" +#line 275 "runmath.in" // real hypot(real x, real y); void gen_runmath16(stack *Stack) { real y=vm::pop(Stack); real x=vm::pop(Stack); -#line 281 "runmath.in" +#line 276 "runmath.in" {Stack->push(hypot(x,y)); return;} } -#line 285 "runmath.in" +#line 280 "runmath.in" // real remainder(real x, real y); void gen_runmath17(stack *Stack) { real y=vm::pop(Stack); real x=vm::pop(Stack); -#line 286 "runmath.in" +#line 281 "runmath.in" {Stack->push(remainder(x,y)); return;} } -#line 290 "runmath.in" +#line 285 "runmath.in" // real Jn(Int n, real x); void gen_runmath18(stack *Stack) { real x=vm::pop(Stack); Int n=vm::pop(Stack); -#line 291 "runmath.in" +#line 286 "runmath.in" {Stack->push(jn(n,x)); return;} } -#line 295 "runmath.in" +#line 290 "runmath.in" // real Yn(Int n, real x); void gen_runmath19(stack *Stack) { real x=vm::pop(Stack); Int n=vm::pop(Stack); -#line 296 "runmath.in" +#line 291 "runmath.in" {Stack->push(yn(n,x)); return;} } -#line 300 "runmath.in" +#line 295 "runmath.in" // real erf(real x); void gen_runmath20(stack *Stack) { real x=vm::pop(Stack); -#line 301 "runmath.in" +#line 296 "runmath.in" {Stack->push(erf(x)); return;} } -#line 305 "runmath.in" +#line 300 "runmath.in" // real erfc(real x); void gen_runmath21(stack *Stack) { real x=vm::pop(Stack); -#line 306 "runmath.in" +#line 301 "runmath.in" {Stack->push(erfc(x)); return;} } -#line 310 "runmath.in" +#line 305 "runmath.in" // Int factorial(Int n); void gen_runmath22(stack *Stack) { Int n=vm::pop(Stack); -#line 311 "runmath.in" +#line 306 "runmath.in" if(n < 0) error(invalidargument); {Stack->push(factorial(n)); return;} } -#line 315 "runmath.in" +#line 310 "runmath.in" // Int choose(Int n, Int k); void gen_runmath23(stack *Stack) { Int k=vm::pop(Stack); Int n=vm::pop(Stack); -#line 316 "runmath.in" +#line 311 "runmath.in" if(n < 0 || k < 0 || k > n) error(invalidargument); Int f=1; Int r=n-k; @@ -488,12 +483,12 @@ void gen_runmath23(stack *Stack) {Stack->push(f); return;} } -#line 326 "runmath.in" +#line 321 "runmath.in" // real gamma(real x); void gen_runmath24(stack *Stack) { real x=vm::pop(Stack); -#line 327 "runmath.in" +#line 322 "runmath.in" #ifdef HAVE_TGAMMA {Stack->push(tgamma(x)); return;} #else @@ -502,14 +497,14 @@ void gen_runmath24(stack *Stack) #endif } -#line 336 "runmath.in" +#line 331 "runmath.in" // realarray* quadraticroots(real a, real b, real c); void gen_runmath25(stack *Stack) { real c=vm::pop(Stack); real b=vm::pop(Stack); real a=vm::pop(Stack); -#line 337 "runmath.in" +#line 332 "runmath.in" quadraticroots q(a,b,c); array *roots=new array(q.roots); if(q.roots >= 1) (*roots)[0]=q.t1; @@ -517,14 +512,14 @@ void gen_runmath25(stack *Stack) {Stack->push(roots); return;} } -#line 345 "runmath.in" +#line 340 "runmath.in" // pairarray* quadraticroots(explicit pair a, explicit pair b, explicit pair c); void gen_runmath26(stack *Stack) { pair c=vm::pop(Stack); pair b=vm::pop(Stack); pair a=vm::pop(Stack); -#line 346 "runmath.in" +#line 341 "runmath.in" Quadraticroots q(a,b,c); array *roots=new array(q.roots); if(q.roots >= 1) (*roots)[0]=q.z1; @@ -532,7 +527,7 @@ void gen_runmath26(stack *Stack) {Stack->push(roots); return;} } -#line 354 "runmath.in" +#line 349 "runmath.in" // realarray* cubicroots(real a, real b, real c, real d); void gen_runmath27(stack *Stack) { @@ -540,7 +535,7 @@ void gen_runmath27(stack *Stack) real c=vm::pop(Stack); real b=vm::pop(Stack); real a=vm::pop(Stack); -#line 355 "runmath.in" +#line 350 "runmath.in" cubicroots q(a,b,c,d); array *roots=new array(q.roots); if(q.roots >= 1) (*roots)[0]=q.t1; @@ -551,98 +546,98 @@ void gen_runmath27(stack *Stack) // Logical operations -#line 366 "runmath.in" +#line 361 "runmath.in" // bool !(bool b); void gen_runmath28(stack *Stack) { bool b=vm::pop(Stack); -#line 367 "runmath.in" +#line 362 "runmath.in" {Stack->push(!b); return;} } -#line 372 "runmath.in" +#line 367 "runmath.in" void boolMemEq(stack *Stack) { frame * b=vm::pop(Stack); frame * a=vm::pop(Stack); -#line 373 "runmath.in" +#line 368 "runmath.in" {Stack->push(a == b); return;} } -#line 377 "runmath.in" +#line 372 "runmath.in" void boolMemNeq(stack *Stack) { frame * b=vm::pop(Stack); frame * a=vm::pop(Stack); -#line 378 "runmath.in" +#line 373 "runmath.in" {Stack->push(a != b); return;} } -#line 382 "runmath.in" +#line 377 "runmath.in" void boolFuncEq(stack *Stack) { callable * b=vm::pop(Stack); callable * a=vm::pop(Stack); -#line 383 "runmath.in" +#line 378 "runmath.in" {Stack->push(a->compare(b)); return;} } -#line 387 "runmath.in" +#line 382 "runmath.in" void boolFuncNeq(stack *Stack) { callable * b=vm::pop(Stack); callable * a=vm::pop(Stack); -#line 388 "runmath.in" +#line 383 "runmath.in" {Stack->push(!(a->compare(b))); return;} } // Bit operations -#line 394 "runmath.in" +#line 389 "runmath.in" // Int AND(Int a, Int b); void gen_runmath33(stack *Stack) { Int b=vm::pop(Stack); Int a=vm::pop(Stack); -#line 395 "runmath.in" +#line 390 "runmath.in" {Stack->push(a & b); return;} } -#line 400 "runmath.in" +#line 395 "runmath.in" // Int OR(Int a, Int b); void gen_runmath34(stack *Stack) { Int b=vm::pop(Stack); Int a=vm::pop(Stack); -#line 401 "runmath.in" +#line 396 "runmath.in" {Stack->push(a | b); return;} } -#line 405 "runmath.in" +#line 400 "runmath.in" // Int XOR(Int a, Int b); void gen_runmath35(stack *Stack) { Int b=vm::pop(Stack); Int a=vm::pop(Stack); -#line 406 "runmath.in" +#line 401 "runmath.in" {Stack->push(a ^ b); return;} } -#line 410 "runmath.in" +#line 405 "runmath.in" // Int NOT(Int a); void gen_runmath36(stack *Stack) { Int a=vm::pop(Stack); -#line 411 "runmath.in" +#line 406 "runmath.in" {Stack->push(~a); return;} } -#line 415 "runmath.in" +#line 410 "runmath.in" // Int CLZ(Int a); void gen_runmath37(stack *Stack) { Int a=vm::pop(Stack); -#line 416 "runmath.in" +#line 411 "runmath.in" if((unsigned long long) a > 0xFFFFFFFF) {Stack->push(CLZ((uint32_t) ((unsigned long long) a >> 32))); return;} else { @@ -652,32 +647,32 @@ void gen_runmath37(stack *Stack) } } -#line 426 "runmath.in" +#line 421 "runmath.in" // Int popcount(Int a); void gen_runmath38(stack *Stack) { Int a=vm::pop(Stack); -#line 427 "runmath.in" +#line 422 "runmath.in" {Stack->push(popcount(a)); return;} } -#line 431 "runmath.in" +#line 426 "runmath.in" // Int CTZ(Int a); void gen_runmath39(stack *Stack) { Int a=vm::pop(Stack); -#line 432 "runmath.in" +#line 427 "runmath.in" {Stack->push(popcount((a&-a)-1)); return;} } // bitreverse a within a word of length bits. -#line 437 "runmath.in" +#line 432 "runmath.in" // Int bitreverse(Int a, Int bits); void gen_runmath40(stack *Stack) { Int bits=vm::pop(Stack); Int a=vm::pop(Stack); -#line 438 "runmath.in" +#line 433 "runmath.in" typedef unsigned long long Bitreverse(unsigned long long a); static Bitreverse *B[]={bitreverse8,bitreverse16,bitreverse24,bitreverse32, bitreverse40,bitreverse48,bitreverse56,bitreverse64}; @@ -698,87 +693,87 @@ namespace trans { void gen_runmath_venv(venv &ve) { -#line 195 "runmath.in" +#line 190 "runmath.in" addFunc(ve, run::gen_runmath0, primReal(), SYM_CARET, formal(primReal(), SYM(x), false, false), formal(primInt(), SYM(y), false, false)); -#line 200 "runmath.in" +#line 195 "runmath.in" addFunc(ve, run::gen_runmath1, primPair(), SYM_CARET, formal(primPair(), SYM(z), false, false), formal(primInt(), SYM(y), false, false)); -#line 205 "runmath.in" +#line 200 "runmath.in" addFunc(ve, run::gen_runmath2, primInt(), SYM(quotient), formal(primInt(), SYM(x), false, false), formal(primInt(), SYM(y), false, false)); -#line 210 "runmath.in" +#line 205 "runmath.in" addFunc(ve, run::gen_runmath3, primInt(), SYM(abs), formal(primInt(), SYM(x), false, false)); -#line 215 "runmath.in" +#line 210 "runmath.in" addFunc(ve, run::gen_runmath4, primInt(), SYM(sgn), formal(primReal(), SYM(x), false, false)); -#line 220 "runmath.in" +#line 215 "runmath.in" addFunc(ve, run::gen_runmath5, primInt(), SYM(rand)); -#line 227 "runmath.in" +#line 222 "runmath.in" addFunc(ve, run::gen_runmath6, primVoid(), SYM(srand), formal(primInt(), SYM(seed), false, false)); -#line 232 "runmath.in" +#line 227 "runmath.in" addFunc(ve, run::gen_runmath7, primReal(), SYM(unitrand)); -#line 238 "runmath.in" +#line 233 "runmath.in" addFunc(ve, run::gen_runmath8, primInt(), SYM(ceil), formal(primReal(), SYM(x), false, false)); -#line 243 "runmath.in" +#line 238 "runmath.in" addFunc(ve, run::gen_runmath9, primInt(), SYM(floor), formal(primReal(), SYM(x), false, false)); -#line 248 "runmath.in" +#line 243 "runmath.in" addFunc(ve, run::gen_runmath10, primInt(), SYM(round), formal(primReal(), SYM(x), false, false)); -#line 254 "runmath.in" +#line 249 "runmath.in" addFunc(ve, run::gen_runmath11, primInt(), SYM(Ceil), formal(primReal(), SYM(x), false, false)); -#line 259 "runmath.in" +#line 254 "runmath.in" addFunc(ve, run::gen_runmath12, primInt(), SYM(Floor), formal(primReal(), SYM(x), false, false)); -#line 264 "runmath.in" +#line 259 "runmath.in" addFunc(ve, run::gen_runmath13, primInt(), SYM(Round), formal(primReal(), SYM(x), false, false)); -#line 269 "runmath.in" +#line 264 "runmath.in" addFunc(ve, run::gen_runmath14, primReal(), SYM(fmod), formal(primReal(), SYM(x), false, false), formal(primReal(), SYM(y), false, false)); -#line 275 "runmath.in" +#line 270 "runmath.in" addFunc(ve, run::gen_runmath15, primReal(), SYM(atan2), formal(primReal(), SYM(y), false, false), formal(primReal(), SYM(x), false, false)); -#line 280 "runmath.in" +#line 275 "runmath.in" addFunc(ve, run::gen_runmath16, primReal(), SYM(hypot), formal(primReal(), SYM(x), false, false), formal(primReal(), SYM(y), false, false)); -#line 285 "runmath.in" +#line 280 "runmath.in" addFunc(ve, run::gen_runmath17, primReal(), SYM(remainder), formal(primReal(), SYM(x), false, false), formal(primReal(), SYM(y), false, false)); -#line 290 "runmath.in" +#line 285 "runmath.in" addFunc(ve, run::gen_runmath18, primReal(), SYM(Jn), formal(primInt(), SYM(n), false, false), formal(primReal(), SYM(x), false, false)); -#line 295 "runmath.in" +#line 290 "runmath.in" addFunc(ve, run::gen_runmath19, primReal(), SYM(Yn), formal(primInt(), SYM(n), false, false), formal(primReal(), SYM(x), false, false)); -#line 300 "runmath.in" +#line 295 "runmath.in" addFunc(ve, run::gen_runmath20, primReal(), SYM(erf), formal(primReal(), SYM(x), false, false)); -#line 305 "runmath.in" +#line 300 "runmath.in" addFunc(ve, run::gen_runmath21, primReal(), SYM(erfc), formal(primReal(), SYM(x), false, false)); -#line 310 "runmath.in" +#line 305 "runmath.in" addFunc(ve, run::gen_runmath22, primInt(), SYM(factorial), formal(primInt(), SYM(n), false, false)); -#line 315 "runmath.in" +#line 310 "runmath.in" addFunc(ve, run::gen_runmath23, primInt(), SYM(choose), formal(primInt(), SYM(n), false, false), formal(primInt(), SYM(k), false, false)); -#line 326 "runmath.in" +#line 321 "runmath.in" addFunc(ve, run::gen_runmath24, primReal(), SYM(gamma), formal(primReal(), SYM(x), false, false)); -#line 336 "runmath.in" +#line 331 "runmath.in" addFunc(ve, run::gen_runmath25, realArray(), SYM(quadraticroots), formal(primReal(), SYM(a), false, false), formal(primReal(), SYM(b), false, false), formal(primReal(), SYM(c), false, false)); -#line 345 "runmath.in" +#line 340 "runmath.in" addFunc(ve, run::gen_runmath26, pairArray(), SYM(quadraticroots), formal(primPair(), SYM(a), false, true), formal(primPair(), SYM(b), false, true), formal(primPair(), SYM(c), false, true)); -#line 354 "runmath.in" +#line 349 "runmath.in" addFunc(ve, run::gen_runmath27, realArray(), SYM(cubicroots), formal(primReal(), SYM(a), false, false), formal(primReal(), SYM(b), false, false), formal(primReal(), SYM(c), false, false), formal(primReal(), SYM(d), false, false)); -#line 364 "runmath.in" +#line 359 "runmath.in" addFunc(ve, run::gen_runmath28, primBoolean(), SYM_LOGNOT, formal(primBoolean(), SYM(b), false, false)); -#line 372 "runmath.in" +#line 367 "runmath.in" REGISTER_BLTIN(run::boolMemEq,"boolMemEq"); -#line 377 "runmath.in" +#line 372 "runmath.in" REGISTER_BLTIN(run::boolMemNeq,"boolMemNeq"); -#line 382 "runmath.in" +#line 377 "runmath.in" REGISTER_BLTIN(run::boolFuncEq,"boolFuncEq"); -#line 387 "runmath.in" +#line 382 "runmath.in" REGISTER_BLTIN(run::boolFuncNeq,"boolFuncNeq"); -#line 392 "runmath.in" +#line 387 "runmath.in" addFunc(ve, run::gen_runmath33, primInt(), SYM(AND), formal(primInt(), SYM(a), false, false), formal(primInt(), SYM(b), false, false)); -#line 400 "runmath.in" +#line 395 "runmath.in" addFunc(ve, run::gen_runmath34, primInt(), SYM(OR), formal(primInt(), SYM(a), false, false), formal(primInt(), SYM(b), false, false)); -#line 405 "runmath.in" +#line 400 "runmath.in" addFunc(ve, run::gen_runmath35, primInt(), SYM(XOR), formal(primInt(), SYM(a), false, false), formal(primInt(), SYM(b), false, false)); -#line 410 "runmath.in" +#line 405 "runmath.in" addFunc(ve, run::gen_runmath36, primInt(), SYM(NOT), formal(primInt(), SYM(a), false, false)); -#line 415 "runmath.in" +#line 410 "runmath.in" addFunc(ve, run::gen_runmath37, primInt(), SYM(CLZ), formal(primInt(), SYM(a), false, false)); -#line 426 "runmath.in" +#line 421 "runmath.in" addFunc(ve, run::gen_runmath38, primInt(), SYM(popcount), formal(primInt(), SYM(a), false, false)); -#line 431 "runmath.in" +#line 426 "runmath.in" addFunc(ve, run::gen_runmath39, primInt(), SYM(CTZ), formal(primInt(), SYM(a), false, false)); -#line 436 "runmath.in" +#line 431 "runmath.in" addFunc(ve, run::gen_runmath40, primInt(), SYM(bitreverse), formal(primInt(), SYM(a), false, false), formal(primInt(), SYM(bits), false, false)); } -- cgit v1.2.3