move math functions out of common.h, and into include/avian/util/math.h

This commit is contained in:
Joshua Warner 2013-02-20 07:51:57 -07:00
parent b9e281612b
commit 52b2fd74ef
10 changed files with 84 additions and 62 deletions

53
include/avian/util/math.h Normal file
View File

@ -0,0 +1,53 @@
/* Copyright (c) 2008-2012, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
#ifndef AVIAN_UTIL_MATH_H
#define AVIAN_UTIL_MATH_H
namespace avian {
namespace util {
inline unsigned max(unsigned a, unsigned b) {
return (a > b ? a : b);
}
inline unsigned min(unsigned a, unsigned b) {
return (a < b ? a : b);
}
inline unsigned avg(unsigned a, unsigned b) {
return (a + b) / 2;
}
inline unsigned ceilingDivide(unsigned n, unsigned d) {
return (n + d - 1) / d;
}
inline bool powerOfTwo(unsigned n) {
for (; n > 2; n >>= 1) if (n & 1) return false;
return true;
}
inline unsigned nextPowerOfTwo(unsigned n) {
unsigned r = 1;
while (r < n) r <<= 1;
return r;
}
inline unsigned log(unsigned n) {
unsigned r = 0;
for (unsigned i = 1; i < n; ++r) i <<= 1;
return r;
}
} // namespace util
} // namespace avian
#endif // AVIAN_UTIL_MATH_H

View File

@ -14,6 +14,8 @@
#include "system.h"
#include "target.h"
#include <avian/util/math.h>
namespace vm {
class Vector {
@ -51,8 +53,8 @@ class Vector {
if (position + space > capacity) {
assert(s, minimumCapacity >= 0);
unsigned newCapacity = max
(position + space, max(minimumCapacity, capacity * 2));
unsigned newCapacity = avian::util::max
(position + space, avian::util::max(minimumCapacity, capacity * 2));
uint8_t* newData = static_cast<uint8_t*>
(allocator->allocate(newCapacity));
if (data) {

View File

@ -9,13 +9,13 @@ const unsigned NAME(BootHeapOffset) = 1 << (NAME(BootShift) + 1);
inline unsigned
LABEL(codeMapSize)(unsigned codeSize)
{
return ceilingDivide(codeSize, TargetBitsPerWord) * TargetBytesPerWord;
return avian::util::ceilingDivide(codeSize, TargetBitsPerWord) * TargetBytesPerWord;
}
inline unsigned
LABEL(heapMapSize)(unsigned heapSize)
{
return ceilingDivide(heapSize, TargetBitsPerWord * TargetBytesPerWord)
return avian::util::ceilingDivide(heapSize, TargetBitsPerWord * TargetBytesPerWord)
* TargetBytesPerWord;
}

View File

@ -15,6 +15,8 @@
#include "target.h"
#include "machine.h"
#include <avian/util/math.h>
namespace vm {
class BootImage {

View File

@ -10,6 +10,7 @@
#include "target.h"
#include <avian/util/runtime-array.h>
#include <avian/util/math.h>
#include "codegen/compiler/context.h"
#include "codegen/compiler/event.h"
@ -20,6 +21,8 @@
#include "codegen/compiler/frame.h"
#include "codegen/compiler/ir.h"
using namespace avian::util;
namespace avian {
namespace codegen {
namespace compiler {
@ -921,11 +924,11 @@ appendCombine(Context* c, lir::TernaryOperation type,
intptr_t handler = c->client->getThunk
(type, firstSize, resultSize, &threadParameter);
unsigned stackSize = vm::ceilingDivide(secondSize, vm::TargetBytesPerWord)
+ vm::ceilingDivide(firstSize, vm::TargetBytesPerWord);
unsigned stackSize = ceilingDivide(secondSize, vm::TargetBytesPerWord)
+ ceilingDivide(firstSize, vm::TargetBytesPerWord);
compiler::push(c, vm::ceilingDivide(secondSize, vm::TargetBytesPerWord), second);
compiler::push(c, vm::ceilingDivide(firstSize, vm::TargetBytesPerWord), first);
compiler::push(c, ceilingDivide(secondSize, vm::TargetBytesPerWord), second);
compiler::push(c, ceilingDivide(firstSize, vm::TargetBytesPerWord), first);
if (threadParameter) {
++ stackSize;
@ -1047,7 +1050,7 @@ appendTranslate(Context* c, lir::BinaryOperation type, unsigned firstSize,
if (thunk) {
Stack* oldStack = c->stack;
compiler::push(c, vm::ceilingDivide(firstSize, vm::TargetBytesPerWord), first);
compiler::push(c, ceilingDivide(firstSize, vm::TargetBytesPerWord), first);
Stack* argumentStack = c->stack;
c->stack = oldStack;
@ -1057,7 +1060,7 @@ appendTranslate(Context* c, lir::BinaryOperation type, unsigned firstSize,
(c, lir::ValueGeneral, constantSite
(c, c->client->getThunk(type, firstSize, resultSize))),
0, 0, result, resultSize, argumentStack,
vm::ceilingDivide(firstSize, vm::TargetBytesPerWord), 0);
ceilingDivide(firstSize, vm::TargetBytesPerWord), 0);
} else {
append(c, new(c->zone)
TranslateEvent
@ -1404,8 +1407,8 @@ appendBranch(Context* c, lir::TernaryOperation type, unsigned size, Value* first
assert(c, not threadParameter);
compiler::push(c, vm::ceilingDivide(size, vm::TargetBytesPerWord), second);
compiler::push(c, vm::ceilingDivide(size, vm::TargetBytesPerWord), first);
compiler::push(c, ceilingDivide(size, vm::TargetBytesPerWord), second);
compiler::push(c, ceilingDivide(size, vm::TargetBytesPerWord), first);
Stack* argumentStack = c->stack;
c->stack = oldStack;
@ -1414,7 +1417,7 @@ appendBranch(Context* c, lir::TernaryOperation type, unsigned size, Value* first
appendCall
(c, value
(c, lir::ValueGeneral, constantSite(c, handler)), 0, 0, result, 4,
argumentStack, vm::ceilingDivide(size, vm::TargetBytesPerWord) * 2, 0);
argumentStack, ceilingDivide(size, vm::TargetBytesPerWord) * 2, 0);
appendBranch(c, thunkBranch(c, type), 4, value
(c, lir::ValueGeneral, constantSite(c, static_cast<int64_t>(0))),

View File

@ -296,24 +296,6 @@ const uintptr_t PointerMask
const unsigned LikelyPageSizeInBytes = 4 * 1024;
inline unsigned
max(unsigned a, unsigned b)
{
return (a > b ? a : b);
}
inline unsigned
min(unsigned a, unsigned b)
{
return (a < b ? a : b);
}
inline unsigned
avg(unsigned a, unsigned b)
{
return (a + b) / 2;
}
inline unsigned
pad(unsigned n, unsigned alignment)
{
@ -338,35 +320,6 @@ padWord(uintptr_t n)
return padWord(n, BytesPerWord);
}
inline unsigned
ceilingDivide(unsigned n, unsigned d)
{
return (n + d - 1) / d;
}
inline bool
powerOfTwo(unsigned n)
{
for (; n > 2; n >>= 1) if (n & 1) return false;
return true;
}
inline unsigned
nextPowerOfTwo(unsigned n)
{
unsigned r = 1;
while (r < n) r <<= 1;
return r;
}
inline unsigned
log(unsigned n)
{
unsigned r = 0;
for (unsigned i = 1; i < n; ++r) i <<= 1;
return r;
}
template <class T>
inline unsigned
wordOf(unsigned i)

View File

@ -13,6 +13,8 @@
#include "common.h"
#include "arch.h"
#include <avian/util/math.h>
using namespace vm;
using namespace avian::util;

View File

@ -18,6 +18,7 @@
#include "lzma.h"
#include <avian/util/runtime-array.h>
#include <avian/util/math.h>
#if defined(PLATFORM_WINDOWS)
# define WIN32_LEAN_AND_MEAN
@ -25,6 +26,7 @@
#endif
using namespace vm;
using namespace avian::util;
namespace {

View File

@ -51,10 +51,13 @@
#include "arch.h"
#include "system.h"
#include <avian/util/math.h>
#define ACQUIRE(x) MutexResource MAKE_NAME(mutexResource_) (x)
using namespace vm;
using namespace avian::util;
namespace {

View File

@ -14,6 +14,8 @@
#include "system.h"
#include "allocator.h"
#include <avian/util/math.h>
namespace vm {
class Zone: public Allocator {
@ -59,8 +61,8 @@ class Zone: public Allocator {
bool tryEnsure(unsigned space) {
if (segment == 0 or segment->position + space > segment->size) {
unsigned size = padToPage
(max
(space, max
(avian::util::max
(space, avian::util::max
(minimumFootprint, segment == 0 ? 0 : segment->size * 2))
+ sizeof(Segment));