Beispiel #1
0
void ZopfliBlockSplitLZ77(const ZopfliOptions* options,
                          const ZopfliLZ77Store* lz77, size_t maxblocks,
                          size_t** splitpoints, size_t* npoints) {
  size_t lstart, lend;
  size_t i;
  size_t llpos = 0;
  size_t numblocks = 1;
  unsigned char* done;
  double splitcost, origcost;

  if (lz77->size < 10) return;  /* This code fails on tiny files. */

  done = (unsigned char*)malloc(lz77->size);
  if (!done) exit(-1); /* Allocation failed. */
  for (i = 0; i < lz77->size; i++) done[i] = 0;

  lstart = 0;
  lend = lz77->size;
  for (;;) {
    SplitCostContext c;

    if (maxblocks > 0 && numblocks >= maxblocks) {
      break;
    }

    c.lz77 = lz77;
    c.start = lstart;
    c.end = lend;
    assert(lstart < lend);
    llpos = FindMinimum(SplitCost, &c, lstart + 1, lend, &splitcost);

    assert(llpos > lstart);
    assert(llpos < lend);

    origcost = EstimateCost(lz77, lstart, lend);

    if (splitcost > origcost || llpos == lstart + 1 || llpos == lend) {
      done[lstart] = 1;
    } else {
      AddSorted(llpos, splitpoints, npoints);
      numblocks++;
    }

    if (!FindLargestSplittableBlock(
        lz77->size, done, *splitpoints, *npoints, &lstart, &lend)) {
      break;  /* No further split will probably reduce compression. */
    }

    if (lend - lstart < 10) {
      break;
    }
  }

  if (options->verbose) {
    PrintBlockSplitPoints(lz77, *splitpoints, *npoints);
  }

  free(done);
}
Beispiel #2
0
/*
Gets the cost which is the sum of the cost of the left and the right section
of the data.
type: FindMinimumFun
*/
static double SplitCost(size_t i, void* context) {
  SplitCostContext* c = (SplitCostContext*)context;
  return EstimateCost(c->lz77, c->start, i) + EstimateCost(c->lz77, i, c->end);
}
Beispiel #3
0
/** Given the statements implementing a function, emit the code that
    implements the function.  Most of the work do be done here just
    involves wiring up the function parameter values to be available in the
    function body code.
 */
