void run_log2_tests(void) { int i; /* * We should insist that log2() return exactly the correct * result and not raise an inexact exception for powers of 2. */ feclearexcept(FE_ALL_EXCEPT); for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) { assert(log2f(ldexpf(1.0, i)) == i); assert(fetestexcept(ALL_STD_EXCEPT) == 0); } for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) { assert(log2(ldexp(1.0, i)) == i); assert(fetestexcept(ALL_STD_EXCEPT) == 0); } for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) { assert(log2l(ldexpl(1.0, i)) == i); #if 0 /* XXX This test does not pass yet. */ assert(fetestexcept(ALL_STD_EXCEPT) == 0); #endif } }
TEST(math, exp2_STRICT_ALIGN_OpenBSD_bug) { // OpenBSD/x86's libm had a bug here, but it was already fixed in FreeBSD: // http://svnweb.FreeBSD.org/base/head/lib/msun/src/math_private.h?revision=240827&view=markup ASSERT_DOUBLE_EQ(5.0, exp2(log2(5))); ASSERT_FLOAT_EQ(5.0f, exp2f(log2f(5))); ASSERT_DOUBLE_EQ(5.0L, exp2l(log2l(5))); }
static TACommandVerdict log2l_cmd(TAThread thread,TAInputStream stream) { long double x, res; // Prepare x = readLongDouble(&stream); errno = 0; START_TARGET_OPERATION(thread); // Execute res = log2l(x); END_TARGET_OPERATION(thread); // Response writeLongDouble(thread, res); writeInt(thread, errno); sendResponse(thread); return taDefaultVerdict; }
int main() { ll k,temp; double res,l,r,temp1,temp3; scanf("%d",&t); while(t--) { scanf("%d%lld",&x,&k); if(k==1) printf("%lf\n",x/2.0); else { i=(int)log2l(k); //printf("i %d\n",i); temp=(ll)pow(2,i); temp1=rec(temp); // printf("pow %lf\n",temp1); res=temp1; if((k&(k-1))!=0){ temp3=(k-temp)*2*temp1; // printf("pow %lf\n",temp3); res=(temp3+(temp3+2*temp1))/2.0; } printf("%lf\n",res); } } return 0; }
int main(void) { #pragma STDC FENV_ACCESS ON long double y; float d; int e, i, err = 0; struct l_l *p; for (i = 0; i < sizeof t/sizeof *t; i++) { p = t + i; if (p->r < 0) continue; fesetround(p->r); feclearexcept(FE_ALL_EXCEPT); y = log2l(p->x); e = fetestexcept(INEXACT|INVALID|DIVBYZERO|UNDERFLOW|OVERFLOW); if (!checkexcept(e, p->e, p->r)) { printf("%s:%d: bad fp exception: %s log2l(%La)=%La, want %s", p->file, p->line, rstr(p->r), p->x, p->y, estr(p->e)); printf(" got %s\n", estr(e)); err++; } d = ulperrl(y, p->y, p->dy); if (!checkulp(d, p->r)) { printf("%s:%d: %s log2l(%La) want %La got %La ulperr %.3f = %a + %a\n", p->file, p->line, rstr(p->r), p->x, p->y, y, d, d-p->dy, p->dy); err++; } } return !!err; }
int bpf_prog2(struct pt_regs *ctx) { u64 *ts, cur_ts, delta; int key, cpu; long *val; cpu = bpf_get_smp_processor_id(); ts = bpf_map_lookup_elem(&my_map, &cpu); if (!ts) return 0; cur_ts = bpf_ktime_get_ns(); delta = log2l(cur_ts - *ts); if (delta > MAX_ENTRIES - 1) delta = MAX_ENTRIES - 1; key = cpu * MAX_ENTRIES + delta; val = bpf_map_lookup_elem(&my_lat, &key); if (val) __sync_fetch_and_add((long *)val, 1); return 0; }
void test_log2() { static_assert((std::is_same<decltype(log2((double)0)), double>::value), ""); static_assert((std::is_same<decltype(log2f(0)), float>::value), ""); static_assert((std::is_same<decltype(log2l(0)), long double>::value), ""); assert(log2(1) == 0); }
// // powl // long double powl(long double x, long double y) { if(isnan(x)) return x; if(isnan(y)) return y; return exp2l(y * log2l(x)); }
inline long double operator()(const long double& x) const { #if defined(NTA_OS_WINDOWS) return log(x) / log(2.0); #else return log2l(x); #endif }
int main(void){ //int byte = 0; unsigned char *byte; unsigned long long total = 0; unsigned long long bc[256]; long double prob; long double entropy = 0.0; const int bs = 4096; int i; int status; byte = malloc(bs); if( byte == NULL ){ fputs("Can't allocate memory.",stderr); exit( 1 ); } memset(byte,0,bs); memset(bc,0,sizeof(bc)); while(1){ status = fread(byte,1,bs,stdin); if(status < bs){ break; } for(i=0;i<bs;i++){ bc[byte[i]]++; } } for(i=0;i<status;i++){ bc[byte[i]]++; } i = 0; while(1){ printf("%3d : %lld\n",i,bc[i]); total += bc[i]; i++; if(i > 255){ printf("total : %lld\n",total); break; } } i = 0; while(1){ if(bc[i] != 0){ prob = (long double) bc[i] / (long double) total; entropy += prob * log2l(prob); } i++; if(i > 255){ printf("entropy : %Lf\n",-entropy); break; } } return 0; }
vector<Trust<int64_t> *> *Stoichastic::pickRandomTrusts(LLRB_Tree<Trust<int64_t> *, uint64_t> *tree, vector<Trust<int64_t> *> *originals, uint64_t requests) { LLRB_TreeNode<Trust<int64_t> *, uint64_t> *curr; LLRB_Tree<Trust<int64_t> *, uint64_t> randoms; vector<LLRB_TreeNode<Trust<int64_t> *, uint64_t> *> stack; uint64_t tmp; uint8_t leftDepth = 0, rightDepth = 0; int64_t pivot; curr = tree->treeRoot; while (curr != randoms.leftOf(curr)) { leftDepth++; curr = randoms.leftOf(curr); } curr = tree->treeRoot; while (curr != randoms.leftOf(curr)) { rightDepth++; curr = randoms.rightOf(curr); } pivot = (int64_t)leftDepth / ((int64_t)(leftDepth+rightDepth)); if (originals != NULL) { for (uint64_t ix = 0; ix < originals->size(); ix++) { randoms.insert(originals->at(ix), (uint64_t)originals->at(ix)); } originals->resize(0); delete originals; } for (uint64_t ix = 0; ix < requests; ix++) { curr = tree->treeRoot; while (curr != randoms.leftOf(curr)) { stack.push_back(curr); if ((rand() / (int64_t)RAND_MAX) < pivot) { curr = randoms.leftOf(curr); } else { curr = randoms.rightOf(curr); } } tmp = random() % (uint64_t)powl(2, stack.size()); Trust<int64_t> *entry = stack[log2l(tmp)]->data; randoms.insert(entry, (uint64_t)entry); stack.resize(0); } return randoms.select(NULL, NULL); }
int main(int argc, char ** argv){ int p, m; sscanf(argv[1], "%d", &p); sscanf(argv[2], "%d", &m); // What dimensions are necessary? int x = sqrt(p); int y = sqrt(p); float result = sqrt(p); float check = fmod(result, 1); if(check != 0) { float a = log2l(p); x = pow(2, ((a + 1) / 2)); y = p / x; } printf("%d x %d\n\n", x, y); // What coordinates do I need to obtain? // First find leftover. int size_x = m / p; int leftover_x = m - (p * size_x); int my_rank; for(my_rank == 0; my_rank < p; my_rank++) { int start_adjust_x = 0; int end_adjust_x = 0; if(leftover_x != 0) { int p_adjust_x = (p - leftover_x) + 1; if((my_rank + 1) >= p_adjust_x) { printf("Adjusted "); start_adjust_x = leftover_x - (p - my_rank); end_adjust_x = leftover_x - (p - (my_rank + 1)); } } int from_x = (my_rank * size_x) + start_adjust_x; int to_x = ((my_rank + 1) * size_x) + end_adjust_x; printf("%d : (%d, 0) --> (%d, y)\n", my_rank, from_x, to_x); } }
long double Log2Expression::getLongDouble(long double op) { errno=0; long double reply ; #ifdef HAVE_LOGL reply = log2l(op); #else reply = static_cast<long double>(log2(static_cast<double>(op))); #endif if( errno && ( errno == EDOM || errno == ERANGE )) { return edom(); } return reply; }
long double en_entropy_ld(enctx * ctx) { long double probs[256], acc = 0; int i; if (!ctx) return 0; if (ctx->len == 0) return 0; for (i = 0; i < 256; i++) probs[i] = ((long double) ctx->state[i]) / ((long double) ctx->len); for (i = 0; i < 256; i++) if (probs[i] != 0) acc += probs[i] * log2l(probs[i]); return -acc; }
int main() { while(1) { char y[5]; fflush(stdin); scanf("%s",y); int n,z; n=(10*(y[0]-'0')+y[1]-'0')*po(10,y[3]-'0'); if(n==0) break; if((n&(n-1))==0) printf("1\n"); else { z=log2l(n); z=po(2,z); printf("%d\n",1+2*(n-z)); } } return 0; }
int bpf_prog2(struct pt_regs *ctx) { long rq = PT_REGS_PARM1(ctx); u64 *value, l, base; u32 index; value = bpf_map_lookup_elem(&my_map, &rq); if (!value) return 0; u64 cur_time = bpf_ktime_get_ns(); u64 delta = cur_time - *value; bpf_map_delete_elem(&my_map, &rq); /* the lines below are computing index = log10(delta)*10 * using integer arithmetic * index = 29 ~ 1 usec * index = 59 ~ 1 msec * index = 89 ~ 1 sec * index = 99 ~ 10sec or more * log10(x)*10 = log2(x)*10/log2(10) = log2(x)*3 */ l = log2l(delta); base = 1ll << l; index = (l * 64 + (delta - base) * 64 / base) * 3 / 64; if (index >= SLOTS) index = SLOTS - 1; value = bpf_map_lookup_elem(&lat_map, &index); if (value) *value += 1; return 0; }
void domathl (void) { #ifndef NO_LONG_DOUBLE long double f1; long double f2; int i1; f1 = acosl(0.0); fprintf( stdout, "acosl : %Lf\n", f1); f1 = acoshl(0.0); fprintf( stdout, "acoshl : %Lf\n", f1); f1 = asinl(1.0); fprintf( stdout, "asinl : %Lf\n", f1); f1 = asinhl(1.0); fprintf( stdout, "asinhl : %Lf\n", f1); f1 = atanl(M_PI_4); fprintf( stdout, "atanl : %Lf\n", f1); f1 = atan2l(2.3, 2.3); fprintf( stdout, "atan2l : %Lf\n", f1); f1 = atanhl(1.0); fprintf( stdout, "atanhl : %Lf\n", f1); f1 = cbrtl(27.0); fprintf( stdout, "cbrtl : %Lf\n", f1); f1 = ceill(3.5); fprintf( stdout, "ceill : %Lf\n", f1); f1 = copysignl(3.5, -2.5); fprintf( stdout, "copysignl : %Lf\n", f1); f1 = cosl(M_PI_2); fprintf( stdout, "cosl : %Lf\n", f1); f1 = coshl(M_PI_2); fprintf( stdout, "coshl : %Lf\n", f1); f1 = erfl(42.0); fprintf( stdout, "erfl : %Lf\n", f1); f1 = erfcl(42.0); fprintf( stdout, "erfcl : %Lf\n", f1); f1 = expl(0.42); fprintf( stdout, "expl : %Lf\n", f1); f1 = exp2l(0.42); fprintf( stdout, "exp2l : %Lf\n", f1); f1 = expm1l(0.00042); fprintf( stdout, "expm1l : %Lf\n", f1); f1 = fabsl(-1.123); fprintf( stdout, "fabsl : %Lf\n", f1); f1 = fdiml(1.123, 2.123); fprintf( stdout, "fdiml : %Lf\n", f1); f1 = floorl(0.5); fprintf( stdout, "floorl : %Lf\n", f1); f1 = floorl(-0.5); fprintf( stdout, "floorl : %Lf\n", f1); f1 = fmal(2.1, 2.2, 3.01); fprintf( stdout, "fmal : %Lf\n", f1); f1 = fmaxl(-0.42, 0.42); fprintf( stdout, "fmaxl : %Lf\n", f1); f1 = fminl(-0.42, 0.42); fprintf( stdout, "fminl : %Lf\n", f1); f1 = fmodl(42.0, 3.0); fprintf( stdout, "fmodl : %Lf\n", f1); /* no type-specific variant */ i1 = fpclassify(1.0); fprintf( stdout, "fpclassify : %d\n", i1); f1 = frexpl(42.0, &i1); fprintf( stdout, "frexpl : %Lf\n", f1); f1 = hypotl(42.0, 42.0); fprintf( stdout, "hypotl : %Lf\n", f1); i1 = ilogbl(42.0); fprintf( stdout, "ilogbl : %d\n", i1); /* no type-specific variant */ i1 = isfinite(3.0); fprintf( stdout, "isfinite : %d\n", i1); /* no type-specific variant */ i1 = isgreater(3.0, 3.1); fprintf( stdout, "isgreater : %d\n", i1); /* no type-specific variant */ i1 = isgreaterequal(3.0, 3.1); fprintf( stdout, "isgreaterequal : %d\n", i1); /* no type-specific variant */ i1 = isinf(3.0); fprintf( stdout, "isinf : %d\n", i1); /* no type-specific variant */ i1 = isless(3.0, 3.1); fprintf( stdout, "isless : %d\n", i1); /* no type-specific variant */ i1 = islessequal(3.0, 3.1); fprintf( stdout, "islessequal : %d\n", i1); /* no type-specific variant */ i1 = islessgreater(3.0, 3.1); fprintf( stdout, "islessgreater : %d\n", i1); /* no type-specific variant */ i1 = isnan(0.0); fprintf( stdout, "isnan : %d\n", i1); /* no type-specific variant */ i1 = isnormal(3.0); fprintf( stdout, "isnormal : %d\n", i1); /* no type-specific variant */ f1 = isunordered(1.0, 2.0); fprintf( stdout, "isunordered : %d\n", i1); f1 = j0l(1.2); fprintf( stdout, "j0l : %Lf\n", f1); f1 = j1l(1.2); fprintf( stdout, "j1l : %Lf\n", f1); f1 = jnl(2,1.2); fprintf( stdout, "jnl : %Lf\n", f1); f1 = ldexpl(1.2,3); fprintf( stdout, "ldexpl : %Lf\n", f1); f1 = lgammal(42.0); fprintf( stdout, "lgammal : %Lf\n", f1); f1 = llrintl(-0.5); fprintf( stdout, "llrintl : %Lf\n", f1); f1 = llrintl(0.5); fprintf( stdout, "llrintl : %Lf\n", f1); f1 = llroundl(-0.5); fprintf( stdout, "lroundl : %Lf\n", f1); f1 = llroundl(0.5); fprintf( stdout, "lroundl : %Lf\n", f1); f1 = logl(42.0); fprintf( stdout, "logl : %Lf\n", f1); f1 = log10l(42.0); fprintf( stdout, "log10l : %Lf\n", f1); f1 = log1pl(42.0); fprintf( stdout, "log1pl : %Lf\n", f1); f1 = log2l(42.0); fprintf( stdout, "log2l : %Lf\n", f1); f1 = logbl(42.0); fprintf( stdout, "logbl : %Lf\n", f1); f1 = lrintl(-0.5); fprintf( stdout, "lrintl : %Lf\n", f1); f1 = lrintl(0.5); fprintf( stdout, "lrintl : %Lf\n", f1); f1 = lroundl(-0.5); fprintf( stdout, "lroundl : %Lf\n", f1); f1 = lroundl(0.5); fprintf( stdout, "lroundl : %Lf\n", f1); f1 = modfl(42.0,&f2); fprintf( stdout, "lmodfl : %Lf\n", f1); f1 = nanl(""); fprintf( stdout, "nanl : %Lf\n", f1); f1 = nearbyintl(1.5); fprintf( stdout, "nearbyintl : %Lf\n", f1); f1 = nextafterl(1.5,2.0); fprintf( stdout, "nextafterl : %Lf\n", f1); f1 = powl(3.01, 2.0); fprintf( stdout, "powl : %Lf\n", f1); f1 = remainderl(3.01,2.0); fprintf( stdout, "remainderl : %Lf\n", f1); f1 = remquol(29.0,3.0,&i1); fprintf( stdout, "remquol : %Lf\n", f1); f1 = rintl(0.5); fprintf( stdout, "rintl : %Lf\n", f1); f1 = rintl(-0.5); fprintf( stdout, "rintl : %Lf\n", f1); f1 = roundl(0.5); fprintf( stdout, "roundl : %Lf\n", f1); f1 = roundl(-0.5); fprintf( stdout, "roundl : %Lf\n", f1); f1 = scalblnl(1.2,3); fprintf( stdout, "scalblnl : %Lf\n", f1); f1 = scalbnl(1.2,3); fprintf( stdout, "scalbnl : %Lf\n", f1); /* no type-specific variant */ i1 = signbit(1.0); fprintf( stdout, "signbit : %i\n", i1); f1 = sinl(M_PI_4); fprintf( stdout, "sinl : %Lf\n", f1); f1 = sinhl(M_PI_4); fprintf( stdout, "sinhl : %Lf\n", f1); f1 = sqrtl(9.0); fprintf( stdout, "sqrtl : %Lf\n", f1); f1 = tanl(M_PI_4); fprintf( stdout, "tanl : %Lf\n", f1); f1 = tanhl(M_PI_4); fprintf( stdout, "tanhl : %Lf\n", f1); f1 = tgammal(2.1); fprintf( stdout, "tgammal : %Lf\n", f1); f1 = truncl(3.5); fprintf( stdout, "truncl : %Lf\n", f1); f1 = y0l(1.2); fprintf( stdout, "y0l : %Lf\n", f1); f1 = y1l(1.2); fprintf( stdout, "y1l : %Lf\n", f1); f1 = ynl(3,1.2); fprintf( stdout, "ynl : %Lf\n", f1); #endif }
static int testl(long double long_double_x, int int_x, long long_x) { int r = 0; r += __finitel(long_double_x); r += __fpclassifyl(long_double_x); r += __isinfl(long_double_x); r += __isnanl(long_double_x); r += __signbitl(long_double_x); r += acoshl(long_double_x); r += acosl(long_double_x); r += asinhl(long_double_x); r += asinl(long_double_x); r += atan2l(long_double_x, long_double_x); r += atanhl(long_double_x); r += atanl(long_double_x); r += cbrtl(long_double_x); r += ceill(long_double_x); r += copysignl(long_double_x, long_double_x); r += coshl(long_double_x); r += cosl(long_double_x); r += erfcl(long_double_x); r += erfl(long_double_x); r += exp2l(long_double_x); r += expl(long_double_x); r += expm1l(long_double_x); r += fabsl(long_double_x); r += fdiml(long_double_x, long_double_x); r += floorl(long_double_x); r += fmal(long_double_x, long_double_x, long_double_x); r += fmaxl(long_double_x, long_double_x); r += fminl(long_double_x, long_double_x); r += fmodl(long_double_x, long_double_x); r += frexpl(long_double_x, &int_x); r += hypotl(long_double_x, long_double_x); r += ilogbl(long_double_x); r += ldexpl(long_double_x, int_x); r += lgammal(long_double_x); r += llrintl(long_double_x); r += llroundl(long_double_x); r += log10l(long_double_x); r += log1pl(long_double_x); r += log2l(long_double_x); r += logbl(long_double_x); r += logl(long_double_x); r += lrintl(long_double_x); r += lroundl(long_double_x); r += modfl(long_double_x, &long_double_x); r += nearbyintl(long_double_x); r += nextafterl(long_double_x, long_double_x); r += nexttowardl(long_double_x, long_double_x); r += powl(long_double_x, long_double_x); r += remainderl(long_double_x, long_double_x); r += remquol(long_double_x, long_double_x, &int_x); r += rintl(long_double_x); r += roundl(long_double_x); r += scalblnl(long_double_x, long_x); r += scalbnl(long_double_x, int_x); r += sinhl(long_double_x); r += sinl(long_double_x); r += sqrtl(long_double_x); r += tanhl(long_double_x); r += tanl(long_double_x); r += tgammal(long_double_x); r += truncl(long_double_x); return r; }
/* Main function. Feed to options to set the cache Options: -h : print out help message -s : set L1 cache Size (KB) -w : set L1 cache ways -l : set L1 cache line size */ int main(int argc, char **argv) { int i; uint32_t size = 32; //total size of L1$ (KB) uint32_t ways = 1; //# of ways in L1. Default to direct-mapped uint32_t line = 32; //line size (B) char repPolicy = 'f'; //replacement policy: f for FIFO, l for LRU // hit and miss counts int totalHits = 0; int totalMisses = 0; char * filename; //strings to compare const char helpString[] = "-h"; const char sizeString[] = "-s"; const char waysString[] = "-w"; const char lineString[] = "-l"; const char traceString[] = "-t"; const char lruString[] = "-lru"; if (argc == 1) { // No arguments passed, show help printHelp(argv[0]); return 1; } //parse command line for(i = 1; i < argc; i++) { //check for help if(!strcmp(helpString, argv[i])) { //print out help text and terminate printHelp(argv[0]); return 1; //return 1 for help termination } //check for size else if(!strcmp(sizeString, argv[i])) { //take next string and convert to int i++; //increment i so that it skips data string in the next loop iteration //check next string's first char. If not digit, fail if(isdigit(argv[i][0])) { size = atoi(argv[i]); } else { printf("Incorrect formatting of size value\n"); return -1; //input failure } } //check for ways else if(!strcmp(waysString, argv[i])) { //take next string and convert to int i++; //increment i so that it skips data string in the next loop iteration //check next string's first char. If not digit, fail // TODO: check if log \in N if(isdigit(argv[i][0])) { ways = atoi(argv[i]); } else { printf("Incorrect formatting of ways value\n"); return -1; //input failure } } //check for line size else if(!strcmp(lineString, argv[i])) { //take next string and convert to int i++; //increment i so that it skips data string in the next loop iteration //check next string's first char. If not digit, fail // TODO: check log \in N if(isdigit(argv[i][0])) { line = atoi(argv[i]); } else { printf("Incorrect formatting of line size value\n"); return -1; //input failure } } else if (!strcmp(traceString, argv[i])) { filename = argv[++i]; } else if (!strcmp(lruString, argv[i])) { repPolicy = 'l'; return -1; } //unrecognized input else{ printf("Unrecognized argument. Exiting.\n"); return -1; } } /* TODO: Probably should intitalize the cache */ unsigned long indexBits, offsetBits, tagBits; // before determining number of bits for things, // determine number of rows in array indexBits = (size*0x400) / (line * ways); uint32_t cache[indexBits][ways]; uint32_t fullAssCache[1][indexBits*ways]; memset(cache, 0, sizeof cache); memset(fullAssCache, 0, sizeof fullAssCache); // initialize valid, dirty bit arrays int valid[indexBits][ways]; int faValid[1][indexBits*ways]; int dirty[indexBits][ways]; memset(valid, 0, sizeof valid); memset(faValid, 0, sizeof faValid); memset(dirty, 0, sizeof dirty); // determine bits breakup of address indexBits = log2l(indexBits); offsetBits = log2l(line); tagBits = 0x20 - (indexBits + offsetBits); // construct queues Queue_t *seenBlocks; Queue_t *replacementQueue; Queue_t *faReplacementQueue; seenBlocks = queueConstructor(); replacementQueue = queueConstructor(); faReplacementQueue = queueConstructor(); indexBits = pow(2, indexBits); printf("Ways: %u; Sets: %u; Line Size: %uB\n", (unsigned int) ways, (unsigned int) indexBits, line); indexBits = log2l(indexBits); printf("Tag: %d bits; Index: %d bits; Offset: %d bits\n", (int) tagBits, (int) indexBits, (int) offsetBits); /* TODO: Now we read the trace file line by line */ /* TODO: Now we simulate the cache */ /* TODO: Now we output the file */ FILE *traceFP; traceFP = fopen(filename, "r"); if (traceFP == NULL) { fprintf(stderr, "Invalid filename: %s\n", filename); return -1; } FILE *simFP; simFP = fopen(strcat(filename,".simulated"), "w"); char requestType = 0; uint32_t requestAddress = 0; char *inString = malloc(sizeof(char)*16); while (fgets(inString, 16, traceFP) != NULL) { requestType = *inString; inString += sizeof(char) * 4; requestAddress = strtol(inString, NULL, 16); inString -= sizeof(char) * 4; uint32_t tag = getAddressTag(requestAddress, tagBits); uint32_t index = getAddressIndex(requestAddress, tagBits, indexBits); fprintf(simFP, "%c 0x%X ", requestType, requestAddress); int seenFlag; seenFlag = queueContainsBlock(seenBlocks, tag, index); if (!seenFlag) { enqueue(seenBlocks, tag, index); fprintf(simFP, "compulsory\n"); } int h, fah; switch (requestType) { case 'l': // Read // Check for hit h = cacheLookup(ways, cache, valid, tagBits, tag, index); fah = cacheLookup((indexBits*ways), fullAssCache, faValid, (tagBits + indexBits), (tag<<(0x20-tagBits)|(index<<(offsetBits))), 0); if (h) { // Hit! totalHits++; fprintf(simFP, "hit\n"); // Refresh requested thing in the queue if (repPolicy == 'l') { queueRemoveBlock(replacementQueue, tag, index); enqueue(replacementQueue, tag, index); if (fah) { queueRemoveBlock(faReplacementQueue, (tag<<(0x20-tagBits)|(index<<(offsetBits))), index); enqueue(faReplacementQueue, (tag<<(0x20-tagBits)|(index<<(offsetBits))), index); } } // return data. // done. break; } else { // Miss! totalMisses++; if (seenFlag) { if (fah) { fprintf(simFP, "conflict\n"); } else { fprintf(simFP, "capacity\n"); } } // Locate cache block to use uint32_t indexToRead, faIndexToRead; indexToRead = locateCacheBlock(ways, cache, valid, tagBits, replacementQueue, tag, index); if (!fah) { faIndexToRead = locateCacheBlock((indexBits * ways), fullAssCache, faValid, (tagBits + indexBits), faReplacementQueue, (tag<<(0x20-tagBits)|(index<<(offsetBits))), 0); } // Add requested thing to the queue enqueue(replacementQueue, tag, index); if (!fah) { enqueue(faReplacementQueue, (tag<<(0x20-tagBits)|(index<<(offsetBits))), 0); } if (dirty[index][indexToRead]) { // Writeback if dirty block write_xactions++; } // Read data from memory into cache block read_xactions++; // Mark cache block as not dirty dirty[index][indexToRead] = 0; // return data. // done. break; } case 's': // Write // Check for hit h = cacheLookup(ways, cache, valid, tagBits, tag, index); fah = cacheLookup((indexBits*ways), fullAssCache, faValid, (tagBits + indexBits), (tag<<(0x20-tagBits)|(index<<(offsetBits))), 0); if (h) { // Hit! totalHits++; fprintf(simFP, "hit\n"); // Refresh requested thing in the queue if (repPolicy == 'l') { queueRemoveBlock(replacementQueue, tag, index); enqueue(replacementQueue, tag, index); if (fah) { queueRemoveBlock(faReplacementQueue, (tag<<(0x20-tagBits)|(index<<(offsetBits))), index); enqueue(faReplacementQueue, (tag<<(0x20-tagBits)|(index<<(offsetBits))), index); } } // Write new data to cache block uint32_t indexToWrite, faIndexToWrite; indexToWrite = locateCacheBlock(ways, cache, valid, tagBits, replacementQueue, tag, index); cache[index][indexToWrite] = requestAddress; // Add requested thing to queue enqueue(replacementQueue, tag, index); if (fah) { faIndexToWrite = locateCacheBlock((indexBits * ways), fullAssCache, faValid, (tagBits + indexBits), faReplacementQueue, (tag<<(0x20-tagBits)|(index<<(offsetBits))), 0); fullAssCache[0][faIndexToWrite] = requestAddress; enqueue(faReplacementQueue, (tag<<(0x20-tagBits)|(index<<(offsetBits))), 0); } // done. break; } else { // Miss! totalMisses++; if (seenFlag) { if (fah) { fprintf(simFP, "conflict\n"); } else { fprintf(simFP, "capacity\n"); } } // Locate cache block to use uint32_t indexToWrite, faIndexToWrite; indexToWrite = locateCacheBlock(ways, cache, valid, tagBits, replacementQueue, tag, index); // Add requested thing to the queue enqueue(replacementQueue, tag, index); if (!fah) { faIndexToWrite = locateCacheBlock((indexBits * ways), fullAssCache, faValid, (tagBits + indexBits), faReplacementQueue, (tag<<(0x20-tagBits)|(index<<(offsetBits))), 0); fullAssCache[0][faIndexToWrite] = requestAddress; enqueue(faReplacementQueue, (tag<<(0x20-tagBits)|(index<<(offsetBits))), 0); } if (dirty[index][indexToWrite]) { // Writeback if dirty block write_xactions++; } // Read data from memory into cache block read_xactions++; // Write new data to cache block cache[index][indexToWrite] = requestAddress; // Mark cache block as dirty dirty[index][indexToWrite] = 1; // done. break; } default: fprintf(stderr, "Error, unexpected accessType: %c\n", requestType); return -1; } fclose(traceFP); fclose(simFP); /* Print results */ printf("Miss Rate: %8lf%%\n", ((double) totalMisses) / ((double) totalMisses + (double) totalHits) * 100.0); printf("Read Transactions: %d\n", read_xactions); printf("Write Transactions: %d\n", write_xactions); /* TODO: Cleanup */ } }
/* ******************************** Multiplicative model for the placement of a file at a given depth taking into account the mean bytes at the depth and the count of files at that depth ********************************/ int fn_depthsize_prob (long double filesize) { double meansizediff[DEPTH_ENTRIES]; double final_prob[DEPTH_ENTRIES]; double totalsize_prob=0, totalprob=0; double sum1=0, sum2=0, sum3=0; int i =0; float token_until_now=0; int token; int factor = 100000; double depthsize_prob[DEPTH_ENTRIES]; srand(deseeder()); if(filesize ==0) return rand()%max_dir_depth+1; for(i=0; i< DEPTH_ENTRIES; i++) { meansizediff[i]=(double) 1/fabsl(log2l(filesize)-(long double)depth_meansize[i]); print_debug(0,"%Lf %Lf %f\n", log2l(filesize), (long double)depth_meansize[i], meansizediff[i]); totalsize_prob+=meansizediff[i]; } for(i=0; i< DEPTH_ENTRIES; i++) { final_prob[i]=(depthcount_prob[i]/Total_depthcount_prob)*\ (meansizediff[i]/totalsize_prob); } for(i=0; i< DEPTH_ENTRIES; i++) { totalprob+=final_prob[i]; } for(i=0; i< DEPTH_ENTRIES; i++) { print_debug(0, "Probsize[%d] %f; Probcount %f; Finalprob %f\n", i+1, \ meansizediff[i]/totalsize_prob*100, depthcount_prob[i]/Total_depthcount_prob*100,\ final_prob[i]/totalprob*100); sum1+= meansizediff[i]/totalsize_prob; sum2+= depthcount_prob[i]/Total_depthcount_prob; sum3+= final_prob[i]/totalprob; } print_debug(0,"sums %f, sumc %f sumt %f\n", sum1, sum2, sum3); i=0; do { token_until_now=0; token = rand() % factor; i=0; token_until_now=final_prob[i]/totalprob*factor; while (token_until_now < token) { print_debug(0,"%f %d\n", token_until_now , token); i++; token_until_now+=final_prob[i]/totalprob*factor; } if(i== DEPTH_ENTRIES-1) { // last bin is actually 20 to infinite ..not just 20 i+= rand()%10; // e.g., any depth between 20 and 30, if DEPTH_ENTRIES=20 } print_debug(0,"Chosen %d, max_dir %d\n", i, max_dir_depth); } while(i > max_dir_depth); return i+1; }
npy_longdouble npy_log2l(npy_longdouble x) { return log2l(x); }
TEST(math, log2l) { ASSERT_FLOAT_EQ(12.0, log2l(4096.0)); }
TEST(math, log2l) { ASSERT_DOUBLE_EQ(12.0L, log2l(4096.0L)); }
main(int argc, char** argv) { int num = 0; int thread_cnt = 0; int* sem_init_vals = malloc(4*sizeof(int)); memset(sem_init_vals,0,4*sizeof(int)); FILE *seminit_file_ptr = fopen("semainit.dat", "r"); fscanf(seminit_file_ptr, "%d", (int *) &sem_init_vals[0]); fscanf(seminit_file_ptr, "%d", (int *) &sem_init_vals[1]); fscanf(seminit_file_ptr, "%d", (int *) &sem_init_vals[2]); fscanf(seminit_file_ptr, "%d", (int *) &sem_init_vals[3]); // printf("seminit vals: %d %d %d.\n",sem_init_vals[1],sem_init_vals[2],sem_init_vals[3]); if((sem_init_vals[1] != 0) || (sem_init_vals[2] != 0) || (sem_init_vals[3] != 1)) { printf("NOTE: Have noticed that the default semaphor initialization values of (0,0,1) are not retained. As a result, correct working of the program is not guarenteed.\n"); } if (argc < 3) { fprintf(stderr, "Please pass input file and observation mode as input.\n"); return; } char* inputfile = argv[1]; char obs_mode = 0; if(strncmp(argv[2],"-o",2) == 0) { obs_mode = 1; } else if(strncmp(argv[2],"-r",2) != 0) { fprintf(stderr, "Observation mode has only two valid options: -r or -o.\n"); return; } size = 0; FILE *file_ptr = fopen(inputfile, "r"); while(fscanf(file_ptr, "%d", &size) > 0) { if(size == 0) { printf("Main: Reached end of input integer lists.\n"); break; } thread_cnt = size/2; logsize = log2l(size); sem_init((sem_t *) &sem1, 0, sem_init_vals[1]); sem_init((sem_t *) &sem2, 0, sem_init_vals[2]); sem_init((sem_t *) &sem3, 0, sem_init_vals[3]); input = malloc(size*sizeof(int)); memset((int *) input, 0, size*sizeof(int)); int i,j,z; for(i = 0; i < size; i++) { num = 0; fscanf(file_ptr, "%d", &num); input[i] = num; } if(obs_mode == 1) { printf("Initial Array State: "); for(z = 0; z < size; z++) { printf("%d,",input[z]); } printf("\n"); } cnt1 = 0; cnt2 = 0; pthread_t threads[thread_cnt]; int args[thread_cnt]; for(i = 0; i < thread_cnt; i++) { args[i] = i; pthread_create(&threads[i], NULL, compare_and_swap, (void *) &args[i]); } for(i = 0; i < logsize*logsize; i++) { while(cnt1 < thread_cnt) {} cnt1 = 0; for(j = 0; j < thread_cnt; j++) { sem_post((sem_t *) &sem1); } while(cnt2 < thread_cnt) {} cnt2 = 0; for(j = 0; j < thread_cnt; j++) { sem_post((sem_t *) &sem2); } if(obs_mode == 1) { printf("In %d stage after %d phase, Array State: ", (i/logsize)+1, (i%logsize)+1); for(z = 0; z < size; z++) { printf("%d,",input[z]); } printf("\n"); } } for(i = 0; i < (size/2); i++) { pthread_join(threads[i], NULL); } printf("Final Array State: "); for(z = 0; z < size; z++) { printf("%d,",input[z]); } printf("\n"); memset((int *) input, 0, size*sizeof(int)); free((int *) input); size = 0; sem_destroy((sem_t *) &sem1); sem_destroy((sem_t *) &sem2); sem_destroy((sem_t *) &sem3); } memset(sem_init_vals,0,4*sizeof(int)); free(sem_init_vals); exit(0); }
double log2(double x) { return log2l(x); }