void
Function::emitCode(FunctionEmitContext *ctx, llvm::Function *function,
                   SourcePos firstStmtPos) {
    // Connect the __mask builtin to the location in memory that stores its
    // value
    maskSymbol->storagePtr = ctx->GetFullMaskPointer();

    // add debugging info for __mask
    maskSymbol->pos = firstStmtPos;
    ctx->EmitVariableDebugInfo(maskSymbol);

#if ISPC_LLVM_VERSION >= ISPC_LLVM_3_7 // LLVM 3.7+
    if (g->NoOmitFramePointer)
        function->addFnAttr("no-frame-pointer-elim", "true");
#endif

#if 0
    llvm::BasicBlock *entryBBlock = ctx->GetCurrentBasicBlock();
#endif
    const FunctionType *type = CastType<FunctionType>(sym->type);
    Assert(type != NULL);
    if (type->isTask == true
#ifdef ISPC_NVPTX_ENABLED
        && (g->target->getISA() != Target::NVPTX) 
#endif 
       ){
        // For tasks, there should always be three parameters: the
        // pointer to the structure that holds all of the arguments, the
        // thread index, and the thread count variables.
        llvm::Function::arg_iterator argIter = function->arg_begin();
#if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
        llvm::Value *structParamPtr = argIter++;
        llvm::Value *threadIndex = argIter++;
        llvm::Value *threadCount = argIter++;
        llvm::Value *taskIndex = argIter++;
        llvm::Value *taskCount = argIter++;
        llvm::Value *taskIndex0 = argIter++;
        llvm::Value *taskIndex1 = argIter++;
        llvm::Value *taskIndex2 = argIter++;
        llvm::Value *taskCount0 = argIter++;
        llvm::Value *taskCount1 = argIter++;
        llvm::Value *taskCount2 = argIter++;
#else /* LLVM 3.8+ */
        llvm::Value *structParamPtr = &*(argIter++);
        llvm::Value *threadIndex = &*(argIter++);
        llvm::Value *threadCount = &*(argIter++);
        llvm::Value *taskIndex = &*(argIter++);
        llvm::Value *taskCount = &*(argIter++);
        llvm::Value *taskIndex0 = &*(argIter++);
        llvm::Value *taskIndex1 = &*(argIter++);
        llvm::Value *taskIndex2 = &*(argIter++);
        llvm::Value *taskCount0 = &*(argIter++);
        llvm::Value *taskCount1 = &*(argIter++);
        llvm::Value *taskCount2 = &*(argIter++);
#endif
        // Copy the function parameter values from the structure into local
        // storage
        for (unsigned int i = 0; i < args.size(); ++i)
            lCopyInTaskParameter(i, structParamPtr, args, ctx);

        if (type->isUnmasked == false) {
            // Copy in the mask as well.
            int nArgs = (int)args.size();
            // The mask is the last parameter in the argument structure
            llvm::Value *ptr = ctx->AddElementOffset(structParamPtr, nArgs, NULL,
                                                     "task_struct_mask");
            llvm::Value *ptrval = ctx->LoadInst(ptr, "mask");
            ctx->SetFunctionMask(ptrval);
        }

        // Copy threadIndex and threadCount into stack-allocated storage so
        // that their symbols point to something reasonable.
        threadIndexSym->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "threadIndex");
        ctx->StoreInst(threadIndex, threadIndexSym->storagePtr);

        threadCountSym->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "threadCount");
        ctx->StoreInst(threadCount, threadCountSym->storagePtr);

        // Copy taskIndex and taskCount into stack-allocated storage so
        // that their symbols point to something reasonable.
        taskIndexSym->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "taskIndex");
        ctx->StoreInst(taskIndex, taskIndexSym->storagePtr);

        taskCountSym->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "taskCount");
        ctx->StoreInst(taskCount, taskCountSym->storagePtr);
        
        taskIndexSym0->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "taskIndex0");
        ctx->StoreInst(taskIndex0, taskIndexSym0->storagePtr);
        taskIndexSym1->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "taskIndex1");
        ctx->StoreInst(taskIndex1, taskIndexSym1->storagePtr);
        taskIndexSym2->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "taskIndex2");
        ctx->StoreInst(taskIndex2, taskIndexSym2->storagePtr);
        
        taskCountSym0->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "taskCount0");
        ctx->StoreInst(taskCount0, taskCountSym0->storagePtr);
        taskCountSym1->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "taskCount1");
        ctx->StoreInst(taskCount1, taskCountSym1->storagePtr);
        taskCountSym2->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "taskCount2");
        ctx->StoreInst(taskCount2, taskCountSym2->storagePtr);
    }
    else {
        // Regular, non-task function
        llvm::Function::arg_iterator argIter = function->arg_begin();
        for (unsigned int i = 0; i < args.size(); ++i, ++argIter) {
            Symbol *sym = args[i];
            if (sym == NULL)
                // anonymous function parameter
                continue;

            argIter->setName(sym->name.c_str());

            // Allocate stack storage for the parameter and emit code
            // to store the its value there.
            sym->storagePtr = ctx->AllocaInst(argIter->getType(), sym->name.c_str());
#if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
            ctx->StoreInst(argIter, sym->storagePtr);
#else /* LLVM 3.8+ */
            ctx->StoreInst(&*argIter, sym->storagePtr);
#endif
            ctx->EmitFunctionParameterDebugInfo(sym, i);
        }

        // If the number of actual function arguments is equal to the
        // number of declared arguments in decl->functionParams, then we
        // don't have a mask parameter, so set it to be all on.  This
        // happens for exmaple with 'export'ed functions that the app
        // calls.
        if (argIter == function->arg_end()) {
            Assert(type->isUnmasked || type->isExported);
            ctx->SetFunctionMask(LLVMMaskAllOn);
        }
        else {
            Assert(type->isUnmasked == false);

            // Otherwise use the mask to set the entry mask value
            argIter->setName("__mask");
            Assert(argIter->getType() == LLVMTypes::MaskType);
#if ISPC_LLVM_VERSION <= ISPC_LLVM_3_7 /* 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 */
            ctx->SetFunctionMask(argIter);
#else /* LLVM 3.8+ */
            ctx->SetFunctionMask(&*argIter);
#endif
            Assert(++argIter == function->arg_end());
        }
#ifdef ISPC_NVPTX_ENABLED
        if (type->isTask == true && g->target->getISA() == Target::NVPTX)
        {
          llvm::NamedMDNode* annotations =
            m->module->getOrInsertNamedMetadata("nvvm.annotations");
#if ISPC_LLVM_VERSION >= ISPC_LLVM_3_6 // LLVM 3.6+
          llvm::SmallVector<llvm::Metadata*, 3> av;
          av.push_back(llvm::ValueAsMetadata::get(function));
          av.push_back(llvm::MDString::get(*g->ctx, "kernel"));
          av.push_back(llvm::ConstantAsMetadata::get(LLVMInt32(1)));
          annotations->addOperand(llvm::MDNode::get(*g->ctx, llvm::ArrayRef<llvm::Metadata*>(av)));
#else
          llvm::SmallVector<llvm::Value*, 3> av;
          av.push_back(function);
          av.push_back(llvm::MDString::get(*g->ctx, "kernel"));
          av.push_back(LLVMInt32(1));
          annotations->addOperand(llvm::MDNode::get(*g->ctx, av));
#endif
        }
#endif /* ISPC_NVPTX_ENABLED */
    }

    // Finally, we can generate code for the function
    if (code != NULL) {
        ctx->SetDebugPos(code->pos);
        ctx->AddInstrumentationPoint("function entry");

        int costEstimate = EstimateCost(code);
        Debug(code->pos, "Estimated cost for function \"%s\" = %d\n",
              sym->name.c_str(), costEstimate);

        // If the body of the function is non-trivial, then we wrap the
        // entire thing inside code that tests to see if the mask is all
        // on, all off, or mixed.  If this is a simple function, then this
        // isn't worth the code bloat / overhead.
        bool checkMask = (type->isTask == true) ||
            (
#if ISPC_LLVM_VERSION == ISPC_LLVM_3_2 // 3.2
              (function->getFnAttributes().hasAttribute(llvm::Attributes::AlwaysInline) == false)
#else // LLVM 3.3+
              (function->getAttributes().getFnAttributes().hasAttribute(llvm::AttributeSet::FunctionIndex, llvm::Attribute::AlwaysInline) == false)
#endif
             &&
             costEstimate > CHECK_MASK_AT_FUNCTION_START_COST);
        checkMask &= (type->isUnmasked == false);
        checkMask &= (g->target->getMaskingIsFree() == false);
        checkMask &= (g->opt.disableCoherentControlFlow == false);

        if (checkMask) {
            llvm::Value *mask = ctx->GetFunctionMask();
            llvm::Value *allOn = ctx->All(mask);
            llvm::BasicBlock *bbAllOn = ctx->CreateBasicBlock("all_on");
            llvm::BasicBlock *bbSomeOn = ctx->CreateBasicBlock("some_on");

            // Set up basic blocks for goto targets
            ctx->InitializeLabelMap(code);

            ctx->BranchInst(bbAllOn, bbSomeOn, allOn);
            // all on: we've determined dynamically that the mask is all
            // on.  Set the current mask to "all on" explicitly so that
            // codegen for this path can be improved with this knowledge in
            // hand...
            ctx->SetCurrentBasicBlock(bbAllOn);
            if (!g->opt.disableMaskAllOnOptimizations)
                ctx->SetFunctionMask(LLVMMaskAllOn);
            code->EmitCode(ctx);
            if (ctx->GetCurrentBasicBlock())
                ctx->ReturnInst();

            // not all on: however, at least one lane must be running,
            // since we should never run with all off...  some on: reset
            // the mask to the value it had at function entry and emit the
            // code.  Resetting the mask here is important, due to the "all
            // on" setting of it for the path above.
            ctx->SetCurrentBasicBlock(bbSomeOn);
            ctx->SetFunctionMask(mask);

            // Set up basic blocks for goto targets again; we want to have
            // one set of them for gotos in the 'all on' case, and a
            // distinct set for the 'mixed mask' case.
            ctx->InitializeLabelMap(code);

            code->EmitCode(ctx);
            if (ctx->GetCurrentBasicBlock())
                ctx->ReturnInst();
        }
        else {
            // Set up basic blocks for goto targets
            ctx->InitializeLabelMap(code);
            // No check, just emit the code
            code->EmitCode(ctx);
        }
    }

    if (ctx->GetCurrentBasicBlock()) {
        // FIXME: We'd like to issue a warning if we've reached the end of
        // the function without a return statement (for non-void
        // functions).  But the test below isn't right, since we can have
        // (with 'x' a varying test) "if (x) return a; else return b;", in
        // which case we have a valid basic block but its unreachable so ok
        // to not have return statement.
#if 0
        // If the bblock has no predecessors, then it doesn't matter if it
        // doesn't have a return; it'll never be reached.  If it does,
        // issue a warning.  Also need to warn if it's the entry block for
        // the function (in which case it will not have predeccesors but is
        // still reachable.)
        if (type->GetReturnType()->IsVoidType() == false &&
            (pred_begin(ec.bblock) != pred_end(ec.bblock) || (ec.bblock == entryBBlock)))
            Warning(sym->pos, "Missing return statement in function returning \"%s\".",
                    type->rType->GetString().c_str());
#endif

        // FIXME: would like to set the context's current position to
        // e.g. the end of the function code

        // if bblock is non-NULL, it hasn't been terminated by e.g. a
        // return instruction.  Need to add a return instruction.
        ctx->ReturnInst();
    }
}
Beispiel #4
0
/** Given the statements implementing a function, emit the code that
    implements the function.  Most of the work do be done here just
    involves wiring up the function parameter values to be available in the
    function body code.
 */
void 
Function::emitCode(FunctionEmitContext *ctx, llvm::Function *function, 
                   SourcePos firstStmtPos) {
    // Connect the __mask builtin to the location in memory that stores its
    // value
    maskSymbol->storagePtr = ctx->GetFullMaskPointer();

    // add debugging info for __mask, programIndex, ...
    maskSymbol->pos = firstStmtPos;
    ctx->EmitVariableDebugInfo(maskSymbol);

#if 0
    llvm::BasicBlock *entryBBlock = ctx->GetCurrentBasicBlock();
#endif
    const FunctionType *type = dynamic_cast<const FunctionType *>(sym->type);
    Assert(type != NULL);
    if (type->isTask == true) {
        // For tasks, we there should always be three parmeters: the
        // pointer to the structure that holds all of the arguments, the
        // thread index, and the thread count variables.
        llvm::Function::arg_iterator argIter = function->arg_begin();
        llvm::Value *structParamPtr = argIter++;
        llvm::Value *threadIndex = argIter++;
        llvm::Value *threadCount = argIter++;
        llvm::Value *taskIndex = argIter++;
        llvm::Value *taskCount = argIter++;

        // Copy the function parameter values from the structure into local
        // storage
        for (unsigned int i = 0; i < args.size(); ++i)
            lCopyInTaskParameter(i, structParamPtr, args, ctx);

        // Copy in the mask as well.
        int nArgs = (int)args.size();
        // The mask is the last parameter in the argument structure
        llvm::Value *ptr = ctx->AddElementOffset(structParamPtr, nArgs, NULL,
                                                  "task_struct_mask");
        llvm::Value *ptrval = ctx->LoadInst(ptr, "mask");
        ctx->SetFunctionMask(ptrval);

        // Copy threadIndex and threadCount into stack-allocated storage so
        // that their symbols point to something reasonable.
        threadIndexSym->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "threadIndex");
        ctx->StoreInst(threadIndex, threadIndexSym->storagePtr);

        threadCountSym->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "threadCount");
        ctx->StoreInst(threadCount, threadCountSym->storagePtr);

        // Copy taskIndex and taskCount into stack-allocated storage so
        // that their symbols point to something reasonable.
        taskIndexSym->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "taskIndex");
        ctx->StoreInst(taskIndex, taskIndexSym->storagePtr);

        taskCountSym->storagePtr = ctx->AllocaInst(LLVMTypes::Int32Type, "taskCount");
        ctx->StoreInst(taskCount, taskCountSym->storagePtr);
    }
    else {
        // Regular, non-task function
        llvm::Function::arg_iterator argIter = function->arg_begin(); 
        for (unsigned int i = 0; i < args.size(); ++i, ++argIter) {
            Symbol *sym = args[i];
            if (sym == NULL)
                // anonymous function parameter
                continue;

            argIter->setName(sym->name.c_str());

            // Allocate stack storage for the parameter and emit code
            // to store the its value there.
            sym->storagePtr = ctx->AllocaInst(argIter->getType(), sym->name.c_str());
            ctx->StoreInst(argIter, sym->storagePtr);
            ctx->EmitFunctionParameterDebugInfo(sym);
        }

        // If the number of actual function arguments is equal to the
        // number of declared arguments in decl->functionParams, then we
        // don't have a mask parameter, so set it to be all on.  This
        // happens for exmaple with 'export'ed functions that the app
        // calls.
        if (argIter == function->arg_end())
            ctx->SetFunctionMask(LLVMMaskAllOn);
        else {
            // Otherwise use the mask to set the entry mask value
            argIter->setName("__mask");
            Assert(argIter->getType() == LLVMTypes::MaskType);
            ctx->SetFunctionMask(argIter);
            Assert(++argIter == function->arg_end());
        }
    }

    // Finally, we can generate code for the function
    if (code != NULL) {
        ctx->SetDebugPos(code->pos);
        ctx->AddInstrumentationPoint("function entry");

        int costEstimate = EstimateCost(code);
        Debug(code->pos, "Estimated cost for function \"%s\" = %d\n", 
              sym->name.c_str(), costEstimate);

        // If the body of the function is non-trivial, then we wrap the
        // entire thing inside code that tests to see if the mask is all
        // on, all off, or mixed.  If this is a simple function, then this
        // isn't worth the code bloat / overhead.
        bool checkMask = (type->isTask == true) || 
            ((function->hasFnAttr(llvm::Attribute::AlwaysInline) == false) &&
             costEstimate > CHECK_MASK_AT_FUNCTION_START_COST);
        checkMask &= (g->target.maskingIsFree == false);
        checkMask &= (g->opt.disableCoherentControlFlow == false);

        if (checkMask) {
            llvm::Value *mask = ctx->GetFunctionMask();
            llvm::Value *allOn = ctx->All(mask);
            llvm::BasicBlock *bbAllOn = ctx->CreateBasicBlock("all_on");
            llvm::BasicBlock *bbNotAll = ctx->CreateBasicBlock("not_all_on");

            // Set up basic blocks for goto targets
            ctx->InitializeLabelMap(code);

            ctx->BranchInst(bbAllOn, bbNotAll, allOn);
            // all on: we've determined dynamically that the mask is all
            // on.  Set the current mask to "all on" explicitly so that
            // codegen for this path can be improved with this knowledge in
            // hand...
            ctx->SetCurrentBasicBlock(bbAllOn);
            if (!g->opt.disableMaskAllOnOptimizations)
                ctx->SetFunctionMask(LLVMMaskAllOn);
            code->EmitCode(ctx);
            if (ctx->GetCurrentBasicBlock())
                ctx->ReturnInst();

            // not all on: figure out if no instances are running, or if
            // some of them are
            ctx->SetCurrentBasicBlock(bbNotAll);
            ctx->SetFunctionMask(mask);
            llvm::BasicBlock *bbNoneOn = ctx->CreateBasicBlock("none_on");
            llvm::BasicBlock *bbSomeOn = ctx->CreateBasicBlock("some_on");
            llvm::Value *anyOn = ctx->Any(mask);
            ctx->BranchInst(bbSomeOn, bbNoneOn, anyOn);
            
            // Everyone is off; get out of here.
            ctx->SetCurrentBasicBlock(bbNoneOn);
            ctx->ReturnInst();

            // some on: reset the mask to the value it had at function
            // entry and emit the code.  Resetting the mask here is
            // important, due to the "all on" setting of it for the path
            // above
            ctx->SetCurrentBasicBlock(bbSomeOn);
            ctx->SetFunctionMask(mask);

            // Set up basic blocks for goto targets again; we want to have
            // one set of them for gotos in the 'all on' case, and a
            // distinct set for the 'mixed mask' case.
            ctx->InitializeLabelMap(code);

            code->EmitCode(ctx);
            if (ctx->GetCurrentBasicBlock())
                ctx->ReturnInst();
        }
        else {
            // Set up basic blocks for goto targets
            ctx->InitializeLabelMap(code);
            // No check, just emit the code
            code->EmitCode(ctx);
        }
    }

    if (ctx->GetCurrentBasicBlock()) {
        // FIXME: We'd like to issue a warning if we've reached the end of
        // the function without a return statement (for non-void
        // functions).  But the test below isn't right, since we can have
        // (with 'x' a varying test) "if (x) return a; else return b;", in
        // which case we have a valid basic block but its unreachable so ok
        // to not have return statement.
#if 0
        // If the bblock has no predecessors, then it doesn't matter if it
        // doesn't have a return; it'll never be reached.  If it does,
        // issue a warning.  Also need to warn if it's the entry block for
        // the function (in which case it will not have predeccesors but is
        // still reachable.)
        if (Type::Equal(type->GetReturnType(), AtomicType::Void) == false &&
            (pred_begin(ec.bblock) != pred_end(ec.bblock) || (ec.bblock == entryBBlock)))
            Warning(sym->pos, "Missing return statement in function returning \"%s\".",
                    type->rType->GetString().c_str());
#endif

        // FIXME: would like to set the context's current position to
        // e.g. the end of the function code

        // if bblock is non-NULL, it hasn't been terminated by e.g. a
        // return instruction.  Need to add a return instruction.
        ctx->ReturnInst();
    }
}
Beispiel #5
0
void ZopfliBlockSplitLZ77(const ZopfliOptions* options,
                          const unsigned short* litlens,
                          const unsigned short* dists,
                          size_t llsize, size_t maxblocks,
                          size_t** splitpoints, size_t* npoints, size_t startnpoints) {
  size_t lstart, lend;
  size_t i;
  size_t llpos = 0;
  size_t numblocks = 1;
  unsigned char* done;
  double splitcost, origcost;

  if (llsize < 10) return;  /* This code fails on tiny files. */

  done = (unsigned char*)malloc(llsize);
  if (!done) exit(-1); /* Allocation failed. */
  for (i = 0; i < llsize; i++) done[i] = 0;

  lstart = 0;
  lend = llsize;
  for (;;) {
    SplitCostContext c;

    if (maxblocks > 0 && numblocks >= maxblocks) {
      break;
    }

    c.litlens = litlens;
    c.dists = dists;
    c.llsize = llsize;
    c.start = lstart;
    c.end = lend;
    c.ohh = options->optimizehuffmanheader;
    assert(lstart < lend);
    llpos = FindMinimum(SplitCost, &c, lstart + 1, lend,options);

    assert(llpos > lstart);
    assert(llpos < lend);

    splitcost = EstimateCost(litlens, dists, lstart, llpos, c.ohh) +
        EstimateCost(litlens, dists, llpos, lend, c.ohh);
    origcost = EstimateCost(litlens, dists, lstart, lend, c.ohh);

    if (splitcost > origcost || llpos == lstart + 1 || llpos == lend) {
      done[lstart] = 1;
    } else {
      AddSorted(llpos, splitpoints, npoints);
      ++numblocks;
      if(options->verbose>0 && options->verbose<5) fprintf(stderr,"Initializing blocks: %lu    \r",(unsigned long)(startnpoints+numblocks));
    }

    if (!FindLargestSplittableBlock(
        llsize, done, *splitpoints, *npoints, &lstart, &lend)) {
      break;  /* No further split will probably reduce compression. */
    }

    if (lend - lstart < 10) {
      break;
    }
  }

  if (options->verbose>3) {
    PrintBlockSplitPoints(litlens, dists, llsize, *splitpoints, *npoints);
  }

  if(options->verbose>2) {
    fprintf(stderr, "Total blocks: %lu                 \n\n",(unsigned long)numblocks);
  }


  free(done);
}
Beispiel #6
0
/*
Gets the cost which is the sum of the cost of the left and the right section
of the data.
type: FindMinimumFun
*/
static double SplitCost(size_t i, void* context) {
  SplitCostContext* c = (SplitCostContext*)context;
  return EstimateCost(c->litlens, c->dists, c->start, i, c->ohh) +
      EstimateCost(c->litlens, c->dists, i, c->end, c->ohh);
}
bool CASW_Campaign_Save::BuildCampaignRoute(int iStart, int iEnd)
{
	if (!ASWGameRules() || !ASWGameRules()->GetCampaignInfo())
		return false;
		
	CASW_Campaign_Info *pCI = ASWGameRules()->GetCampaignInfo();
	if (iStart < 0 || iStart >= pCI->GetNumMissions())
		return false;
	if (iEnd < 0 || iEnd >= pCI->GetNumMissions())
		return false;	

	// do A*
	m_ClosedList.Purge();
	m_OpenList.Purge();
	m_iRouteDest = iEnd;
	m_iRouteStart = iStart;

	// add starting mission to the open list
	campaign_route_node_t start_node;
	start_node.iMission = iStart;
	start_node.iParentMission = iStart;	
	start_node.g = 0;
	start_node.h = EstimateCost(pCI, iStart, iEnd);
	start_node.f = start_node.g + start_node.h;
	m_OpenList.AddToTail(start_node);
	int iOverflow = 0;
	while (iOverflow < 1000)
	{
		// find the lowest node on the open list
		int iLowestF = -1;
		float fLowestF_Value = 99999;
		//Msg("%d nodes in open list\n", m_OpenList.Count());
		for (int i=0;i<m_OpenList.Count();i++)
		{
			if (m_OpenList[i].f < fLowestF_Value)
			{
				//Msg(" and node %d is lower than lowest\n", i);
				fLowestF_Value = m_OpenList[i].f;
				iLowestF = i;
			}
		}
		// if open list is empty, that means we've searched all routes and still didn't arrive at the dest
		if (iLowestF == -1)
		{
			//Msg("Failed to find route\n");
			return false;
		}
		// move it to the closed list
		int iCurrentMission = m_OpenList[iLowestF].iMission;
		float fCurrentCost = m_OpenList[iLowestF].f;
		m_ClosedList.AddToTail(m_OpenList[iLowestF]);
		m_OpenList.Remove(iLowestF);

		if (iCurrentMission == iEnd)		// found the target
		{
			//Msg("current mission is the destination!\n");
			return true;
		}
		//Msg("Current mission is %d, open list size %d closed list size %d\n", iCurrentMission, m_OpenList.Count(), m_ClosedList.Count());
		// check all linked missions
		CASW_Campaign_Info::CASW_Campaign_Mission_t *pMission = pCI->GetMission(iCurrentMission);
		if (!pMission)
		{
			//Msg("Failed to get current mission in BuildCampaignRoute\n");
			return false;
		}
		//Msg("Current mission (%d) has %d links\n", iCurrentMission, pMission->m_Links.Count());
		for (int i=0;i<pMission->m_Links.Count();i++)
		{
			int iOtherMission = pMission->m_Links[i];
			//Msg("other mission[%d] is %d\n", i, iOtherMission);
			CASW_Campaign_Info::CASW_Campaign_Mission_t *pOtherMission = pCI->GetMission(pMission->m_Links[i]);
			if (!pOtherMission)
				continue;

			// check if it's on the open list
			int iOnOpenList = -1;
			for (int k=0;k<m_OpenList.Count();k++)
			{
				if (m_OpenList[k].iMission == iOtherMission)
				{
					//Msg("Other mission (%d) is on the open list\n", iOtherMission);
					iOnOpenList = k;
					break;
				}
			}
			// if not, check if it's already on the closed list
			if (iOnOpenList == -1)
			{
				int iOnClosedList = -1;
				for (int k=0;k<m_ClosedList.Count();k++)
				{
					if (m_ClosedList[k].iMission == iOtherMission)
					{
						//Msg("Other mission (%d) is on the closed list already, so ignoring\n", iOtherMission);
						iOnClosedList = k;
						break;
					}
				}
				if (iOnClosedList != -1)
				{
					continue;
				}
			}
			// if not, add it and calculate costs
			if (iOnOpenList == -1)
			{
				//Msg("other mission %d not on the open list, so adding it...", iOtherMission);
				campaign_route_node_t new_node;
				new_node.iMission = iOtherMission;
				new_node.iParentMission = iCurrentMission;				
				new_node.g = fCurrentCost + EstimateCost(pCI, iCurrentMission, iOtherMission);	// actual cost from start to here
				new_node.h = EstimateCost(pCI, iOtherMission, iEnd);
				new_node.f = new_node.g + new_node.h;
				m_OpenList.AddToTail(new_node);				
			}
			else		// if it is, check if going there from us is cheaper, if so update parent and costs
			{
				//Msg("other mission %d already on open list, checking costs\n", iOtherMission);
				float my_g_cost = fCurrentCost + EstimateCost(pCI, iCurrentMission, iOtherMission);
				if (my_g_cost < m_OpenList[iOnOpenList].g)
				{
					m_OpenList[iOnOpenList].iParentMission = iCurrentMission;
					m_OpenList[iOnOpenList].g = my_g_cost;
					m_OpenList[iOnOpenList].f = m_OpenList[iOnOpenList].g + m_OpenList[iOnOpenList].h;
				}
			}
		}
		iOverflow++;
	}
	//Msg("Error, BuildCampaignRoute overflow!\n");
	return false;
}