← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~zorba-coders/zorba/bug-867059 into lp:zorba

 

Paul J. Lucas has proposed merging lp:~zorba-coders/zorba/bug-867059 into lp:zorba.

Requested reviews:
  Matthias Brantner (matthias-brantner)
  Markos Zaharioudakis (markos-za)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/bug-867059/+merge/80848

Better implementation of ZORBA_WITH_BIG_INTEGER=OFF: now limited to 63 bits (for a 64-bit implementation of a long long).  Also added build_options.dox.
-- 
https://code.launchpad.net/~zorba-coders/zorba/bug-867059/+merge/80848
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'doc/zorba/build.dox'
--- doc/zorba/build.dox	2011-09-23 10:54:01 +0000
+++ doc/zorba/build.dox	2011-10-31 21:41:43 +0000
@@ -116,7 +116,8 @@
   To change the build mode (to Debug, Release, RelWithDebInfo or MinSizeRel),
   you can pass an additional parameter to CMake, e.g.,
   <tt>cmake -D CMAKE_BUILD_TYPE=Debug [ZORBA]</tt>.
-- Zorba has other options as well.
+- Zorba has other build options as well
+  (see \ref build_options).
   You can tweak the performance and library footprint
   by enabling or disabling various features from Zorba.
 

=== added file 'doc/zorba/build_options.dox'
--- doc/zorba/build_options.dox	1970-01-01 00:00:00 +0000
+++ doc/zorba/build_options.dox	2011-10-31 21:41:43 +0000
@@ -0,0 +1,35 @@
+/** \page build_options Zorba Build Options
+
+\section ZORBA_WITH_BIG_INTEGER Big Integer Support (ZORBA_WITH_BIG_INTEGER)
+
+The Zorba XQuery processor
+by default
+has arbitrary precision
+for the \c xs:integer type.
+Compared to a C++ \c int,
+however,
+arbitrary precision integers
+are orders of magnitude slower.
+If arbitrary precision integers are not needed,
+they can be disabled
+by configuring Zorba
+with the
+\c ZORBA_WITH_BIG_INTEGER
+option set to \c OFF.
+
+When arbitrary precision integers are disabled,
+Zorba uses a C++ <code>long long</code>
+for \c xs:integer,
+the precision for which
+is dependent on your C++ implementation.
+Typically, however,
+the precision is at least 64 bits.
+However,
+in order to maintain the condition
+that the value space for \c xs:nonNegativeInteger
+is a subset of that of \c xs:integer,
+one less bit is allowed
+for \c xs:nonNegativeInteger.
+
+*/
+/* vim:set et sw=2 ts=2: */

=== modified file 'src/api/itemfactoryimpl.cpp'
--- src/api/itemfactoryimpl.cpp	2011-07-22 07:23:17 +0000
+++ src/api/itemfactoryimpl.cpp	2011-10-31 21:41:43 +0000
@@ -269,7 +269,7 @@
 ItemFactoryImpl::createInteger(long long aInteger)
 {
   store::Item_t lItem;
-  Integer const lInteger(aInteger);
+  xs_integer const lInteger(aInteger);
   theItemFactory->createInteger(lItem, lInteger);
   return &*lItem;
 }
@@ -281,7 +281,7 @@
   zstring const &lString = Unmarshaller::getInternalString( aInteger );
   store::Item_t lItem;
   try {
-    Integer const lInteger( lString.c_str() );
+    xs_integer const lInteger( lString.c_str() );
     theItemFactory->createInteger(lItem, lInteger);
   }
   catch ( std::exception const& ) {
@@ -447,7 +447,7 @@
 {
   store::Item_t lItem;
   if (aValue < 0) {
-    Integer const lInteger(aValue);
+    xs_integer const lInteger(aValue);
     theItemFactory->createNegativeInteger(lItem, lInteger);
   }
   return &*lItem;
@@ -457,7 +457,7 @@
 Item ItemFactoryImpl::createNonNegativeInteger ( unsigned long long aValue )
 {
   store::Item_t lItem;
-  Integer lInteger(aValue);
+  xs_nonNegativeInteger lInteger(aValue);
   theItemFactory->createNonNegativeInteger(lItem, lInteger);
   return &*lItem;
 }
@@ -467,7 +467,7 @@
 {
   store::Item_t lItem;
   if (aValue < 0) {
-    Integer const lInteger(aValue);
+    xs_integer const lInteger(aValue);
     theItemFactory->createNonPositiveInteger(lItem, lInteger);
   }
   return &*lItem;
@@ -477,7 +477,7 @@
 Item ItemFactoryImpl::createPositiveInteger ( unsigned long long aValue )
 {
   store::Item_t lItem;
-  Integer lInteger(aValue);
+  xs_nonNegativeInteger lInteger(aValue);
   theItemFactory->createPositiveInteger(lItem, lInteger);
   return &*lItem;
 }

=== modified file 'src/common/shared_types.h'
--- src/common/shared_types.h	2011-09-12 22:42:28 +0000
+++ src/common/shared_types.h	2011-10-31 21:41:43 +0000
@@ -140,12 +140,18 @@
 typedef rchandle<GMonth> GMonth_t;
 
 /* numerics */
-template <class Object> class FloatImpl;
-class Integer;
-
-/* numerics */
+template<typename FloatType> class FloatImpl;
 typedef FloatImpl<double> Double;
 typedef FloatImpl<float>  Float;
+#ifdef ZORBA_WITH_BIG_INTEGER
+class IntegerImpl;
+typedef IntegerImpl Integer;
+typedef IntegerImpl UInteger;
+#else
+template<typename IntType> class IntegerImpl;
+typedef IntegerImpl<long long> Integer;
+typedef IntegerImpl<unsigned long long> UInteger;
+#endif /* ZORBA_WITH_BIG_INTEGER */
 
 /* api */
 class serializer;

=== modified file 'src/compiler/rewriter/rules/flwor_rules.cpp'
--- src/compiler/rewriter/rules/flwor_rules.cpp	2011-09-02 20:43:46 +0000
+++ src/compiler/rewriter/rules/flwor_rules.cpp	2011-10-31 21:41:43 +0000
@@ -950,7 +950,7 @@
   (a)  op is eq or =, and
   (b1) posExpr is an integer literal with value >= 1, or
   (b2) the flwor expr has no sequential clauses and posExpr is an expression 
-       whose type is xs:Integer? and which does not reference the for var 
+       whose type is xs:integer? and which does not reference the for var 
        associated with posVar nor any other vars that are defined after that 
        for var.
 

=== modified file 'src/compiler/rewriter/rules/fold_rules.cpp'
--- src/compiler/rewriter/rules/fold_rules.cpp	2011-09-27 10:43:46 +0000
+++ src/compiler/rewriter/rules/fold_rules.cpp	2011-10-31 21:41:43 +0000
@@ -755,7 +755,7 @@
       {
         return new const_expr(fo->get_sctx(),
                               fo->get_loc(),
-                              Integer(type_cnt));
+                              xs_integer(type_cnt));
       }
       else if (fkind == FunctionConsts::FN_EMPTY_1)
       {

=== modified file 'src/compiler/translator/translator.cpp'
--- src/compiler/translator/translator.cpp	2011-10-21 22:53:06 +0000
+++ src/compiler/translator/translator.cpp	2011-10-31 21:41:43 +0000
@@ -9674,8 +9674,8 @@
     {
       case FunctionConsts::FN_HEAD_1:
       {
-        arguments.push_back(new const_expr(theRootSctx, loc, Integer(1)));
-        arguments.push_back(new const_expr(theRootSctx, loc, Integer(1)));
+        arguments.push_back(new const_expr(theRootSctx, loc, xs_integer(1)));
+        arguments.push_back(new const_expr(theRootSctx, loc, xs_integer(1)));
         function* f = GET_BUILTIN_FUNCTION(OP_ZORBA_SUBSEQUENCE_INT_3);
         fo_expr_t foExpr = new fo_expr(theRootSctx, loc, f, arguments);
         normalize_fo(foExpr);
@@ -9684,7 +9684,7 @@
       }
       case FunctionConsts::FN_TAIL_1:
       {
-        arguments.push_back(new const_expr(theRootSctx, loc, Integer(2)));
+        arguments.push_back(new const_expr(theRootSctx, loc, xs_integer(2)));
         function* f = GET_BUILTIN_FUNCTION(OP_ZORBA_SUBSEQUENCE_INT_2);
         fo_expr_t foExpr = new fo_expr(theRootSctx, loc, f, arguments);
         normalize_fo(foExpr);

=== modified file 'src/runtime/booleans/BooleanImpl.cpp'
--- src/runtime/booleans/BooleanImpl.cpp	2011-08-08 17:46:14 +0000
+++ src/runtime/booleans/BooleanImpl.cpp	2011-10-31 21:41:43 +0000
@@ -852,7 +852,7 @@
   {
     // There are 2 cases when two types are comparable without one being a
     // subtype of the other: (a) they belong to different branches under of
-    // the type-inheritance subtree rooted at xs:Integer, (b) they belong to
+    // the type-inheritance subtree rooted at xs:integer, (b) they belong to
     // different branches under of the type-inheritance subtree rooted at
     // xs::duration (i.e. one is xs:yearMonthDuration and the other is
     // xs:dayTimeDuration).
@@ -949,7 +949,7 @@
     {
       // There is 1 case when two types are order-comparable without one being a
       // subtype of the other: they belong to different branches under of the
-      // type-inheritance subtree rooted at xs:Integer.
+      // type-inheritance subtree rooted at xs:integer.
       if (TypeOps::is_subtype(tm, *type0, *GENV_TYPESYSTEM.INTEGER_TYPE_ONE) &&
           TypeOps::is_subtype(tm, *type1, *GENV_TYPESYSTEM.INTEGER_TYPE_ONE))
       {

=== modified file 'src/runtime/core/flwor_iterator.cpp'
--- src/runtime/core/flwor_iterator.cpp	2011-10-22 15:44:45 +0000
+++ src/runtime/core/flwor_iterator.cpp	2011-10-31 21:41:43 +0000
@@ -1215,7 +1215,7 @@
     if (!flc.thePosVarRefs.empty())
     {
       store::Item_t posItem;
-      GENV_ITEMFACTORY->createInteger(posItem, Integer(bindingState));
+      GENV_ITEMFACTORY->createInteger(posItem, xs_integer(bindingState));
 
       std::vector<PlanIter_t>::const_iterator viter = flc.thePosVarRefs.begin();
       std::vector<PlanIter_t>::const_iterator end = flc.thePosVarRefs.end();

=== modified file 'src/runtime/core/gflwor/count_iterator.cpp'
--- src/runtime/core/gflwor/count_iterator.cpp	2011-06-14 17:26:33 +0000
+++ src/runtime/core/gflwor/count_iterator.cpp	2011-10-31 21:41:43 +0000
@@ -91,7 +91,7 @@
   {
     {
       store::Item_t lCountItem;
-      GENV_ITEMFACTORY->createInteger(lCountItem, Integer(lState->incCount()));
+      GENV_ITEMFACTORY->createInteger(lCountItem, xs_integer(lState->incCount()));
       bindVariables(lCountItem, theCountVars, aPlanState);
     }
     STACK_PUSH(true, lState);

=== modified file 'src/runtime/core/gflwor/for_iterator.cpp'
--- src/runtime/core/gflwor/for_iterator.cpp	2011-06-22 14:42:38 +0000
+++ src/runtime/core/gflwor/for_iterator.cpp	2011-10-31 21:41:43 +0000
@@ -126,7 +126,7 @@
       {
         store::Item_t lPosItem;
         GENV_ITEMFACTORY->createInteger(lPosItem,
-                                        Integer(lState->incReturnPosition()));
+                                        xs_integer(lState->incReturnPosition()));
         bindVariables(lPosItem, thePosVarRefs, aPlanState);
       }
       STACK_PUSH(true, lState);

=== modified file 'src/runtime/numerics/NumericsImpl.cpp'
--- src/runtime/numerics/NumericsImpl.cpp	2011-07-10 14:04:53 +0000
+++ src/runtime/numerics/NumericsImpl.cpp	2011-10-31 21:41:43 +0000
@@ -441,10 +441,11 @@
   xs_integer ll0 = i0->getIntegerValue();
   xs_integer ll1 = i1->getIntegerValue();
 
-  if ( ll1 == Integer::zero() )
+  if ( ll1.sign() == 0 )
   {
     throw XQUERY_EXCEPTION( err::FOAR0001, ERROR_LOC( loc ) );
   }
+
   return GENV_ITEMFACTORY->createInteger (result, ll0 / ll1);
 }
 

=== modified file 'src/store/api/item.h'
--- src/store/api/item.h	2011-10-05 17:49:48 +0000
+++ src/store/api/item.h	2011-10-31 21:41:43 +0000
@@ -292,7 +292,7 @@
 
   /** Accessor for xs:nonNegativeInteager, xs:positiveInteger
    */
-  virtual xs_uinteger
+  virtual xs_nonNegativeInteger
   getUnsignedIntegerValue() const;
 
   /** Accessor for xs:long

=== modified file 'src/store/api/item_factory.h'
--- src/store/api/item_factory.h	2011-07-22 07:23:17 +0000
+++ src/store/api/item_factory.h	2011-10-31 21:41:43 +0000
@@ -236,13 +236,13 @@
    * Specification: [http://www.w3.org/TR/xmlschema-2/#nonNegativeInteger]
    * @param value
    */
-  virtual bool createNonNegativeInteger(Item_t& result, const xs_uinteger& value) = 0;
+  virtual bool createNonNegativeInteger(Item_t& result, const xs_nonNegativeInteger& value) = 0;
 
   /**
    * Specification: [http://www.w3.org/TR/xmlschema-2/#positiveInteger]
    * @param value
    */
-  virtual bool createPositiveInteger(Item_t& result, const xs_uinteger& value) = 0;
+  virtual bool createPositiveInteger(Item_t& result, const xs_positiveInteger& value) = 0;
 
   /**
    * Specification: [http://www.w3.org/TR/xmlschema-2/#nonPositiveInteger]

=== modified file 'src/store/naive/atomic_items.cpp'
--- src/store/naive/atomic_items.cpp	2011-10-22 15:44:45 +0000
+++ src/store/naive/atomic_items.cpp	2011-10-31 21:41:43 +0000
@@ -145,7 +145,7 @@
     const IntegerItem* item = static_cast<const IntegerItem*>(item1);
     try
     {
-      longValue = to_xs_long(item->theValue);
+      longValue = item->getLongValue();
       GET_FACTORY().createLong(result, longValue);
     }
     catch (std::range_error const&)
@@ -215,11 +215,11 @@
   {
     const IntegerItem* item = static_cast<const IntegerItem*>(item1);
 
-    doubleValue = item->theValue;
+    doubleValue = item->getIntegerValue();
 
     const xs_integer intValue(doubleValue);
 
-    lossy = (intValue != item->theValue);
+    lossy = (intValue != item->getIntegerValue());
     break;
   }
 
@@ -1681,10 +1681,11 @@
 
 
 /*******************************************************************************
-  class IntegerItem
+  class IntegerItemImpl
 ********************************************************************************/
 
-long IntegerItem::compare( Item const *other, long, const XQPCollator* ) const {
+long IntegerItemImpl::compare( Item const *other, long,
+                               const XQPCollator* ) const {
   try
   {
     return theValue.compare( other->getIntegerValue() );
@@ -1695,8 +1696,8 @@
   }
 }
 
-bool IntegerItem::equals( const store::Item* other, long,
-                          const XQPCollator*) const
+bool IntegerItemImpl::equals( const store::Item* other, long,
+                              const XQPCollator*) const
 {
   try
   {
@@ -1708,13 +1709,13 @@
   }
 }
 
-xs_decimal IntegerItem::getDecimalValue() const
+xs_decimal IntegerItemImpl::getDecimalValue() const
 {
   return xs_decimal(theValue);
 }
 
 
-xs_long IntegerItem::getLongValue() const
+xs_long IntegerItemImpl::getLongValue() const
 {
   try
   {
@@ -1730,44 +1731,45 @@
 }
 
 
-store::Item* IntegerItem::getType() const
+store::Item* IntegerItemImpl::getType() const
 {
   return GET_STORE().theSchemaTypeNames[XS_INTEGER];
 }
 
 
-store::Item_t IntegerItem::getEBV() const
+store::Item_t IntegerItemImpl::getEBV() const
 {
-  bool b = ( theValue != xs_integer::zero() );
+  bool b = !!theValue.sign();
   store::Item_t bVal;
   CREATE_BOOLITEM(bVal, b);
   return bVal;
 }
 
 
-zstring IntegerItem::getStringValue() const
+zstring IntegerItemImpl::getStringValue() const
 {
   return theValue.toString();
 }
 
 
-void IntegerItem::getStringValue2(zstring& val) const
+void IntegerItemImpl::getStringValue2(zstring& val) const
 {
   val = theValue.toString();
 }
 
-uint32_t IntegerItem::hash(long, const XQPCollator*) const
+uint32_t IntegerItemImpl::hash(long, const XQPCollator*) const
 {
   return theValue.hash();
 }
 
-void IntegerItem::appendStringValue(zstring& buf) const
+
+void IntegerItemImpl::appendStringValue(zstring& buf) const
 {
   buf += theValue.toString();
 }
 
 
-zstring IntegerItem::show() const
+zstring IntegerItemImpl::show() const
 {
   zstring res("xs:integer(");
   appendStringValue(res);
@@ -1779,11 +1781,91 @@
 /*******************************************************************************
   class NonPositiveIntegerItem
 ********************************************************************************/
+long NonPositiveIntegerItem::compare( Item const *other, long,
+                                      const XQPCollator* ) const {
+  try
+  {
+    return theValue.compare( other->getIntegerValue() );
+  }
+  catch ( ZorbaException const& )
+  {
+    return getDecimalValue().compare( other->getDecimalValue() );
+  }
+}
+
+bool NonPositiveIntegerItem::equals( const store::Item* other, long,
+                                     const XQPCollator* ) const
+{
+  try
+  {
+    return theValue == other->getIntegerValue();
+  }
+  catch (ZorbaException const&)
+  {
+    return getDecimalValue() == other->getDecimalValue();
+  }
+}
+
 store::Item* NonPositiveIntegerItem::getType() const
 {
   return GET_STORE().theSchemaTypeNames[XS_NON_POSITIVE_INTEGER];
 }
 
+xs_decimal NonPositiveIntegerItem::getDecimalValue() const
+{
+  return xs_decimal(theValue);
+}
+
+xs_integer NonPositiveIntegerItem::getIntegerValue() const
+{
+  return xs_integer(theValue);
+}
+
+xs_long NonPositiveIntegerItem::getLongValue() const
+{
+  try
+  {
+    return to_xs_long(theValue);
+  }
+  catch ( std::range_error const& )
+  {
+    throw XQUERY_EXCEPTION(
+      err::FORG0001,
+      ERROR_PARAMS( theValue, ZED( CastFromToFailed_34 ), "integer", "long" )
+    );
+  }
+}
+
+zstring NonPositiveIntegerItem::getStringValue() const
+{
+  return theValue.toString();
+}
+
+
+void NonPositiveIntegerItem::getStringValue2(zstring& val) const
+{
+  val = theValue.toString();
+}
+
+uint32_t NonPositiveIntegerItem::hash(long, const XQPCollator*) const
+{
+  return theValue.hash();
+}
+
+
+void NonPositiveIntegerItem::appendStringValue(zstring& buf) const
+{
+  buf += theValue.toString();
+}
+
+store::Item_t NonPositiveIntegerItem::getEBV() const
+{
+  bool b = !!theValue.sign();
+  store::Item_t bVal;
+  CREATE_BOOLITEM(bVal, b);
+  return bVal;
+}
+
 zstring NonPositiveIntegerItem::show() const
 {
   zstring res("xs:nonPositiveInteger(");
@@ -1812,14 +1894,94 @@
 
 
 /*******************************************************************************
-  class NonNegativeINtegerItem
+  class NonNegativeIntegerItem
 ********************************************************************************/
+long NonNegativeIntegerItem::compare( Item const *other, long,
+                                      const XQPCollator* ) const {
+  try
+  {
+    return theValue.compare( other->getUnsignedIntegerValue() );
+  }
+  catch ( ZorbaException const& )
+  {
+    return getDecimalValue().compare( other->getDecimalValue() );
+  }
+}
+
+bool NonNegativeIntegerItem::equals( const store::Item* other, long,
+                                     const XQPCollator* ) const
+{
+  try
+  {
+    return theValue == other->getUnsignedIntegerValue();
+  }
+  catch (ZorbaException const&)
+  {
+    return getDecimalValue() == other->getDecimalValue();
+  }
+}
+
 store::Item* NonNegativeIntegerItem::getType() const
 {
   return GET_STORE().theSchemaTypeNames[XS_NON_NEGATIVE_INTEGER];
 }
 
 
+xs_decimal NonNegativeIntegerItem::getDecimalValue() const
+{
+  return xs_decimal(theValue);
+}
+
+xs_integer NonNegativeIntegerItem::getIntegerValue() const
+{
+  return xs_integer(theValue);
+}
+
+xs_long NonNegativeIntegerItem::getLongValue() const
+{
+  try
+  {
+    return to_xs_long(theValue);
+  }
+  catch ( std::range_error const& )
+  {
+    throw XQUERY_EXCEPTION(
+      err::FORG0001,
+      ERROR_PARAMS( theValue, ZED( CastFromToFailed_34 ), "integer", "long" )
+    );
+  }
+}
+
+zstring NonNegativeIntegerItem::getStringValue() const
+{
+  return theValue.toString();
+}
+
+
+void NonNegativeIntegerItem::getStringValue2(zstring& val) const
+{
+  val = theValue.toString();
+}
+
+uint32_t NonNegativeIntegerItem::hash(long, const XQPCollator*) const
+{
+  return theValue.hash();
+}
+
+
+void NonNegativeIntegerItem::appendStringValue(zstring& buf) const
+{
+  buf += theValue.toString();
+}
+
+store::Item_t NonNegativeIntegerItem::getEBV() const
+{
+  bool b = !!theValue.sign();
+  store::Item_t bVal;
+  CREATE_BOOLITEM(bVal, b);
+  return bVal;
+}
+
 zstring NonNegativeIntegerItem::show() const
 {
   zstring res("xs:nonNegativeInteger(");
@@ -1861,6 +2023,9 @@
   return xs_integer(theValue);
 }
 
+xs_nonNegativeInteger LongItem::getUnsignedIntegerValue() const {
+  return theValue >= 0 ? theValue : -theValue;
+}
 
 store::Item* LongItem::getType() const
 {
@@ -2105,9 +2270,9 @@
 }
 
 
-xs_uinteger UnsignedLongItem::getUnsignedIntegerValue() const
+xs_nonNegativeInteger UnsignedLongItem::getUnsignedIntegerValue() const
 {
-  return xs_uinteger(theValue);
+  return xs_nonNegativeInteger(theValue);
 }
 
 
@@ -2172,9 +2337,9 @@
 }
 
 
-xs_uinteger UnsignedIntItem::getUnsignedIntegerValue() const
+xs_nonNegativeInteger UnsignedIntItem::getUnsignedIntegerValue() const
 {
-  return Integer(theValue);
+  return xs_nonNegativeInteger(theValue);
 }
 
 
@@ -2239,9 +2404,9 @@
 }
 
 
-xs_uinteger UnsignedShortItem::getUnsignedIntegerValue() const
+xs_nonNegativeInteger UnsignedShortItem::getUnsignedIntegerValue() const
 {
-  return Integer(theValue);
+  return xs_nonNegativeInteger(theValue);
 }
 
 
@@ -2302,13 +2467,13 @@
 
 xs_integer UnsignedByteItem::getIntegerValue() const
 {
-  return Integer((uint32_t)theValue);
+  return xs_integer((uint32_t)theValue);
 }
 
 
-xs_uinteger UnsignedByteItem::getUnsignedIntegerValue() const
+xs_nonNegativeInteger UnsignedByteItem::getUnsignedIntegerValue() const
 {
-  return Integer(theValue);
+  return xs_nonNegativeInteger(theValue);
 }
 
 

=== modified file 'src/store/naive/atomic_items.h'
--- src/store/naive/atomic_items.h	2011-10-22 15:44:45 +0000
+++ src/store/naive/atomic_items.h	2011-10-31 21:41:43 +0000
@@ -167,7 +167,7 @@
 
   xs_integer getIntegerValue() const { return theBaseItem->getIntegerValue(); }
 
-  xs_uinteger getUnsignedIntegerValue() const { return theBaseItem->getUnsignedIntegerValue(); }
+  xs_nonNegativeInteger getUnsignedIntegerValue() const { return theBaseItem->getUnsignedIntegerValue(); }
 
   xs_long getLongValue() const { return theBaseItem->getLongValue(); }
 
@@ -1150,6 +1150,31 @@
 ********************************************************************************/
 class IntegerItem : public AtomicItem
 {
+protected:
+  IntegerItem() {}
+
+public:
+  virtual xs_decimal getDecimalValue() const = 0;
+  virtual xs_integer getIntegerValue() const = 0;
+  virtual xs_long getLongValue() const = 0;
+
+  virtual store::Item_t getEBV() const = 0;
+  virtual store::Item* getType() const = 0;
+
+  virtual zstring getStringValue() const = 0;
+  virtual void getStringValue2(zstring&) const = 0;
+  virtual void appendStringValue(zstring& buf) const = 0;
+
+  uint32_t hash(long = 0, const XQPCollator* aCollation = 0) const = 0;
+  bool isNaN() const { return false; }
+};
+
+
+/*******************************************************************************
+  class IntegerItemImpl
+********************************************************************************/
+class IntegerItemImpl : public IntegerItem
+{
   friend class BasicItemFactory;
   friend class AtomicItem;
 
@@ -1157,44 +1182,35 @@
   xs_integer theValue;
 
 protected:
-  IntegerItem(const xs_integer& aValue) : theValue ( aValue ) {}
+  IntegerItemImpl(const xs_integer& aValue) : theValue ( aValue ) {}
 
-  IntegerItem() {}
+  IntegerItemImpl() {}
 
 public:
   xs_decimal getDecimalValue() const;
-
   xs_integer getIntegerValue() const { return theValue; }
-
-  xs_long getLongValue() const;
-
+  xs_long getLongValue() const; 
+  xs_nonNegativeInteger getUnsignedIntegerValue() const { return theValue; }
+
+  zstring getStringValue() const;
+  void getStringValue2(zstring&) const;
+  void appendStringValue(zstring&) const;
+
+  store::Item_t getEBV() const;
   virtual SchemaTypeCode getTypeCode() const { return XS_INTEGER; }
-
   virtual store::Item* getType() const;
-
   uint32_t hash(long = 0, const XQPCollator* aCollation = 0) const;
+  virtual zstring show() const;
+
+  long compare(
+        const Item* other,
+        long timezone = 0,
+        const XQPCollator* aCollation = 0) const;
 
   bool equals(
         const store::Item* other,
         long timezone = 0,
         const XQPCollator* aCollation = 0) const;
-
-  long compare(
-        const Item* other,
-        long timezone = 0,
-        const XQPCollator* aCollation = 0) const;
-
-  store::Item_t getEBV( ) const;
-
-  zstring getStringValue() const;
-
-  void getStringValue2(zstring& val) const;
-
-  void appendStringValue(zstring& buf) const;
-
-  bool isNaN() const { return false; }
-
-  virtual zstring show() const;
 };
 
 
@@ -1206,28 +1222,50 @@
   friend class BasicItemFactory;
 
 protected:
-  NonPositiveIntegerItem(const xs_integer& aValue) : IntegerItem(aValue) {}
+  xs_nonPositiveInteger theValue;
+
+  NonPositiveIntegerItem(const xs_integer& aValue) : theValue(aValue) {}
 
   NonPositiveIntegerItem() {}
 
 public:
+  // inherited
+  xs_decimal getDecimalValue() const;
+  xs_integer getIntegerValue() const;
+  xs_long getLongValue() const;
+  xs_nonNegativeInteger getUnsignedIntegerValue() const { return theValue; }
+
+  zstring getStringValue() const;
+  void getStringValue2(zstring& val) const;
+  void appendStringValue(zstring&) const;
+
+  store::Item_t getEBV() const;
+  store::Item* getType() const;
   SchemaTypeCode getTypeCode() const { return XS_NON_POSITIVE_INTEGER; }
-
-  store::Item* getType() const;
-
+  uint32_t hash(long = 0, const XQPCollator* aCollation = 0) const;
   zstring show() const;
+
+  long compare(
+        const Item* other,
+        long timezone = 0,
+        const XQPCollator* aCollation = 0) const;
+
+  bool equals(
+        const store::Item* other,
+        long timezone = 0,
+        const XQPCollator* aCollation = 0) const;
 };
 
 
 /*******************************************************************************
   class NegativeIntegerItem
 ********************************************************************************/
-class NegativeIntegerItem : public IntegerItem
+class NegativeIntegerItem : public NonPositiveIntegerItem
 {
   friend class BasicItemFactory;
 
 protected:
-  NegativeIntegerItem(const xs_integer& aValue) : IntegerItem(aValue) {}
+  NegativeIntegerItem(const xs_integer& aValue) : NonPositiveIntegerItem(aValue) {}
 
   NegativeIntegerItem() {}
 
@@ -1242,44 +1280,60 @@
 
 /*******************************************************************************
   class NonNegativeIntegerItem
-
-  Note: xs_uinteger is typedef of Integer
 ********************************************************************************/
 class NonNegativeIntegerItem : public IntegerItem
 {
   friend class BasicItemFactory;
 
 protected:
-  NonNegativeIntegerItem(const xs_uinteger& aValue) : IntegerItem(aValue) {}
+  xs_nonNegativeInteger theValue;
+
+  NonNegativeIntegerItem(const xs_nonNegativeInteger& aValue) : theValue(aValue) {}
 
   NonNegativeIntegerItem() {}
 
 public:
-  xs_uinteger getUnsignedIntegerValue() const { return theValue; }
+  // inherited
+  xs_decimal getDecimalValue() const;
+  xs_integer getIntegerValue() const;
+  xs_long getLongValue() const;
+  xs_nonNegativeInteger getUnsignedIntegerValue() const { return theValue; }
 
+  store::Item_t getEBV() const;
+  store::Item* getType() const;
   SchemaTypeCode getTypeCode() const { return XS_NON_NEGATIVE_INTEGER; }
-
-  store::Item* getType() const;
-
+  uint32_t hash(long = 0, const XQPCollator* aCollation = 0) const;
   zstring show() const;
+
+  zstring getStringValue() const;
+  void getStringValue2(zstring& val) const;
+  void appendStringValue(zstring&) const;
+
+  long compare(
+        const Item* other,
+        long timezone = 0,
+        const XQPCollator* aCollation = 0) const;
+
+  bool equals(
+        const store::Item* other,
+        long timezone = 0,
+        const XQPCollator* aCollation = 0) const;
 };
 
 
 /*******************************************************************************
   class PositiveIntegerItem
 ********************************************************************************/
-class PositiveIntegerItem : public  IntegerItem
+class PositiveIntegerItem : public  NonNegativeIntegerItem
 {
   friend class BasicItemFactory;
 
 protected:
-  PositiveIntegerItem(const xs_uinteger& aValue) : IntegerItem(aValue) { }
+  PositiveIntegerItem(const xs_positiveInteger& aValue) : NonNegativeIntegerItem(aValue) { }
 
   PositiveIntegerItem() {}
 
 public:
-  xs_uinteger getUnsignedIntegerValue() const { return theValue; }
-
   SchemaTypeCode getTypeCode() const { return XS_POSITIVE_INTEGER; }
 
   store::Item* getType() const;
@@ -1311,6 +1365,8 @@
 
   xs_long getLongValue() const { return theValue; }
 
+  xs_nonNegativeInteger getUnsignedIntegerValue() const;
+
   SchemaTypeCode getTypeCode() const { return XS_LONG; }
 
   store::Item* getType() const;
@@ -1635,7 +1691,7 @@
 
   xs_integer getIntegerValue() const;
 
-  xs_uinteger getUnsignedIntegerValue() const;
+  xs_nonNegativeInteger getUnsignedIntegerValue() const;
 
   xs_unsignedLong getUnsignedLongValue() const { return theValue; }
 
@@ -1715,7 +1771,7 @@
 
   xs_integer getIntegerValue() const;
 
-  xs_uinteger getUnsignedIntegerValue() const;
+  xs_nonNegativeInteger getUnsignedIntegerValue() const;
 
   xs_long getLongValue() const { return static_cast<xs_long>(theValue); }
 
@@ -1804,7 +1860,7 @@
 
   xs_integer getIntegerValue() const;
 
-  xs_uinteger getUnsignedIntegerValue() const;
+  xs_nonNegativeInteger getUnsignedIntegerValue() const;
 
   xs_long getLongValue() const { return static_cast<xs_long>(theValue); }
 
@@ -1897,7 +1953,7 @@
 
   xs_integer getIntegerValue() const;
 
-  xs_uinteger getUnsignedIntegerValue() const;
+  xs_nonNegativeInteger getUnsignedIntegerValue() const;
 
   xs_long getLongValue() const { return static_cast<xs_long>(theValue); }
 

=== modified file 'src/store/naive/item.cpp'
--- src/store/naive/item.cpp	2011-09-27 20:45:01 +0000
+++ src/store/naive/item.cpp	2011-10-31 21:41:43 +0000
@@ -508,7 +508,7 @@
  * Accessor for xs:unsignedLong,  xs:unsignedInt,  xs:unsignedShort,
  *  xs:unsignedByte, xs:nonNegativeInteager, xs:positiveInteger
  */
-xs_uinteger Item::getUnsignedIntegerValue() const
+xs_nonNegativeInteger Item::getUnsignedIntegerValue() const
 {
   throw ZORBA_EXCEPTION(
     zerr::ZSTR0040_TYPE_ERROR,

=== modified file 'src/store/naive/simple_item_factory.cpp'
--- src/store/naive/simple_item_factory.cpp	2011-10-12 07:17:05 +0000
+++ src/store/naive/simple_item_factory.cpp	2011-10-31 21:41:43 +0000
@@ -284,7 +284,7 @@
 
 bool BasicItemFactory::createInteger(store::Item_t& result, const xs_integer& value)
 {
-  result = new IntegerItem( value );
+  result = new IntegerItemImpl( value );
   return true;
 }
 
@@ -293,7 +293,7 @@
     store::Item_t& result,
     const xs_integer& value)
 {
-  ZORBA_ASSERT(value <= Integer::zero());
+  ZORBA_ASSERT(value.sign() <= 0);
   result = new NonPositiveIntegerItem( value );
   return true;
 }
@@ -303,7 +303,7 @@
     store::Item_t& result,
     const xs_integer& value)
 {
-  ZORBA_ASSERT(value < xs_integer::zero());
+  ZORBA_ASSERT(value.sign() < 0);
   result = new NegativeIntegerItem(value);
   return true;
 }
@@ -311,7 +311,7 @@
 
 bool BasicItemFactory::createNonNegativeInteger(
     store::Item_t& result,
-    const xs_uinteger& value )
+    const xs_nonNegativeInteger& value )
 {
   result = new NonNegativeIntegerItem( value );
   return true;
@@ -321,9 +321,9 @@
 
 bool BasicItemFactory::createPositiveInteger(
     store::Item_t& result,
-    const xs_uinteger& value)
+    const xs_positiveInteger& value)
 {
-  ZORBA_ASSERT(value > Integer::zero());
+  ZORBA_ASSERT(value.sign() > 0);
   result = new PositiveIntegerItem( value );
   return true;
 }

=== modified file 'src/store/naive/simple_item_factory.h'
--- src/store/naive/simple_item_factory.h	2011-07-22 07:23:17 +0000
+++ src/store/naive/simple_item_factory.h	2011-10-31 21:41:43 +0000
@@ -101,9 +101,9 @@
 
   bool createInteger(store::Item_t& result, const xs_integer& value);
 
-  bool createNonNegativeInteger(store::Item_t& result, const xs_uinteger& value);
+  bool createNonNegativeInteger(store::Item_t& result, const xs_nonNegativeInteger& value);
 
-  bool createPositiveInteger(store::Item_t& result,  const xs_uinteger& value );
+  bool createPositiveInteger(store::Item_t& result,  const xs_positiveInteger& value );
 
   bool createNonPositiveInteger(store::Item_t& result, const xs_integer& value);
 

=== modified file 'src/types/casting.cpp'
--- src/types/casting.cpp	2011-10-22 15:44:45 +0000
+++ src/types/casting.cpp	2011-10-31 21:41:43 +0000
@@ -119,6 +119,7 @@
 SAME_S_AND_T(aURI)
 SAME_S_AND_T(QN)
 SAME_S_AND_T(NOT)
+SAME_S_AND_T(uint)
 
 #undef SAME_S_AND_T
 
@@ -1263,6 +1264,148 @@
   return uA_str(result, aItem, strval, aFactory, nsCtx, aErrorInfo);
 }
 
+T1_TO_T2(uint, uA)
+{
+  zstring strval2;
+  aItem->getStringValue2(strval2);
+  return str_uA(result, aItem, strval2, aFactory, nsCtx, aErrorInfo);
+}
+
+
+T1_TO_T2(uint, str)
+{
+  return uA_str(result, aItem, strval, aFactory, nsCtx, aErrorInfo);
+}
+
+
+T1_TO_T2(uint, flt)
+{
+  return aFactory->createFloat(
+    result, xs_float(aItem->getUnsignedIntegerValue())
+  );
+}
+
+
+T1_TO_T2(uint, dbl)
+{
+  return aFactory->createDouble(
+    result, xs_double(aItem->getUnsignedIntegerValue())
+  );
+}
+
+
+T1_TO_T2(uint, dec)
+{
+  return aFactory->createDecimal(
+    result, xs_decimal(aItem->getUnsignedIntegerValue())
+  );
+}
+
+
+T1_TO_T2(uint, bool)
+{
+  result = aItem->getEBV();
+  return true;
+}
+
+
+T1_TO_T2(uA, uint)
+{
+  zstring strval2;
+  aItem->getStringValue2(strval2);
+  return str_int(result, aItem, strval2, aFactory, nsCtx, aErrorInfo);
+}
+
+
+T1_TO_T2(flt, uint)
+{
+  try 
+  {
+    xs_nonNegativeInteger const n(aItem->getFloatValue());
+    return aFactory->createNonNegativeInteger(result, n);
+  }
+  catch ( std::exception const& ) 
+  {
+    throw TYPE_EXCEPTION( err::FOCA0002, aErrorInfo );
+  }
+}
+
+
+T1_TO_T2(int, uint)
+{
+  try 
+  {
+    xs_nonNegativeInteger const n(aItem->getIntegerValue());
+    return aFactory->createNonNegativeInteger(result, n);
+  }
+  catch ( std::exception const& ) 
+  {
+    throw TYPE_EXCEPTION( err::FOCA0002, aErrorInfo );
+  }
+}
+
+
+T1_TO_T2(uint, int)
+{
+  try 
+  {
+    xs_integer const n(aItem->getIntegerValue());
+    return aFactory->createInteger(result, n);
+  }
+  catch ( std::exception const& ) 
+  {
+    throw TYPE_EXCEPTION( err::FOCA0002, aErrorInfo );
+  }
+}
+
+
+T1_TO_T2(dbl, uint)
+{
+  try 
+  {
+    xs_nonNegativeInteger const n(aItem->getDoubleValue());
+    return aFactory->createInteger(result, n);
+  }
+  catch ( std::exception const& ) 
+  {
+    throw TYPE_EXCEPTION( err::FOCA0002, aErrorInfo );
+  }
+}
+
+
+T1_TO_T2(dec, uint)
+{
+  xs_nonNegativeInteger const n(aItem->getDecimalValue());
+  return aFactory->createNonNegativeInteger(result, n);
+}
+
+
+T1_TO_T2(bool, uint)
+{
+  if (aItem->getBooleanValue())
+    return aFactory->createNonNegativeInteger(
+      result, xs_nonNegativeInteger::one()
+    );
+  else
+    return aFactory->createNonNegativeInteger(
+      result, xs_nonNegativeInteger::zero()
+    );
+}
+
+
+T1_TO_T2(str, uint)
+{
+  try {
+    xs_nonNegativeInteger const n(strval.c_str());
+    return aFactory->createNonNegativeInteger(result, n);
+  }
+  catch ( std::invalid_argument const& ) {
+    throw TYPE_EXCEPTION( err::FORG0001, aErrorInfo );
+  }
+  catch ( std::range_error const& ) {
+    throw TYPE_EXCEPTION( err::FOAR0002, aErrorInfo );
+  }
+}
 
 /*******************************************************************************
 
@@ -1371,14 +1514,14 @@
   case TypeConstants::XS_NON_POSITIVE_INTEGER:
   {
     xs_integer const lInteger = aItem->getIntegerValue();
-    if (lInteger <= xs_integer::zero())
+    if (lInteger.sign() <= 0)
       return aFactory->createNonPositiveInteger(result, lInteger);
     break;
   }
   case TypeConstants::XS_NEGATIVE_INTEGER:
   {
     xs_integer const lInteger = aItem->getIntegerValue();
-    if (lInteger < xs_integer::zero())
+    if (lInteger.sign() < 0)
       return aFactory->createNegativeInteger(result, lInteger);
     break;
   }
@@ -1436,9 +1579,15 @@
   }
   case TypeConstants::XS_NON_NEGATIVE_INTEGER:
   {
-    xs_integer const lInteger = aItem->getIntegerValue();
-    if (lInteger >= xs_integer::zero())
-      return aFactory->createNonNegativeInteger(result, lInteger);
+    xs_decimal const d = aItem->getDecimalValue();
+    if (d.sign() >= 0)
+      try {
+        xs_nonNegativeInteger const i(d);
+        return aFactory->createNonNegativeInteger(result, i);
+      }
+      catch ( std::exception const& ) {
+        // ignore
+      }
     break;
   }
   case TypeConstants::XS_UNSIGNED_LONG:
@@ -1495,9 +1644,9 @@
   }
   case TypeConstants::XS_POSITIVE_INTEGER:
   {
-    xs_integer lInteger = aItem->getIntegerValue();
-    if (lInteger > xs_integer::zero())
-      return aFactory->createPositiveInteger(result, lInteger);
+    xs_positiveInteger const i = aItem->getUnsignedIntegerValue();
+    if (i.sign() > 0)
+      return aFactory->createPositiveInteger(result, i);
     break;
   }
   default:
@@ -1541,12 +1690,12 @@
    5,  // 25 XS_INT
    5,  // 26 XS_SHORT
    5,  // 27 XS_BYTE
-   5,  // 28 XS_NON_NEGATIVE_INTEGER
-   5,  // 29 XS_UNSIGNED_LONG
-   5,  // 30 XS_UNSIGNED_INT
-   5,  // 31 XS_UNSIGNED_SHORT
-   5,  // 32 XS_UNSIGNED_BYTE
-   5,  // 33 XS_POSITIVE_INTEGER
+  23,  // 28 XS_NON_NEGATIVE_INTEGER
+  23,  // 29 XS_UNSIGNED_LONG
+  23,  // 30 XS_UNSIGNED_INT
+  23,  // 31 XS_UNSIGNED_SHORT
+  23,  // 32 XS_UNSIGNED_BYTE
+  23,  // 33 XS_POSITIVE_INTEGER
   12,  // 34 XS_GYEAR_MONTH
   13,  // 35 XS_GYEAR
   14,  // 36 XS_GMONTH_DAY
@@ -1564,98 +1713,155 @@
 /*******************************************************************************
 
 ********************************************************************************/
-const GenericCast::DownCastFunc GenericCast::theDownCastMatrix[23] =
+const GenericCast::DownCastFunc GenericCast::theDownCastMatrix[24] =
 {
-/*uA*/  0, /*str*/ str_down, /*flt*/ 0, /*dbl*/ 0, /*dec*/ 0, /*int*/ int_down, /*dur*/ 0,
-/*yMD*/ 0, /*dTD*/ 0, /*dT*/  0, /*tim*/ 0, /*dat*/ 0, /*gYM*/ 0, /*gYr*/ 0, /*gMD*/ 0,
-/*gDay*/0, /*gMon*/0, /*bool*/0, /*b64*/ 0, /*hxB*/ 0, /*aURI*/0, /*QN*/  0, /*NOT*/ 0
+/*uA*/    0,
+/*str*/   str_down,
+/*flt*/   0,
+/*dbl*/   0,
+/*dec*/   0,
+/*int*/   int_down,
+/*dur*/   0,
+/*yMD*/   0,
+/*dTD*/   0,
+/*dT*/    0,
+/*tim*/   0,
+/*dat*/   0,
+/*gYM*/   0,
+/*gYr*/   0,
+/*gMD*/   0,
+/*gDay*/  0,
+/*gMon*/  0,
+/*bool*/  0,
+/*b64*/   0,
+/*hxB*/   0,
+/*aURI*/  0,
+/*QN*/    0,
+/*NOT*/   0,
+/*uint*/  int_down,
 };
 
 
 /*******************************************************************************
 
 ********************************************************************************/
-const GenericCast::CastFunc GenericCast::theCastMatrix[23][23] =
+const GenericCast::CastFunc GenericCast::theCastMatrix[24][24] =
 {
           /*uA*/     /*str*/    /*flt*/    /*dbl*/    /*dec*/    /*int*/    /*dur*/    /*yMD*/    /*dTD*/    /*dT*/
           /*tim*/    /*dat*/    /*gYM*/    /*gYr*/    /*gMD*/    /*gDay*/   /*gMon*/   /*bool*/   /*b64*/    /*hxB*/
-          /*aURI*/   /*QN*/     /*NOT*/
+          /*aURI*/   /*QN*/     /*NOT*/    /*uint*/
+
 /*uA*/   {&uA_uA,    &uA_str,   &uA_flt,   &uA_dbl,   &uA_dec,   &uA_int,   &uA_dur,   &uA_yMD,   &uA_dTD,   &uA_dT,
           &uA_tim,   &uA_dat,   &uA_gYM,   &uA_gYr,   &uA_gMD,   &uA_gDay,  &uA_gMon,  &uA_bool,  &uA_b64,   &uA_hxB,
-          &uA_aURI,  0,         0},
+          &uA_aURI,  0,         0,         &uA_uint},
 
 /*str*/  {&str_uA,   &str_str,  &str_flt,  &str_dbl,  &str_dec,  &str_int,  &str_dur,  &str_yMD,  &str_dTD,  &str_dT,
           &str_tim,  &str_dat,  &str_gYM,  &str_gYr,  &str_gMD,  &str_gDay, &str_gMon, &str_bool, &str_b64,  &str_hxB,
-          &str_aURI, &str_QN,   &str_NOT},
+          &str_aURI, &str_QN,   &str_NOT,  &str_uint},
 
 /*flt*/  {&flt_uA,   &flt_str,  &flt_flt,  &flt_dbl,  &flt_dec,  &flt_int,  0,         0,         0,         0,
           0,         0,         0,         0,         0,         0,         0,         &flt_bool, 0,         0,
-          0,         0,         0},
+          0,         0,         0,         &flt_uint},
 
 /*dbl*/  {&dbl_uA,   &dbl_str,  &dbl_flt,  &dbl_dbl,  &dbl_dec,  &dbl_int,  0,         0,         0,         0,
           0,         0,         0,         0,         0,         0,         0,         &dbl_bool, 0,         0,
-          0,         0,         0},
+          0,         0,         0,         &dbl_uint},
 
 /*dec*/  {&dec_uA,   &dec_str,  &dec_flt,  &dec_dbl,  &dec_dec,  &dec_int,  0,         0,         0,         0,
           0,         0,         0,         0,         0,         0,         0,         &dec_bool, 0,         0,
-          0,         0,         0},
+          0,         0,         0,         &dec_uint},
+
+          /*uA*/     /*str*/    /*flt*/    /*dbl*/    /*dec*/    /*int*/    /*dur*/    /*yMD*/    /*dTD*/    /*dT*/
+          /*tim*/    /*dat*/    /*gYM*/    /*gYr*/    /*gMD*/    /*gDay*/   /*gMon*/   /*bool*/   /*b64*/    /*hxB*/
+          /*aURI*/   /*QN*/     /*NOT*/    /*uint*/
+
 /*int*/  {&int_uA,   &int_str,  &int_flt,  &int_dbl,  &int_dec,  &int_int,  0,         0,         0,         0,
           0,         0,         0,         0,         0,         0,         0,         &int_bool, 0,         0,
-          0,         0,         0},
+          0,         0,         0,         &int_uint},
+
 /*dur*/  {&dur_uA,   &dur_str,  0,         0,         0,         0,         &dur_dur,  &dur_yMD,  &dur_dTD,  0,
           0,         0,         0,         0,         0,         0,         0,         0,         0,         0,
-          0,         0,         0},
+          0,         0,         0,         0},
+
 /*yMD*/  {&yMD_uA,   &yMD_str,  0,         0,         0,         0,         &yMD_dur,  &yMD_yMD,  &yMD_dTD,  0,
           0,         0,         0,         0,         0,         0,         0,         0,         0,         0,
-          0,         0,         0},
+          0,         0,         0,         0},
+
 /*dTD*/  {&dTD_uA,   &dTD_str,  0,         0,         0,         0,         &dTD_dur,  &dTD_yMD,  &dTD_dTD,  0,
           0,         0,         0,         0,         0,         0,         0,         0,         0,         0,
-          0,         0,         0},
-          /*uA*/     /*str*/    /*flt*/    /*dbl*/    /*dec*/    /*int*/    /*dur*/    /*yMD*/    /*dTD*/    /*dT*/
-          /*tim*/    /*dat*/    /*gYM*/    /*gYr*/    /*gMD*/    /*gDay*/   /*gMon*/   /*bool*/   /*b64*/    /*hxB*/
-          /*aURI*/   /*QN*/     /*NOT*/
+          0,         0,         0,         0},
+
 /*dT*/   {&dT_uA,    &dT_str,   0,         0,         0,         0,         0,         0,         0,         &dT_dT,
           &dT_tim,   &dT_dat,   &dT_gYM,   &dT_gYr,   &dT_gMD,   &dT_gDay,  &dT_gMon,  0,         0,         0,
-          0,         0,         0},
+          0,         0,         0,         0},
+
+          /*uA*/     /*str*/    /*flt*/    /*dbl*/    /*dec*/    /*int*/    /*dur*/    /*yMD*/    /*dTD*/    /*dT*/
+          /*tim*/    /*dat*/    /*gYM*/    /*gYr*/    /*gMD*/    /*gDay*/   /*gMon*/   /*bool*/   /*b64*/    /*hxB*/
+          /*aURI*/   /*QN*/     /*NOT*/    /*uint*/
+
 /*tim*/  {&tim_uA,   &tim_str,  0,         0,         0,         0,         0,         0,         0,         0,
           &tim_tim,  0,         0,         0,         0,         0,         0,         0,         0,         0,
-          0,         0,         0},
+          0,         0,         0,         0},
+
 /*dat*/  {&dat_uA,   &dat_str,  0,         0,         0,         0,         0,         0,         0,         &dat_dT,
           0,         &dat_dat,  &dat_gYM,  &dat_gYr,  &dat_gMD,  &dat_gDay, &dat_gMon, 0,         0,         0,
-          0,         0,         0},
+          0,         0,         0,         0},
+
 /*gYM*/  {&gYM_uA,   &gYM_str,  0,         0,         0,         0,         0,         0,         0,         0,
           0,         0,         &gYM_gYM,  0,         0,         0,         0,         0,         0,         0,
-          0,         0,         0},
+          0,         0,         0,         0},
+
 /*gYr*/  {&gYr_uA,   &gYr_str,  0,         0,         0,         0,         0,         0,         0,         0,
           0,         0,         0,         &gYr_gYr,  0,         0,         0,         0,         0,         0,
-          0,         0,         0},
+          0,         0,         0,         0},
+
 /*gMD*/  {&gMD_uA,   &gMD_str,  0,         0,         0,         0,         0,         0,         0,         0,
           0,         0,         0,         0,         &gMD_gMD,  0,         0,         0,         0,         0,
-          0,         0,         0},
+          0,         0,         0,         0},
+
+          /*uA*/     /*str*/    /*flt*/    /*dbl*/    /*dec*/    /*int*/    /*dur*/    /*yMD*/    /*dTD*/    /*dT*/
+          /*tim*/    /*dat*/    /*gYM*/    /*gYr*/    /*gMD*/    /*gDay*/   /*gMon*/   /*bool*/   /*b64*/    /*hxB*/
+          /*aURI*/   /*QN*/     /*NOT*/    /*uint*/
+
 /*gDay*/ {&gDay_uA,  &gDay_str, 0,         0,         0,         0,         0,         0,         0,         0,
           0,         0,         0,         0,         0,         &gDay_gDay,0,         0,         0,         0,
-          0,         0,         0},
+          0,         0,         0,         0},
+
 /*gMon*/ {&gMon_uA,  &gMon_str, 0,         0,         0,         0,         0,         0,         0,         0,
           0,         0,         0,         0,         0,         0,         &gMon_gMon,0,         0,         0,
-          0,         0,         0},
+          0,         0,         0,         0},
+
 /*bool*/ {&bool_uA,  &bool_str, &bool_flt, &bool_dbl, &bool_dec, &bool_int, 0,         0,         0,         0,
           0,         0,         0,         0,         0,         0,         0,         &bool_bool,0,         0,
-          0,         0,         0},
+          0,         0,         0,         &bool_uint},
+
 /*b64*/  {&b64_uA,   &b64_str,  0,         0,         0,         0,         0,         0,         0,         0,
           0,         0,         0,         0,         0,         0,         0,         0,         &b64_b64,  &b64_hxB,
-          0,         0,         0},
+          0,         0,         0,         0},
+
 /*hxB*/  {&hxB_uA,   &hxB_str,  0,         0,         0,         0,         0,         0,         0,         0,
           0,         0,         0,         0,         0,         0,         0,         0,         &hxB_b64,  &hxB_hxB,
-          0,         0,         0},
+          0,         0,         0,         0},
+
+          /*uA*/     /*str*/    /*flt*/    /*dbl*/    /*dec*/    /*int*/    /*dur*/    /*yMD*/    /*dTD*/    /*dT*/
+          /*tim*/    /*dat*/    /*gYM*/    /*gYr*/    /*gMD*/    /*gDay*/   /*gMon*/   /*bool*/   /*b64*/    /*hxB*/
+          /*aURI*/   /*QN*/     /*NOT*/    /*uint*/
+
 /*aURI*/ {&aURI_uA,  &aURI_str, 0,         0,         0,         0,         0,         0,         0,         0,
           0,         0,         0,         0,         0,         0,         0,         0,         0,         0,
-          &aURI_aURI,0,         0},
+          &aURI_aURI,0,         0,         0},
+
 /*QN*/   {&QN_uA,    &QN_str,   0,         0,         0,         0,         0,         0,         0,         0,
           0,         0,         0,         0,         0,         0,         0,         0,         0,         0,
-          0,         &QN_QN,    0},
+          0,         &QN_QN,    0,         0},
+
 /*NOT*/  {&NOT_uA,   &NOT_str,  0,         0,         0,         0,         0,         0,         0,         0,
           0,         0,         0,         0,         0,         0,         0,         0,         0,         0,
-          0,         0,         &NOT_NOT}
+          0,         0,         &NOT_NOT,  0},
+
+/*uint*/ {&uint_uA,  &uint_str, &uint_flt, &uint_dbl, &uint_dec, &uint_int, 0,         0,         0,         0,
+          0,         0,         0,         0,         0,         0,         0,         &uint_bool,0,         0,
+          0,         0,         0,         &uint_uint},
 };
 
 

=== modified file 'src/types/casting.h'
--- src/types/casting.h	2011-06-14 17:26:33 +0000
+++ src/types/casting.h	2011-10-31 21:41:43 +0000
@@ -49,8 +49,8 @@
                         ); 
 private:
   static const int          theMapping[TypeConstants::ATOMIC_TYPE_CODE_LIST_SIZE];
-  static const CastFunc     theCastMatrix[23][23];
-  static const DownCastFunc theDownCastMatrix[23];
+  static const CastFunc     theCastMatrix[24][24];
+  static const DownCastFunc theDownCastMatrix[24];
 
   GenericCast() {}
     

=== modified file 'src/types/schema/XercesParseUtils.cpp'
--- src/types/schema/XercesParseUtils.cpp	2011-06-30 05:16:55 +0000
+++ src/types/schema/XercesParseUtils.cpp	2011-10-31 21:41:43 +0000
@@ -118,7 +118,7 @@
   {
     long value = xsval->fData.fValue.f_long;
 
-    xs_integer tvalue(value);
+    xs_positiveInteger tvalue(value);
 
     store::ItemFactory* factory = GENV_ITEMFACTORY;
     return factory->createPositiveInteger(result, tvalue);
@@ -207,7 +207,7 @@
   {
     long value = xsval->fData.fValue.f_long;
 
-    xs_integer tvalue(value);
+    xs_nonNegativeInteger tvalue(value);
 
     store::ItemFactory* factory = GENV_ITEMFACTORY;
     return factory->createNonNegativeInteger(result, tvalue);

=== modified file 'src/util/stl_util.h'
--- src/util/stl_util.h	2011-10-11 17:59:20 +0000
+++ src/util/stl_util.h	2011-10-31 21:41:43 +0000
@@ -199,6 +199,13 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 template<typename NumericType> inline
+typename std::enable_if<ZORBA_TR1_NS::is_arithmetic<NumericType>::value,
+                        bool>::type
+gt0( NumericType n ) {                  // for completeness
+  return n > 0;
+}
+
+template<typename NumericType> inline
 typename std::enable_if<ZORBA_TR1_NS::is_signed<NumericType>::value,bool>::type
 ge0( NumericType n ) {
   return n >= 0;
@@ -210,6 +217,30 @@
   return true;
 }
 
+template<typename NumericType> inline
+typename std::enable_if<ZORBA_TR1_NS::is_signed<NumericType>::value,bool>::type
+lt0( NumericType n ) {
+  return n < 0;
+}
+
+template<typename IntType> inline
+typename std::enable_if<ZORBA_TR1_NS::is_unsigned<IntType>::value,bool>::type
+lt0( IntType ) {
+  return false;
+}
+
+template<typename NumericType> inline
+typename std::enable_if<ZORBA_TR1_NS::is_signed<NumericType>::value,bool>::type
+le0( NumericType n ) {
+  return n <= 0;
+}
+
+template<typename IntType> inline
+typename std::enable_if<ZORBA_TR1_NS::is_unsigned<IntType>::value,bool>::type
+le0( IntType n ) {
+  return n == 0;
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 template<typename T> class stack_generator {

=== modified file 'src/util/string_util.cpp'
--- src/util/string_util.cpp	2011-07-07 18:48:27 +0000
+++ src/util/string_util.cpp	2011-10-31 21:41:43 +0000
@@ -110,14 +110,22 @@
   // We have to check for '-' ourselves since strtoull(3) allows it (oddly).
   //
   s = ascii::trim_start_whitespace( s );
-  if ( *s == '-' )
+  bool const minus = *s == '-';
+
+  char *end;
+  errno = 0;
+  unsigned long long const result = std::strtoull( s, &end, 10 );
+  check_parse_number( s, end, static_cast<unsigned long long*>( nullptr ) );
+
+  if ( minus && result ) {
+    //
+    // Throw an exception only if there was a '-' and the result is non-zero.
+    // Hence, this allows "-0" and treats it as "0".
+    //
     throw std::invalid_argument(
       "\"-\": invalid character for unsigned integer"
     );
-  char *end;
-  errno = 0;
-  unsigned long long const result = std::strtoull( s, &end, 10 );
-  check_parse_number( s, end, static_cast<unsigned long long*>( nullptr ) );
+  }
   return result;
 }
 

=== modified file 'src/zorbaserialization/zorba_class_serializer.cpp'
--- src/zorbaserialization/zorba_class_serializer.cpp	2011-09-30 14:06:33 +0000
+++ src/zorbaserialization/zorba_class_serializer.cpp	2011-10-31 21:41:43 +0000
@@ -205,10 +205,19 @@
   }
 }
 
-void operator&(serialization::Archiver &ar, Integer &obj)
+#ifdef ZORBA_WITH_BIG_INTEGER
+void operator&(serialization::Archiver &ar, IntegerImpl &obj)
+#else
+template<typename IntType>
+void operator&(serialization::Archiver &ar, IntegerImpl<IntType> &obj)
+#endif /* ZORBA_WITH_BIG_INTEGER */
 {
   ar & obj.value_;
 }
+#ifndef ZORBA_WITH_BIG_INTEGER
+template void operator&(serialization::Archiver&, IntegerImpl<long long>&);
+template void operator&(serialization::Archiver&, IntegerImpl<unsigned long long>&);
+#endif /* ZORBA_WITH_BIG_INTEGER */
 
 void iterator_to_vector(store::Iterator_t iter, std::vector<store::Item_t> &items)
 {
@@ -590,7 +599,7 @@
       }
       else if(name_of_type == "nonNegativeInteger")
       {
-        SERIALIZE_FIELD(xs_uinteger, value, getUnsignedIntegerValue());
+        SERIALIZE_FIELD(xs_nonNegativeInteger, value, getUnsignedIntegerValue());
         FINALIZE_SERIALIZE(createNonNegativeInteger, (result, value));
       }
       else if(name_of_type == "negativeInteger")
@@ -600,7 +609,7 @@
       }
       else if(name_of_type == "positiveInteger")
       {
-        SERIALIZE_FIELD(xs_uinteger, value, getUnsignedIntegerValue());
+        SERIALIZE_FIELD(xs_positiveInteger, value, getUnsignedIntegerValue());
         FINALIZE_SERIALIZE(createPositiveInteger, (result, value));
       }
          

=== modified file 'src/zorbaserialization/zorba_class_serializer.h'
--- src/zorbaserialization/zorba_class_serializer.h	2011-07-07 12:05:43 +0000
+++ src/zorbaserialization/zorba_class_serializer.h	2011-10-31 21:41:43 +0000
@@ -37,13 +37,22 @@
   class function;
   class Diagnostic;
   class ZorbaException;
-  class Integer;
+#ifdef ZORBA_WITH_BIG_INTEGER
+  class IntegerImpl;
+#else
+  template<typename IntType> class IntegerImpl;
+#endif /* ZORBA_WITH_BIG_INTEGER */
 
   namespace serialization{
 //void operator&(Archiver &ar, XQType *&obj);
 void operator&(Archiver &ar, const XQType *&obj);
 void operator&(Archiver &ar, MAPM &obj);
-void operator&(Archiver &ar, Integer &obj);
+#ifdef ZORBA_WITH_BIG_INTEGER
+void operator&(Archiver &ar, IntegerImpl &obj);
+#else
+template<typename IntType>
+void operator&(Archiver &ar, IntegerImpl<IntType> &obj);
+#endif /* ZORBA_WITH_BIG_INTEGER */
 
 void operator&(Archiver &ar, XQPCollator *&obj);
 void operator&(Archiver &ar, store::Item* &obj);

=== modified file 'src/zorbatypes/decimal.cpp'
--- src/zorbatypes/decimal.cpp	2011-07-06 19:59:26 +0000
+++ src/zorbatypes/decimal.cpp	2011-10-31 21:41:43 +0000
@@ -28,6 +28,16 @@
 #include "integer.h"
 #include "numconversions.h"
 
+#ifdef ZORBA_WITH_BIG_INTEGER
+# define TEMPLATE_DECL(T) /* nothing */
+# define INTEGER_IMPL(T)  IntegerImpl
+#else
+# define TEMPLATE_DECL(T) template<typename T>
+# define INTEGER_IMPL(T)  IntegerImpl<T>
+#endif /* ZORBA_WITH_BIG_INTEGER */
+#define INTEGER_IMPL_LL  INTEGER_IMPL(long long)
+#define INTEGER_IMPL_ULL INTEGER_IMPL(unsigned long long)
+
 namespace zorba {
 
 SERIALIZABLE_CLASS_VERSIONS(Decimal)
@@ -218,8 +228,13 @@
   value_ = f.getNumber();
 }
 
-Decimal::Decimal( Integer const &i ) : value_( i.itod() ) {
+TEMPLATE_DECL(T)
+Decimal::Decimal( INTEGER_IMPL(T) const &i ) : value_( i.itod() ) {
 }
+#ifndef ZORBA_WITH_BIG_INTEGER
+template Decimal::Decimal( INTEGER_IMPL_LL const& );
+template Decimal::Decimal( INTEGER_IMPL_ULL const& );
+#endif /* ZORBA_WITH_BIG_INTEGER */
 
 ////////// assignment operators ///////////////////////////////////////////////
 
@@ -235,10 +250,15 @@
   return *this;
 }
 
-Decimal& Decimal::operator=( Integer const &i ) {
+TEMPLATE_DECL(T)
+Decimal& Decimal::operator=( INTEGER_IMPL(T) const &i ) {
   value_ = i.itod();
   return *this;
 }
+#ifndef ZORBA_WITH_BIG_INTEGER
+template Decimal& Decimal::operator=( INTEGER_IMPL_LL const& );
+template Decimal& Decimal::operator=( INTEGER_IMPL_ULL const& );
+#endif /* ZORBA_WITH_BIG_INTEGER */
 
 Decimal& Decimal::operator=( Double const &d ) {
   if ( !d.isFinite() )
@@ -256,61 +276,69 @@
 
 ////////// arithmetic operators ///////////////////////////////////////////////
 
-Decimal operator+( Decimal const &d, Integer const &i ) {
-  return d.value_ + i.itod();
-}
-
-Decimal operator-( Decimal const &d, Integer const &i ) {
-  return d.value_ - i.itod();
-}
-
-Decimal operator*( Decimal const &d, Integer const &i ) {
-  return d.value_ * i.itod();
-}
-
-Decimal operator/( Decimal const &d, Integer const &i ) {
-  return d.value_ / i.itod();
-}
-
-Decimal operator%( Decimal const &d, Integer const &i ) {
-  return d.value_ % i.itod();
-}
+#ifdef ZORBA_WITH_BIG_INTEGER
+# define ZORBA_INSTANTIATE(OP) /* nothing */
+#else
+# define ZORBA_INSTANTIATE(OP)                                            \
+  template Decimal operator OP( Decimal const&, INTEGER_IMPL_LL const& ); \
+  template Decimal operator OP( Decimal const&, INTEGER_IMPL_ULL const& )
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
+#define ZORBA_DECIMAL_OP(OP)                                          \
+  TEMPLATE_DECL(T)                                                    \
+  Decimal operator OP( Decimal const &d, INTEGER_IMPL(T) const &i ) { \
+    return d.value_ OP i.itod();                                      \
+  }                                                                   \
+  ZORBA_INSTANTIATE(OP)
+
+ZORBA_DECIMAL_OP(+);
+ZORBA_DECIMAL_OP(-);
+ZORBA_DECIMAL_OP(*);
+ZORBA_DECIMAL_OP(/);
+ZORBA_DECIMAL_OP(%);
+#undef ZORBA_DECIMAL_OP
+#undef ZORBA_INSTANTIATE
 
 ////////// relational operators ///////////////////////////////////////////////
 
-bool operator==( Decimal const &d, Integer const &i ) {
-  return d.value_ == i.itod();
-}
-
-bool operator!=( Decimal const &d, Integer const &i ) {
-  return d.value_ != i.itod();
-}
-
-bool operator<( Decimal const &d, Integer const &i ) {
-  return d.value_ < i.itod();
-}
-
-bool operator<=( Decimal const &d, Integer const &i ) {
-  return d.value_ <= i.itod();
-}
-
-bool operator>( Decimal const &d, Integer const &i ) {
-  return d.value_ > i.itod();
-}
-
-bool operator>=( Decimal const &d, Integer const &i ) {
-  return d.value_ >= i.itod();
-}
+#ifdef ZORBA_WITH_BIG_INTEGER
+# define ZORBA_INSTANTIATE(OP) /* nothing */
+#else
+# define ZORBA_INSTANTIATE(OP)                                          \
+  template bool operator OP( Decimal const&, INTEGER_IMPL_LL const& );  \
+  template bool operator OP( Decimal const&, INTEGER_IMPL_ULL const& )
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
+#define ZORBA_DECIMAL_OP(OP)                                        \
+  TEMPLATE_DECL(T)                                                  \
+  bool operator OP( Decimal const &d, INTEGER_IMPL(T) const &i ) {  \
+    return d.value_ OP i.itod();                                    \
+  }                                                                 \
+  ZORBA_INSTANTIATE(OP)
+
+ZORBA_DECIMAL_OP(==);
+ZORBA_DECIMAL_OP(!=);
+ZORBA_DECIMAL_OP(< );
+ZORBA_DECIMAL_OP(<=);
+ZORBA_DECIMAL_OP(> );
+ZORBA_DECIMAL_OP(>=);
+#undef ZORBA_DECIMAL_OP
+#undef ZORBA_INSTANTIATE
 
 ////////// math functions /////////////////////////////////////////////////////
 
 Decimal Decimal::round() const {
-  return round( Integer::zero() );
+  return round( INTEGER_IMPL_LL::zero() );
 }
 
-Decimal Decimal::round( Integer const &precision ) const {
+TEMPLATE_DECL(T)
+Decimal Decimal::round( INTEGER_IMPL(T) const &precision ) const {
   return round( value_, precision.itod() );
 }
+#ifndef ZORBA_WITH_BIG_INTEGER
+template Decimal Decimal::round( INTEGER_IMPL_LL const& ) const;
+template Decimal Decimal::round( INTEGER_IMPL_ULL const& ) const;
+#endif /* ZORBA_WITH_BIG_INTEGER */
 
 Decimal::value_type Decimal::round( value_type const &v,
                                     value_type const &precision ) {
@@ -322,9 +350,14 @@
   return result;
 }
 
-Decimal Decimal::roundHalfToEven( Integer const &precision ) const {
+TEMPLATE_DECL(T)
+Decimal Decimal::roundHalfToEven( INTEGER_IMPL(T) const &precision ) const {
   return roundHalfToEven( value_, precision.itod() );
 }
+#ifndef ZORBA_WITH_BIG_INTEGER
+template Decimal Decimal::roundHalfToEven( INTEGER_IMPL_LL const& ) const;
+template Decimal Decimal::roundHalfToEven( INTEGER_IMPL_ULL const& ) const;
+#endif /* ZORBA_WITH_BIG_INTEGER */
 
 Decimal::value_type Decimal::roundHalfToEven( value_type const &v,
                                               value_type const &precision ) {

=== modified file 'src/zorbatypes/decimal.h'
--- src/zorbatypes/decimal.h	2011-06-24 18:27:08 +0000
+++ src/zorbatypes/decimal.h	2011-10-31 21:41:43 +0000
@@ -29,6 +29,14 @@
 #include "zorbatypes_decl.h"
 #include "zstring.h"
 
+#ifdef ZORBA_WITH_BIG_INTEGER
+# define TEMPLATE_DECL(T) /* nothing */
+# define INTEGER_IMPL(T)  IntegerImpl
+#else
+# define TEMPLATE_DECL(T) template<typename T>
+# define INTEGER_IMPL(T)  IntegerImpl<T>
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
 namespace zorba {
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -52,7 +60,9 @@
   Decimal( float n );
   Decimal( double n );
   Decimal( Decimal const &d );
-  Decimal( Integer const &i );
+
+  TEMPLATE_DECL(T)
+  Decimal( INTEGER_IMPL(T) const &i );
 
   /**
    * Constructs a %Decimal from a C string.
@@ -98,27 +108,30 @@
   Decimal& operator=( Decimal const &d );
   Decimal& operator=( Double const &d );
   Decimal& operator=( Float const &f );
-  Decimal& operator=( Integer const &i );
+
+  TEMPLATE_DECL(T)
+  Decimal& operator=( INTEGER_IMPL(T) const &i );
 
   ////////// arithmetic operators /////////////////////////////////////////////
 
-  friend Decimal operator+( Decimal const &d1, Decimal const &d2 );
-  friend Decimal operator-( Decimal const &d1, Decimal const &d2 );
-  friend Decimal operator*( Decimal const &d1, Decimal const &d2 );
-  friend Decimal operator/( Decimal const &d1, Decimal const &d2 );
-  friend Decimal operator%( Decimal const &d1, Decimal const &d2 );
-
-  friend Decimal operator+( Decimal const &d, Integer const &i );
-  friend Decimal operator-( Decimal const &d, Integer const &i );
-  friend Decimal operator*( Decimal const &d, Integer const &i );
-  friend Decimal operator/( Decimal const &d, Integer const &i );
-  friend Decimal operator%( Decimal const &d, Integer const &i );
-
-  friend Decimal operator+( Integer const &i, Decimal const &d );
-  friend Decimal operator-( Integer const &i, Decimal const &d );
-  friend Decimal operator*( Integer const &i, Decimal const &d );
-  friend Decimal operator/( Integer const &i, Decimal const &d );
-  friend Decimal operator%( Integer const &i, Decimal const &d );
+  friend Decimal operator+( Decimal const&, Decimal const& );
+  friend Decimal operator-( Decimal const&, Decimal const& );
+  friend Decimal operator*( Decimal const&, Decimal const& );
+  friend Decimal operator/( Decimal const&, Decimal const& );
+  friend Decimal operator%( Decimal const&, Decimal const& );
+
+#define ZORBA_DECIMAL_OP(OP)                                            \
+  TEMPLATE_DECL(T)                                                      \
+  friend Decimal operator OP( Decimal const&, INTEGER_IMPL(T) const& ); \
+  TEMPLATE_DECL(T)                                                      \
+  friend Decimal operator OP( INTEGER_IMPL(T) const&, Decimal const& )
+
+  ZORBA_DECIMAL_OP(+);
+  ZORBA_DECIMAL_OP(-);
+  ZORBA_DECIMAL_OP(*);
+  ZORBA_DECIMAL_OP(/);
+  ZORBA_DECIMAL_OP(%);
+#undef ZORBA_DECIMAL_OP
 
   Decimal& operator+=( Decimal const& );
   Decimal& operator-=( Decimal const& );
@@ -126,46 +139,49 @@
   Decimal& operator/=( Decimal const& );
   Decimal& operator%=( Decimal const& );
 
-  Decimal& operator+=( Integer const& );
-  Decimal& operator-=( Integer const& );
-  Decimal& operator*=( Integer const& );
-  Decimal& operator/=( Integer const& );
-  Decimal& operator%=( Integer const& );
+#define ZORBA_DECIMAL_OP(OP) \
+  TEMPLATE_DECL(T) Decimal& operator OP( INTEGER_IMPL(T) const& )
+
+  ZORBA_DECIMAL_OP(+=);
+  ZORBA_DECIMAL_OP(-=);
+  ZORBA_DECIMAL_OP(*=);
+  ZORBA_DECIMAL_OP(/=);
+  ZORBA_DECIMAL_OP(%=);
+#undef ZORBA_DECIMAL_OP
 
   Decimal operator-() const;
 
   ////////// relational operators /////////////////////////////////////////////
 
-  friend bool operator==( Decimal const &d1, Decimal const &d2 );
-  friend bool operator!=( Decimal const &d1, Decimal const &d2 );
-  friend bool operator< ( Decimal const &d1, Decimal const &d2 );
-  friend bool operator<=( Decimal const &d1, Decimal const &d2 );
-  friend bool operator> ( Decimal const &d1, Decimal const &d2 );
-  friend bool operator>=( Decimal const &d1, Decimal const &d2 );
-
-  friend bool operator==( Decimal const &d, Integer const &i );
-  friend bool operator!=( Decimal const &d, Integer const &i );
-  friend bool operator< ( Decimal const &d, Integer const &i );
-  friend bool operator<=( Decimal const &d, Integer const &i );
-  friend bool operator> ( Decimal const &d, Integer const &i );
-  friend bool operator>=( Decimal const &d, Integer const &i );
-
-  friend bool operator==( Integer const &i, Decimal const &d );
-  friend bool operator!=( Integer const &i, Decimal const &d );
-  friend bool operator< ( Integer const &i, Decimal const &d );
-  friend bool operator<=( Integer const &i, Decimal const &d );
-  friend bool operator> ( Integer const &i, Decimal const &d );
-  friend bool operator>=( Integer const &i, Decimal const &d );
+#define ZORBA_DECIMAL_OP(OP)                                          \
+  friend bool operator OP( Decimal const&, Decimal const& );          \
+  TEMPLATE_DECL(T)                                                    \
+  friend bool operator OP( Decimal const&, INTEGER_IMPL(T) const& );  \
+  TEMPLATE_DECL(T)                                                    \
+  friend bool operator OP( INTEGER_IMPL(T) const&, Decimal const& )
+
+  ZORBA_DECIMAL_OP(==);
+  ZORBA_DECIMAL_OP(!=);
+  ZORBA_DECIMAL_OP(< );
+  ZORBA_DECIMAL_OP(<=);
+  ZORBA_DECIMAL_OP(> );
+  ZORBA_DECIMAL_OP(>=);
+#undef ZORBA_DECIMAL_OP
 
   ////////// math functions ///////////////////////////////////////////////////
 
-  int compare( Decimal const &d ) const;
+  int compare( Decimal const& ) const;
 
   Decimal ceil() const;
   Decimal floor() const;
   Decimal round() const;
-  Decimal round( Integer const &precision ) const;
-  Decimal roundHalfToEven( Integer const &precision ) const;
+
+  TEMPLATE_DECL(T)
+  Decimal round( INTEGER_IMPL(T) const &precision ) const;
+
+  TEMPLATE_DECL(T)
+  Decimal roundHalfToEven( INTEGER_IMPL(T) const &precision ) const;
+
   Decimal sqrt() const;
 
   ////////// miscellaneous ////////////////////////////////////////////////////
@@ -215,7 +231,7 @@
   static zstring toString( value_type const&,
                            int precision = ZORBA_FLOAT_POINT_PRECISION );
 
-  friend class Integer;
+  TEMPLATE_DECL(T) friend class IntegerImpl;
   template<typename T> friend class FloatImpl;
 
   friend xs_long to_xs_long( Decimal const& );
@@ -443,6 +459,10 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 } // namespace zorba
+
+#undef TEMPLATE_DECL
+#undef INTEGER_IMPL
+
 #endif /* ZORBA_DECIMAL_H */
 /*
  * Local variables:

=== modified file 'src/zorbatypes/floatimpl.cpp'
--- src/zorbatypes/floatimpl.cpp	2011-07-07 12:05:43 +0000
+++ src/zorbatypes/floatimpl.cpp	2011-10-31 21:41:43 +0000
@@ -29,6 +29,16 @@
 
 #include "zorbaserialization/serialization_engine.h"
 
+#ifdef ZORBA_WITH_BIG_INTEGER
+# define TEMPLATE_DECL(T) /* nothing */
+# define INTEGER_IMPL(T)  IntegerImpl
+#else
+# define TEMPLATE_DECL(T) template<typename T>
+# define INTEGER_IMPL(T)  IntegerImpl<T>
+#endif /* ZORBA_WITH_BIG_INTEGER */
+#define INTEGER_IMPL_LL  INTEGER_IMPL(long long)
+#define INTEGER_IMPL_ULL INTEGER_IMPL(unsigned long long)
+
 ///////////////////////////////////////////////////////////////////////////////
 
 namespace zorba {
@@ -144,11 +154,20 @@
 }
 
 template<typename FloatType>
-FloatImpl<FloatType>::FloatImpl( Integer const &i ) {
+TEMPLATE_DECL(IntType)
+FloatImpl<FloatType>::FloatImpl( INTEGER_IMPL(IntType) const &i ) {
   zstring const temp( i.toString() );
   parse( temp.c_str() );
 }
 
+#ifndef ZORBA_WITH_BIG_INTEGER
+template FloatImpl<float>::FloatImpl( INTEGER_IMPL_LL const& );
+template FloatImpl<float>::FloatImpl( INTEGER_IMPL_ULL const& );
+
+template FloatImpl<double>::FloatImpl( INTEGER_IMPL_LL const& );
+template FloatImpl<double>::FloatImpl( INTEGER_IMPL_ULL const& );
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
 ////////// math functions /////////////////////////////////////////////////////
 
 template<typename FloatType>

=== modified file 'src/zorbatypes/floatimpl.h'
--- src/zorbatypes/floatimpl.h	2011-10-03 09:18:49 +0000
+++ src/zorbatypes/floatimpl.h	2011-10-31 21:41:43 +0000
@@ -29,13 +29,22 @@
 #include "schema_types.h"
 #include "zorbatypes_decl.h"
 
+#ifdef ZORBA_WITH_BIG_INTEGER
+# define TEMPLATE_DECL(T) /* nothing */
+# define INTEGER_IMPL(T)  IntegerImpl
+#else
+# define TEMPLATE_DECL(T) template<typename T>
+# define INTEGER_IMPL(T)  IntegerImpl<T>
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
 namespace zorba {
 
 template<typename FloatType>
 class FloatImpl;
-namespace serialization{
+
+namespace serialization {
   template<typename FloatType>
-  void operator&(Archiver &ar, FloatImpl<FloatType> &obj);
+  void operator&( Archiver&, FloatImpl<FloatType>& );
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -61,7 +70,9 @@
   FloatImpl( float n );
   FloatImpl( double n );
   FloatImpl( Decimal const &d );
-  FloatImpl( Integer const &i );
+
+  TEMPLATE_DECL(T)
+  FloatImpl( INTEGER_IMPL(T) const &i );
 
   /**
    * Constructs a %FloatImpl from a C string.
@@ -220,7 +231,7 @@
   void parse( char const* );
   bool parse_etc( char const* );
 
-  friend class Integer;
+  TEMPLATE_DECL(T) friend class IntegerImpl;
   friend class Decimal;
 
   friend class FloatImpl<float>;
@@ -772,6 +783,10 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 } // namespace zorba
+
+#undef TEMPLATE_DECL
+#undef INTEGER_IMPL
+
 #endif // ZORBA_FLOATIMPL_H
 /*
  * Local variables:

=== modified file 'src/zorbatypes/integer.cpp'
--- src/zorbatypes/integer.cpp	2011-07-06 20:13:28 +0000
+++ src/zorbatypes/integer.cpp	2011-10-31 21:41:43 +0000
@@ -27,62 +27,82 @@
 #include "floatimpl.h"
 #include "numconversions.h"
 
+#ifdef ZORBA_WITH_BIG_INTEGER
+# define TEMPLATE_DECL(T) /* nothing */
+# define INTEGER_IMPL(T)  IntegerImpl
+#else
+# define TEMPLATE_DECL(T) template<typename T>
+# define INTEGER_IMPL(T)  IntegerImpl<T>
+#endif /* ZORBA_WITH_BIG_INTEGER */
+#define INTEGER_IMPL_LL  INTEGER_IMPL(long long)
+#define INTEGER_IMPL_ULL INTEGER_IMPL(unsigned long long)
+
 using namespace std;
 
-#ifdef WIN32
-namespace std {
-  inline long long strtoll( char const *s, char **end, int base ) {
-    return ::_strtoi64( s, end, base );
-  }
-
-  inline long long strtoull( char const *s, char **end, int base ) {
-    return ::_strtoui64( s, end, base );
-  }
-}
-#endif /* WIN32 */
+#ifndef ZORBA_WITH_BIG_INTEGER
+unsigned long long MaxUIntegerValue = ~0ull >> 1;
+
+inline bool is_too_big( long long ) {
+  return false;
+}
+
+inline bool is_too_big( unsigned long long n ) {
+  return n > MaxUIntegerValue;
+}
+#endif /* ZORBA_WITH_BIG_INTEGER */
 
 namespace zorba {
 
 ///////////////////////////////////////////////////////////////////////////////
 
-void Integer::parse( char const *s ) {
 #ifdef ZORBA_WITH_BIG_INTEGER
+void IntegerImpl::parse( char const *s ) {
   Decimal::parse( s, &value_, Decimal::parse_integer );
 #else
-  value_ = ztd::aton<value_type>( s );
+template<typename IntType>
+void IntegerImpl<IntType>::parse( char const *s ) {
+  value_type const temp = ztd::aton<value_type>( s );
+  if ( is_too_big( temp ) )
+    throw std::invalid_argument(
+      BUILD_STRING( '"', temp, "\": unsigned integer too big" )
+    );
+  value_ = temp;
 #endif /* ZORBA_WITH_BIG_INTEGER */
 }
 
 ////////// constructors ///////////////////////////////////////////////////////
 
 #ifdef ZORBA_WITH_BIG_INTEGER
-Integer::Integer( long long n ) {
-  ztd::itoa_buf_type buf;
-  value_ = ztd::itoa( n, buf );
-}
-
-Integer::Integer( unsigned long n ) {
-  ztd::itoa_buf_type buf;
-  value_ = ztd::itoa( n, buf );
-}
-
-Integer::Integer( unsigned long long n ) {
+IntegerImpl::IntegerImpl( long long n ) {
+  ztd::itoa_buf_type buf;
+  value_ = ztd::itoa( n, buf );
+}
+
+IntegerImpl::IntegerImpl( unsigned long n ) {
+  ztd::itoa_buf_type buf;
+  value_ = ztd::itoa( n, buf );
+}
+
+IntegerImpl::IntegerImpl( unsigned long long n ) {
   ztd::itoa_buf_type buf;
   value_ = ztd::itoa( n, buf );
 }
 #endif /* ZORBA_WITH_BIG_INTEGER */
 
-Integer::Integer( Decimal const &d ) {
+TEMPLATE_DECL(T)
+INTEGER_IMPL(T)::IntegerImpl( Decimal const &d ) {
   value_ = ftoi( d.value_ );
 }
 
-Integer::Integer( Double const &d ) {
+TEMPLATE_DECL(T)
+INTEGER_IMPL(T)::IntegerImpl( Double const &d ) {
   if ( !d.isFinite() )
     throw std::invalid_argument( "not finite" );
   value_ = ftoi( d.getNumber() );
 }
 
-Integer::Integer( Float const &f ) {
+TEMPLATE_DECL(T)
+INTEGER_IMPL(T)::IntegerImpl( Float const &f ) {
   if ( !f.isFinite() )
     throw std::invalid_argument( "not finite" );
   value_ = ftoi( f.getNumber() );
@@ -91,38 +111,41 @@
 ////////// assignment operators ///////////////////////////////////////////////
 
 #ifdef ZORBA_WITH_BIG_INTEGER
-Integer& Integer::operator=( long long n ) {
-  ztd::itoa_buf_type buf;
-  value_ = ztd::itoa( n, buf );
-  return *this;
-}
-
-Integer& Integer::operator=( unsigned long n ) {
-  ztd::itoa_buf_type buf;
-  value_ = ztd::itoa( n, buf );
-  return *this;
-}
-
-Integer& Integer::operator=( unsigned long long n ) {
+IntegerImpl& IntegerImpl::operator=( long long n ) {
+  ztd::itoa_buf_type buf;
+  value_ = ztd::itoa( n, buf );
+  return *this;
+}
+
+IntegerImpl& IntegerImpl::operator=( unsigned long n ) {
+  ztd::itoa_buf_type buf;
+  value_ = ztd::itoa( n, buf );
+  return *this;
+}
+
+IntegerImpl& IntegerImpl::operator=( unsigned long long n ) {
   ztd::itoa_buf_type buf;
   value_ = ztd::itoa( n, buf );
   return *this;
 }
 #endif /* ZORBA_WITH_BIG_INTEGER */
 
-Integer& Integer::operator=( Decimal const &d ) {
+TEMPLATE_DECL(T)
+INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( Decimal const &d ) {
   value_ = ftoi( d.value_ );
   return *this;
 }
 
-Integer& Integer::operator=( Double const &d ) {
+TEMPLATE_DECL(T)
+INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( Double const &d ) {
   if ( !d.isFinite() )
     throw std::invalid_argument( "not finite" );
   value_ = ftoi( d.getNumber() );
   return *this;
 }
 
-Integer& Integer::operator=( Float const &f ) {
+TEMPLATE_DECL(T)
+INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( Float const &f ) {
   if ( !f.isFinite() )
     throw std::invalid_argument( "not finite" );
   value_ = ftoi( f.getNumber() );
@@ -131,55 +154,67 @@
 
 ////////// arithmetic operators ///////////////////////////////////////////////
 
-Decimal operator+( Integer const &i, Decimal const &d ) {
-  return i.itod() + d.value_;
-}
-
-Decimal operator-( Integer const &i, Decimal const &d ) {
-  return i.itod() - d.value_;
-}
-
-Decimal operator*( Integer const &i, Decimal const &d ) {
-  return i.itod() * d.value_;
-}
-
-Decimal operator/( Integer const &i, Decimal const &d ) {
-  return i.itod() / d.value_;
-}
-
-Decimal operator%( Integer const &i, Decimal const &d ) {
-  return i.itod() % d.value_;
-}
+#ifdef ZORBA_WITH_BIG_INTEGER
+# define ZORBA_INSTANTIATE(OP) /* nothing */
+#else
+# define ZORBA_INSTANTIATE(OP)                                            \
+  template Decimal operator OP( INTEGER_IMPL_LL const&, Decimal const& ); \
+  template Decimal operator OP( INTEGER_IMPL_ULL const&, Decimal const& );
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
+#define ZORBA_INTEGER_OP(OP)                                          \
+  TEMPLATE_DECL(T)                                                    \
+  Decimal operator OP( INTEGER_IMPL(T) const &i, Decimal const &d ) { \
+    return i.itod() OP d.value_;                                      \
+  }                                                                   \
+  ZORBA_INSTANTIATE(OP)
+
+ZORBA_INTEGER_OP(+)
+ZORBA_INTEGER_OP(-)
+ZORBA_INTEGER_OP(*)
+ZORBA_INTEGER_OP(/)
+ZORBA_INTEGER_OP(%)
+#undef ZORBA_INTEGER_OP
+#undef ZORBA_INSTANTIATE
 
 ////////// relational operators ///////////////////////////////////////////////
 
-bool operator==( Integer const &i, Decimal const &d ) {
+TEMPLATE_DECL(T)
+bool operator==( INTEGER_IMPL(T) const &i, Decimal const &d ) {
   return d.is_integer() && i.itod() == d.value_;
 }
 
-bool operator!=( Integer const &i, Decimal const &d ) {
-  return i.itod() != d.value_;
-}
-
-bool operator<( Integer const &i, Decimal const &d ) {
-  return i.itod() < d.value_;
-}
-
-bool operator<=( Integer const &i, Decimal const &d ) {
-  return i.itod() <= d.value_;
-}
-
-bool operator>( Integer const &i, Decimal const &d ) {
-  return i.itod() > d.value_;
-}
-
-bool operator>=( Integer const &i, Decimal const &d ) {
-  return i.itod() >= d.value_;
-}
+#define ZORBA_INTEGER_OP(OP)                                        \
+  TEMPLATE_DECL(T)                                                  \
+  bool operator OP( INTEGER_IMPL(T) const &i, Decimal const &d ) {  \
+    return i.itod() OP d.value_;                                    \
+  }
+
+ZORBA_INTEGER_OP(!=)
+ZORBA_INTEGER_OP(< )
+ZORBA_INTEGER_OP(<=)
+ZORBA_INTEGER_OP(> )
+ZORBA_INTEGER_OP(>=)
+#undef ZORBA_INTEGER_OP
+
+#ifndef ZORBA_WITH_BIG_INTEGER
+#define ZORBA_INSTANTIATE(OP)                                           \
+  template bool operator OP( INTEGER_IMPL_LL const&, Decimal const& );  \
+  template bool operator OP( INTEGER_IMPL_ULL const&, Decimal const& )
+
+ZORBA_INSTANTIATE(==);
+ZORBA_INSTANTIATE(!=);
+ZORBA_INSTANTIATE(< );
+ZORBA_INSTANTIATE(<=);
+ZORBA_INSTANTIATE(> );
+ZORBA_INSTANTIATE(>=);
+#undef ZORBA_INSTANTIATE
+#endif /* ZORBA_WITH_BIG_INTEGER */
 
 ////////// math functions /////////////////////////////////////////////////////
 
-Double Integer::pow( Integer const &power ) const {
+TEMPLATE_DECL(T)
+Double INTEGER_IMPL(T)::pow( INTEGER_IMPL(T) const &power ) const {
 #ifdef ZORBA_WITH_BIG_INTEGER
   value_type const result( value_.pow( power.value_, 15 ) );
   char buf[300];
@@ -193,27 +228,32 @@
 #endif /* ZORBA_WITH_BIG_INTEGER */
 }
 
-Integer Integer::round( Integer const &precision ) const {
-  return Integer( Decimal::round( itod(), precision.itod() ) );
+TEMPLATE_DECL(T)
+INTEGER_IMPL(T) INTEGER_IMPL(T)::round( IntegerImpl const &precision ) const {
+  return IntegerImpl( Decimal::round( itod(), precision.itod() ) );
 }
 
-Integer Integer::roundHalfToEven( Integer const &precision ) const {
-  return Integer( Decimal::roundHalfToEven( itod(), precision.itod() ) );
+TEMPLATE_DECL(T)
+INTEGER_IMPL(T)
+INTEGER_IMPL(T)::roundHalfToEven( IntegerImpl const &precision ) const {
+  return IntegerImpl( Decimal::roundHalfToEven( itod(), precision.itod() ) );
 }
 
 ////////// miscellaneous //////////////////////////////////////////////////////
 
 #ifndef ZORBA_WITH_BIG_INTEGER
-Integer::value_type Integer::ftoi( MAPM const &d ) {
+TEMPLATE_DECL(T)
+typename INTEGER_IMPL(T)::value_type INTEGER_IMPL(T)::ftoi( MAPM const &d ) {
   MAPM const temp( d.sign() >= 0 ? d.floor() : d.ceil() );
   char *const buf = new char[ temp.exponent() + 3 ];
   temp.toIntegerString( buf );
-  value_type const result( std::strtoll( buf, nullptr, 10 ) );
+  value_type const result( ztd::aton<value_type>( buf ) );
   delete[] buf;
   return result;
 }
 
-MAPM Integer::itod() const {
+TEMPLATE_DECL(T)
+MAPM INTEGER_IMPL(T)::itod() const {
   if ( is_long() )
     return static_cast<long>( value_ );
   ztd::itoa_buf_type buf;
@@ -222,17 +262,19 @@
 #endif /* ZORBA_WITH_BIG_INTEGER */
 
 #ifdef ZORBA_WITH_BIG_INTEGER
-uint32_t Integer::hash() const {
+uint32_t IntegerImpl::hash() const {
   return Decimal::hash( value_ );
 }
 #endif /* ZORBA_WITH_BIG_INTEGER */
 
-Integer const& Integer::one() {
-  static Integer const i(1);
+TEMPLATE_DECL(T)
+INTEGER_IMPL(T) const& INTEGER_IMPL(T)::one() {
+  static INTEGER_IMPL(T) const i(1);
   return i;
 }
 
-zstring Integer::toString() const {
+TEMPLATE_DECL(T)
+zstring INTEGER_IMPL(T)::toString() const {
 #ifdef ZORBA_WITH_BIG_INTEGER
   char *const buf = new char[ value_.exponent() + 3 ];
   value_.toIntegerString( buf );
@@ -245,12 +287,18 @@
 #endif /* ZORBA_WITH_BIG_INTEGER */
 }
 
-Integer const& Integer::zero() {
-  static Integer const i(0);
+TEMPLATE_DECL(T)
+INTEGER_IMPL(T) const& INTEGER_IMPL(T)::zero() {
+  static INTEGER_IMPL(T) const i(0);
   return i;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 
+#ifndef ZORBA_WITH_BIG_INTEGER
+template class IntegerImpl<long long>;
+template class IntegerImpl<unsigned long long>;
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
 } // namespace zorba
 /* vim:set et sw=2 ts=2: */

=== modified file 'src/zorbatypes/integer.h'
--- src/zorbatypes/integer.h	2011-06-29 20:31:44 +0000
+++ src/zorbatypes/integer.h	2011-10-31 21:41:43 +0000
@@ -24,6 +24,7 @@
 #include <zorba/config.h>
 #include "common/common.h"
 
+#include "util/stl_util.h"
 #include "zorbaserialization/archiver.h"
 #include "zorbaserialization/zorba_class_serializer.h"
 
@@ -32,39 +33,53 @@
 #include "zorbatypes_decl.h"
 #include "zstring.h"
 
+#ifdef ZORBA_WITH_BIG_INTEGER
+# define TEMPLATE_DECL(T) /* nothing */
+# define INTEGER_IMPL(T)  IntegerImpl
+#else
+# define TEMPLATE_DECL(T) template<typename T>
+# define INTEGER_IMPL(T)  IntegerImpl<T>
+#endif /* ZORBA_WITH_BIG_INTEGER */
+#define INTEGER_IMPL_LL  INTEGER_IMPL(long long)
+#define INTEGER_IMPL_ULL INTEGER_IMPL(unsigned long long)
+
 namespace zorba {
 
-class Integer;
+TEMPLATE_DECL(T)
+class IntegerImpl;
+
 namespace serialization {
-  void operator&(serialization::Archiver&, zorba::Integer&);
+  TEMPLATE_DECL(T) void operator&( Archiver&, INTEGER_IMPL(T)& );
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 
-// exported for testing only
-class ZORBA_DLL_PUBLIC Integer {
+TEMPLATE_DECL(IntType)
+class IntegerImpl {
 public:
 
   ////////// constructors /////////////////////////////////////////////////////
 
-  Integer( char c );
-  Integer( signed char c );
-  Integer( short n );
-  Integer( int n = 0 );
-  Integer( long n );
-  Integer( long long n );
-  Integer( unsigned char c );
-  Integer( unsigned short n );
-  Integer( unsigned int n );
-  Integer( unsigned long n );
-  Integer( unsigned long long n );
-  Integer( float n );
-  Integer( double n );
-  Integer( Decimal const &d );
-  Integer( Integer const &i );
+  IntegerImpl( char c );
+  IntegerImpl( signed char c );
+  IntegerImpl( short n );
+  IntegerImpl( int n = 0 );
+  IntegerImpl( long n );
+  IntegerImpl( long long n );
+  IntegerImpl( unsigned char c );
+  IntegerImpl( unsigned short n );
+  IntegerImpl( unsigned int n );
+  IntegerImpl( unsigned long n );
+  IntegerImpl( unsigned long long n );
+  IntegerImpl( float n );
+  IntegerImpl( double n );
+  IntegerImpl( Decimal const &d );
+
+  TEMPLATE_DECL(U)
+  IntegerImpl( INTEGER_IMPL(U) const &i );
 
   /**
-   * Constructs an %Integer from a C string.
+   * Constructs an %IntegerImpl from a C string.
    *
    * @param s The null-terminated C string to parse.  Leading and trailing
    * whitespace is ignored.
@@ -73,115 +88,110 @@
    * or overflows the smallest or largest representable integer (only when not
    * compiled with ZORBA_WITH_BIG_INTEGER).
    */
-  Integer( char const *s );
+  IntegerImpl( char const *s );
 
   /**
-   * Constructs an %Integer from a Double.
+   * Constructs an %IntegerImpl from a Double.
    *
    * @param d The Double.
    * @throw std::invalid_argument if \a d is not finite.
    */
-  Integer( Double const &d );
+  IntegerImpl( Double const &d );
 
   /**
-   * Constructs an %Integer from a Float.
+   * Constructs an %IntegerImpl from a Float.
    *
    * @param f The Float.
    * @throw std::invalid_argument if \a f is not finite.
    */
-  Integer( Float const &f );
+  IntegerImpl( Float const &f );
 
   ////////// assignment operators /////////////////////////////////////////////
 
-  Integer& operator=( char c );
-  Integer& operator=( signed char c );
-  Integer& operator=( short n );
-  Integer& operator=( int n );
-  Integer& operator=( long n );
-  Integer& operator=( long long n );
-  Integer& operator=( unsigned char c );
-  Integer& operator=( unsigned short n );
-  Integer& operator=( unsigned int n );
-  Integer& operator=( unsigned long n );
-  Integer& operator=( unsigned long long n );
-  Integer& operator=( float n );
-  Integer& operator=( double n );
-  Integer& operator=( char const *s );
-  Integer& operator=( Decimal const &d );
-  Integer& operator=( Double const &d );
-  Integer& operator=( Float const &f );
-  Integer& operator=( Integer const &i );
+  IntegerImpl& operator=( char c );
+  IntegerImpl& operator=( signed char c );
+  IntegerImpl& operator=( short n );
+  IntegerImpl& operator=( int n );
+  IntegerImpl& operator=( long n );
+  IntegerImpl& operator=( long long n );
+  IntegerImpl& operator=( unsigned char c );
+  IntegerImpl& operator=( unsigned short n );
+  IntegerImpl& operator=( unsigned int n );
+  IntegerImpl& operator=( unsigned long n );
+  IntegerImpl& operator=( unsigned long long n );
+  IntegerImpl& operator=( float n );
+  IntegerImpl& operator=( double n );
+  IntegerImpl& operator=( char const *s );
+  IntegerImpl& operator=( Decimal const &d );
+  IntegerImpl& operator=( Double const &d );
+  IntegerImpl& operator=( Float const &f );
+
+  TEMPLATE_DECL(U)
+  IntegerImpl& operator=( INTEGER_IMPL(U) const &i );
 
   ////////// arithmetic operators /////////////////////////////////////////////
 
-  friend Integer operator+( Integer const &i, Integer const &j );
-  friend Integer operator-( Integer const &i, Integer const &j );
-  friend Integer operator*( Integer const &i, Integer const &j );
-  friend Integer operator/( Integer const &i, Integer const &j );
-  friend Integer operator%( Integer const &i, Integer const &j );
-
-  friend Decimal operator+( Integer const &i, Decimal const &d );
-  friend Decimal operator-( Integer const &i, Decimal const &d );
-  friend Decimal operator*( Integer const &i, Decimal const &d );
-  friend Decimal operator/( Integer const &i, Decimal const &d );
-  friend Decimal operator%( Integer const &i, Decimal const &d );
-
-  friend Decimal operator+( Decimal const &d, Integer const &i );
-  friend Decimal operator-( Decimal const &d, Integer const &i );
-  friend Decimal operator*( Decimal const &d, Integer const &i );
-  friend Decimal operator/( Decimal const &d, Integer const &i );
-  friend Decimal operator%( Decimal const &d, Integer const &i );
-
-  Integer& operator+=( Integer const &i );
-  Integer& operator-=( Integer const &i );
-  Integer& operator*=( Integer const &i );
-  Integer& operator/=( Integer const &i );
-  Integer& operator%=( Integer const &i );
-
-  Integer operator-() const;
-
-  Integer& operator++();
-  Integer  operator++(int);
-  Integer& operator--();
-  Integer  operator--(int);
+#define ZORBA_INTEGER_OP(OP)                                      \
+  TEMPLATE_DECL(T) friend                                         \
+  INTEGER_IMPL(T) operator OP( INTEGER_IMPL(T) const&,            \
+                               INTEGER_IMPL(T) const& );          \
+  TEMPLATE_DECL(T) friend                                         \
+  Decimal operator OP( INTEGER_IMPL(T) const&, Decimal const& );  \
+  TEMPLATE_DECL(T) friend                                         \
+  Decimal operator OP( Decimal const&, INTEGER_IMPL(T) const& )
+
+  ZORBA_INTEGER_OP(+);
+  ZORBA_INTEGER_OP(-);
+  ZORBA_INTEGER_OP(*);
+  ZORBA_INTEGER_OP(/);
+  ZORBA_INTEGER_OP(%);
+#undef ZORBA_INTEGER_OP
+
+  IntegerImpl& operator+=( IntegerImpl const& );
+  IntegerImpl& operator-=( IntegerImpl const& );
+  IntegerImpl& operator*=( IntegerImpl const& );
+  IntegerImpl& operator/=( IntegerImpl const& );
+  IntegerImpl& operator%=( IntegerImpl const& );
+
+  IntegerImpl operator-() const;
+
+  IntegerImpl& operator++();
+  IntegerImpl  operator++(int);
+  IntegerImpl& operator--();
+  IntegerImpl  operator--(int);
 
   ////////// relational operators /////////////////////////////////////////////
 
-  friend bool operator==( Integer const &i, Integer const &j );
-  friend bool operator!=( Integer const &i, Integer const &j );
-  friend bool operator< ( Integer const &i, Integer const &j );
-  friend bool operator<=( Integer const &i, Integer const &j );
-  friend bool operator> ( Integer const &i, Integer const &j );
-  friend bool operator>=( Integer const &i, Integer const &j );
-
-  friend bool operator==( Integer const &i, Decimal const &d );
-  friend bool operator!=( Integer const &i, Decimal const &d );
-  friend bool operator< ( Integer const &i, Decimal const &d );
-  friend bool operator<=( Integer const &i, Decimal const &d );
-  friend bool operator> ( Integer const &i, Decimal const &d );
-  friend bool operator>=( Integer const &i, Decimal const &d );
-
-  friend bool operator==( Decimal const &d, Integer const &i );
-  friend bool operator!=( Decimal const &d, Integer const &i );
-  friend bool operator< ( Decimal const &d, Integer const &i );
-  friend bool operator<=( Decimal const &d, Integer const &i );
-  friend bool operator> ( Decimal const &d, Integer const &i );
-  friend bool operator>=( Decimal const &d, Integer const &i );
+#define ZORBA_INTEGER_OP(OP)                                          \
+  TEMPLATE_DECL(T) friend                                             \
+  bool operator OP( INTEGER_IMPL(T) const&, INTEGER_IMPL(T) const& ); \
+  TEMPLATE_DECL(T) friend                                             \
+  bool operator OP( INTEGER_IMPL(T) const&, Decimal const& );         \
+  TEMPLATE_DECL(T) friend                                             \
+  bool operator OP( Decimal const&, INTEGER_IMPL(T) const& )
+
+  ZORBA_INTEGER_OP(==);
+  ZORBA_INTEGER_OP(!=);
+  ZORBA_INTEGER_OP(< );
+  ZORBA_INTEGER_OP(<=);
+  ZORBA_INTEGER_OP(> );
+  ZORBA_INTEGER_OP(>=);
+#undef ZORBA_INTEGER_OP
 
   ////////// math functions ///////////////////////////////////////////////////
 
-  Double pow( Integer const &power ) const;
-  Integer round( Integer const &precision ) const;
-  Integer roundHalfToEven( Integer const &precision ) const;
+  Double pow( IntegerImpl const &power ) const;
+  IntegerImpl round( IntegerImpl const &precision ) const;
+  IntegerImpl roundHalfToEven( IntegerImpl const &precision ) const;
 
   ////////// miscellaneous ////////////////////////////////////////////////////
 
-  int compare( Integer const &i ) const;
+  int compare( IntegerImpl const& ) const;
   uint32_t hash() const;
   int sign() const;
   zstring toString() const;
-  static Integer const& one();
-  static Integer const& zero();
+  static IntegerImpl const& one();
+  static IntegerImpl const& zero();
 
   /////////////////////////////////////////////////////////////////////////////
 
@@ -189,13 +199,13 @@
 #ifdef ZORBA_WITH_BIG_INTEGER
   typedef MAPM value_type;
 #else
-  typedef long long value_type;
+  typedef IntType value_type;
 #endif /* ZORBA_WITH_BIG_INTEGER */
 
   value_type value_;
 
 #ifdef ZORBA_WITH_BIG_INTEGER
-  Integer( value_type const &v ) : value_( v ) { }
+  IntegerImpl( value_type const &v ) : value_( v ) { }
 #endif /* ZORBA_WITH_BIG_INTEGER */
 
   static value_type ftoi( double d ) {
@@ -227,58 +237,96 @@
   friend class Decimal;
   template<typename T> friend class FloatImpl;
 
-  friend xs_int to_xs_int( Integer const& );
-  friend xs_long to_xs_long( Integer const& );
-  friend xs_unsignedInt to_xs_unsignedInt( Integer const& );
-  friend xs_unsignedLong to_xs_unsignedLong( Integer const& );
-
-  friend void serialization::operator&(serialization::Archiver&, Integer&);
+#ifndef ZORBA_WITH_BIG_INTEGER
+  template<typename U> friend class IntegerImpl;
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
+  friend xs_int to_xs_int( INTEGER_IMPL_LL const& );
+  friend xs_long to_xs_long( INTEGER_IMPL_LL const& );
+  friend xs_unsignedInt to_xs_unsignedInt( INTEGER_IMPL_LL const& );
+  friend xs_unsignedLong to_xs_unsignedLong( INTEGER_IMPL_LL const& );
+
+  TEMPLATE_DECL(T) friend
+  void serialization::operator&( serialization::Archiver&, INTEGER_IMPL(T)& );
 };
 
+typedef INTEGER_IMPL_LL Integer;
+typedef INTEGER_IMPL_ULL UInteger;
+
 ////////// constructors ///////////////////////////////////////////////////////
 
-inline Integer::Integer( char c ) : value_( static_cast<long>( c ) ) {
-}
-
-inline Integer::Integer( signed char c ) : value_( static_cast<long>( c ) ) {
-}
-
-inline Integer::Integer( short n ) : value_( static_cast<long>( n ) ) {
-}
-
-inline Integer::Integer( int n ) : value_( static_cast<long>( n ) ) {
-}
-
-inline Integer::Integer( long n ) : value_( n ) {
-}
-
-#ifndef ZORBA_WITH_BIG_INTEGER
-inline Integer::Integer( long long n ) : value_( n ) {
-}
-#endif /* ZORBA_WITH_BIG_INTEGER */
-
-inline Integer::Integer( unsigned char c ) : value_( static_cast<long>( c ) ) {
-}
-
-inline Integer::Integer( unsigned short n ) : value_( static_cast<long>( n ) ) {
-}
-
-inline Integer::Integer( unsigned int n ) : value_( static_cast<long>( n ) ) {
-}
-
-#ifndef ZORBA_WITH_BIG_INTEGER
-inline Integer::Integer( unsigned long n ) :
-  value_( static_cast<value_type>( n ) )
-{
-}
-
-inline Integer::Integer( unsigned long long n ) :
-  value_( static_cast<value_type>( n ) )
-{
-}
-#endif /* ZORBA_WITH_BIG_INTEGER */
-
-inline Integer::Integer( float n ) :
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( char c ) :
+  value_( static_cast<long>( c ) )
+{
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( signed char c ) :
+  value_( static_cast<long>( c ) )
+{
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( short n ) :
+  value_( static_cast<long>( n ) )
+{
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( int n ) :
+  value_( static_cast<long>( n ) )
+{
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( long n ) :
+  value_( n )
+{
+}
+
+#ifndef ZORBA_WITH_BIG_INTEGER
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( long long n ) :
+  value_( n )
+{
+}
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( unsigned char c ) :
+  value_( static_cast<long>( c ) )
+{
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( unsigned short n ) :
+  value_( static_cast<long>( n ) )
+{
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( unsigned int n ) :
+  value_( static_cast<long>( n ) )
+{
+}
+
+#ifndef ZORBA_WITH_BIG_INTEGER
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( unsigned long n ) :
+  value_( static_cast<value_type>( n ) )
+{
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( unsigned long long n ) :
+  value_( static_cast<value_type>( n ) )
+{
+}
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( float n ) :
 #ifdef ZORBA_WITH_BIG_INTEGER
   value_( static_cast<double>( n ) )
 #else
@@ -287,7 +335,8 @@
 {
 }
 
-inline Integer::Integer( double n ) :
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( double n ) :
 #ifdef ZORBA_WITH_BIG_INTEGER
   value_( n )
 #else
@@ -296,240 +345,252 @@
 {
 }
 
-inline Integer::Integer( char const *s ) {
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)::IntegerImpl( char const *s ) {
   parse( s );
 }
 
-inline Integer::Integer( Integer const &i ) :
+TEMPLATE_DECL(T)
+TEMPLATE_DECL(U)
+inline INTEGER_IMPL(T)::IntegerImpl( INTEGER_IMPL(U) const &i ) :
   value_( i.value_ )
 {
 }
 
 ////////// assignment operators ///////////////////////////////////////////////
 
-inline Integer& Integer::operator=( char c ) {
-  value_ = static_cast<long>( c );
-  return *this;
-}
-
-inline Integer& Integer::operator=( signed char c ) {
-  value_ = static_cast<long>( c );
-  return *this;
-}
-
-inline Integer& Integer::operator=( short n ) {
-  value_ = static_cast<long>( n );
-  return *this;
-}
-
-inline Integer& Integer::operator=( int n ) {
-  value_ = static_cast<long>( n );
-  return *this;
-}
-
-inline Integer& Integer::operator=( long n ) {
-  value_ = n;
-  return *this;
-}
-
-#ifndef ZORBA_WITH_BIG_INTEGER
-inline Integer& Integer::operator=( long long n ) {
-  value_ = n;
-  return *this;
-}
-#endif /* ZORBA_WITH_BIG_INTEGER */
-
-inline Integer& Integer::operator=( unsigned char c ) {
-  value_ = static_cast<long>( c );
-  return *this;
-}
-
-inline Integer& Integer::operator=( unsigned short n ) {
-  value_ = static_cast<long>( n );
-  return *this;
-}
-
-inline Integer& Integer::operator=( unsigned int n ) {
-  value_ = static_cast<long>( n );
-  return *this;
-}
-
-#ifndef ZORBA_WITH_BIG_INTEGER
-inline Integer& Integer::operator=( unsigned long n ) {
-  value_ = static_cast<long>( n );
-  return *this;
-}
-
-inline Integer& Integer::operator=( unsigned long long n ) {
-  value_ = n;
-  return *this;
-}
-#endif /* ZORBA_WITH_BIG_INTEGER */
-
-inline Integer& Integer::operator=( float n ) {
-  value_ = static_cast<long>( n );
-  return *this;
-}
-
-inline Integer& Integer::operator=( double n ) {
-  value_ = static_cast<long>( n );
-  return *this;
-}
-
-inline Integer& Integer::operator=( char const *s ) {
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( char c ) {
+  value_ = static_cast<long>( c );
+  return *this;
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( signed char c ) {
+  value_ = static_cast<long>( c );
+  return *this;
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( short n ) {
+  value_ = static_cast<long>( n );
+  return *this;
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( int n ) {
+  value_ = static_cast<long>( n );
+  return *this;
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( long n ) {
+  value_ = n;
+  return *this;
+}
+
+#ifndef ZORBA_WITH_BIG_INTEGER
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( long long n ) {
+  value_ = n;
+  return *this;
+}
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( unsigned char c ) {
+  value_ = static_cast<long>( c );
+  return *this;
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( unsigned short n ) {
+  value_ = static_cast<long>( n );
+  return *this;
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( unsigned int n ) {
+  value_ = static_cast<long>( n );
+  return *this;
+}
+
+#ifndef ZORBA_WITH_BIG_INTEGER
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( unsigned long n ) {
+  value_ = static_cast<long>( n );
+  return *this;
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( unsigned long long n ) {
+  value_ = n;
+  return *this;
+}
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( float n ) {
+  value_ = static_cast<long>( n );
+  return *this;
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( double n ) {
+  value_ = static_cast<long>( n );
+  return *this;
+}
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( char const *s ) {
   parse( s );
   return *this;
 }
 
-inline Integer& Integer::operator=( Integer const &i ) {
+TEMPLATE_DECL(T)
+TEMPLATE_DECL(U)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator=( INTEGER_IMPL(U) const &i ) {
   value_ = i.value_;
   return *this;
 }
 
 ////////// arithmetic operators ///////////////////////////////////////////////
 
-inline Integer operator+( Integer const &i, Integer const &j ) {
-  return i.value_ + j.value_;
-}
-
-inline Integer operator-( Integer const &i, Integer const &j ) {
-  return i.value_ - j.value_;
-}
-
-inline Integer operator*( Integer const &i, Integer const &j ) {
-  return i.value_ * j.value_;
-}
-
-inline Integer operator/( Integer const &i, Integer const &j ) {
-  return Integer::ftoi( i.value_ / j.value_ );
-}
-
-inline Integer operator%( Integer const &i, Integer const &j ) {
-  return i.value_ % j.value_;
-}
-
-inline Integer& Integer::operator+=( Integer const &i ) {
-  value_ += i.value_;
-  return *this;
-}
-
-inline Integer& Integer::operator-=( Integer const &i ) {
-  value_ -= i.value_;
-  return *this;
-}
-
-inline Integer& Integer::operator*=( Integer const &i ) {
-  value_ *= i.value_;
-  return *this;
-}
-
-inline Integer& Integer::operator/=( Integer const &i ) {
+#define ZORBA_INTEGER_OP(OP)                                \
+  TEMPLATE_DECL(T) inline                                   \
+  INTEGER_IMPL(T) operator OP( INTEGER_IMPL(T) const &i,    \
+                               INTEGER_IMPL(T) const &j ) { \
+    return i.value_ OP j.value_;                            \
+  }
+
+ZORBA_INTEGER_OP(+)
+ZORBA_INTEGER_OP(-)
+ZORBA_INTEGER_OP(*)
+ZORBA_INTEGER_OP(%)
+#undef ZORBA_INTEGER_OP
+
+TEMPLATE_DECL(T) inline
+INTEGER_IMPL(T) operator/( INTEGER_IMPL(T) const &i, INTEGER_IMPL(T) const &j ) {
+  return INTEGER_IMPL(T)::ftoi( i.value_ / j.value_ );
+}
+
+#define ZORBA_INTEGER_OP(OP)                                              \
+  TEMPLATE_DECL(T) inline                                                 \
+  INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator OP( IntegerImpl const &i ) { \
+    value_ OP i.value_;                                                   \
+    return *this;                                                         \
+  }
+
+ZORBA_INTEGER_OP(+=)
+ZORBA_INTEGER_OP(-=)
+ZORBA_INTEGER_OP(*=)
+ZORBA_INTEGER_OP(%=)
+#undef ZORBA_INTEGER_OP
+
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator/=( IntegerImpl const &i ) {
   value_ = ftoi( value_ / i.value_ );
   return *this;
 }
 
-inline Integer& Integer::operator%=( Integer const &i ) {
-  value_ %= i.value_;
-  return *this;
-}
-
-inline Integer Integer::operator-() const {
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T) INTEGER_IMPL(T)::operator-() const {
   return -value_;
 }
 
-inline Integer& Integer::operator++() {
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator++() {
   ++value_;
   return *this;
 }
 
-inline Integer Integer::operator++(int) {
-  Integer const result( *this );
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T) INTEGER_IMPL(T)::operator++(int) {
+  INTEGER_IMPL(T) const result( *this );
   ++value_;
   return result;
 }
 
-inline Integer& Integer::operator--() {
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T)& INTEGER_IMPL(T)::operator--() {
   --value_;
   return *this;
 }
 
-inline Integer Integer::operator--(int) {
-  Integer const result( *this );
+TEMPLATE_DECL(T)
+inline INTEGER_IMPL(T) INTEGER_IMPL(T)::operator--(int) {
+  INTEGER_IMPL(T) const result( *this );
   --value_;
   return result;
 }
 
 ////////// relational operators ///////////////////////////////////////////////
 
-inline bool operator==( Integer const &i, Integer const &j ) {
-  return i.value_ == j.value_;
-}
-
-inline bool operator!=( Integer const &i, Integer const &j ) {
-  return i.value_ != j.value_;
-}
-
-inline bool operator<( Integer const &i, Integer const &j ) {
-  return i.value_ < j.value_;
-}
-
-inline bool operator<=( Integer const &i, Integer const &j ) {
-  return i.value_ <= j.value_;
-}
-
-inline bool operator>( Integer const &i, Integer const &j ) {
-  return i.value_ > j.value_;
-}
-
-inline bool operator>=( Integer const &i, Integer const &j ) {
-  return i.value_ >= j.value_;
-}
+#define ZORBA_INTEGER_OP(OP)                                                \
+  TEMPLATE_DECL(T) inline                                                   \
+  bool operator OP( INTEGER_IMPL(T) const &i, INTEGER_IMPL(T) const &j ) {  \
+    return i.value_ OP j.value_;                                            \
+  }
+
+ZORBA_INTEGER_OP(==)
+ZORBA_INTEGER_OP(!=)
+ZORBA_INTEGER_OP(< )
+ZORBA_INTEGER_OP(<=)
+ZORBA_INTEGER_OP(> )
+ZORBA_INTEGER_OP(>=)
+#undef ZORBA_INTEGER_OP
 
 ////////// miscellaneous //////////////////////////////////////////////////////
 
 #ifdef ZORBA_WITH_BIG_INTEGER
 
-inline int Integer::compare( Integer const &i ) const {
+inline int IntegerImpl::compare( IntegerImpl const &i ) const {
   return value_.compare( i.value_ );
 }
 
-inline int Integer::sign() const {
+inline int IntegerImpl::sign() const {
   return value_.sign();
 }
 
 #else
 
-inline int Integer::compare( Integer const &i ) const {
-  //
-  // Note that we can't return the difference directly since it will be
-  // truncated if it's ether > max(int) or < min(int) yielding a wrong result.
-  //
-  value_type const temp = value_ - i.value_;
-  return temp < 0 ? -1 : temp > 0 ? 1 : 0;
+template<typename IntType>
+inline int IntegerImpl<IntType>::compare( IntegerImpl const &i ) const {
+  return value_ < i.value_ ? -1 : value_ > i.value_ ? 1 : 0;
 }
 
-inline uint32_t Integer::hash() const {
+template<typename IntType>
+inline uint32_t IntegerImpl<IntType>::hash() const {
   return static_cast<uint32_t>( value_ );
 }
 
-inline bool Integer::is_long() const {
+template<typename IntType>
+inline bool IntegerImpl<IntType>::is_long() const {
   return  value_ >= std::numeric_limits<long>::min() &&
           value_ <= std::numeric_limits<long>::max();
 }
 
-inline int Integer::sign() const {
-  return value_ < 0 ? -1 : value_ > 0 ? 1 : 0;
+template<typename IntType>
+inline int IntegerImpl<IntType>::sign() const {
+  return ztd::lt0( value_ ) ? -1 : value_ > 0 ? 1 : 0;
 }
 
 #endif /* ZORBA_WITH_BIG_INTEGER */
 
-inline std::ostream& operator<<( std::ostream &os, Integer const &i ) {
+TEMPLATE_DECL(T)
+inline std::ostream& operator<<( std::ostream &os, INTEGER_IMPL(T) const &i ) {
   return os << i.toString();
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 
 } // namespace zorba
+
+#undef TEMPLATE_DECL
+#undef INTEGER_IMPL
+#undef INTEGER_IMPL_LL
+#undef INTEGER_IMPL_ULL
+
 #endif // ZORBA_INTEGER_H
 /*
  * Local variables:

=== modified file 'src/zorbatypes/numconversions.cpp'
--- src/zorbatypes/numconversions.cpp	2011-06-29 20:31:44 +0000
+++ src/zorbatypes/numconversions.cpp	2011-10-31 21:41:43 +0000
@@ -33,7 +33,7 @@
   zstring const temp( i.toString() );
   return ztd::aton<xs_int>( temp.c_str() );
 #else
-  return static_cast<xs_int>(i.value_);
+  return static_cast<xs_int>( i.value_ );
 #endif /* ZORBA_WITH_BIG_INTEGER */
 }
 
@@ -56,6 +56,13 @@
 #endif /* ZORBA_WITH_BIG_INTEGER */
 }
 
+#ifndef ZORBA_WITH_BIG_INTEGER
+xs_long to_xs_long( xs_nonNegativeInteger const &i ) {
+  zstring const temp( i.toString() );
+  return ztd::aton<xs_long>( temp.c_str() );
+}
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
 xs_unsignedInt to_xs_unsignedInt( xs_integer const &i ) {
 #ifdef ZORBA_WITH_BIG_INTEGER
   zstring const temp( i.toString() );

=== modified file 'src/zorbatypes/numconversions.h'
--- src/zorbatypes/numconversions.h	2011-06-29 20:31:44 +0000
+++ src/zorbatypes/numconversions.h	2011-10-31 21:41:43 +0000
@@ -64,6 +64,10 @@
  */
 xs_long to_xs_long( xs_integer const &i );
 
+#ifndef ZORBA_WITH_BIG_INTEGER
+xs_long to_xs_long( xs_nonNegativeInteger const &i );
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
 /**
  * Converts an \c xs:integer value to an \c xs:unsignedInt.
  *

=== modified file 'src/zorbatypes/schema_types.h'
--- src/zorbatypes/schema_types.h	2011-06-24 13:38:42 +0000
+++ src/zorbatypes/schema_types.h	2011-10-31 21:41:43 +0000
@@ -46,8 +46,12 @@
 typedef DateTime  xs_gYearMonth;
 typedef Base16    xs_hexBinary;
 typedef Integer   xs_integer;
+typedef Integer   xs_negativeInteger;     // this isn't quite right
+typedef UInteger  xs_nonNegativeInteger;  // i.e., "unsigned"
+typedef Integer   xs_nonPositiveInteger;  // this isn't quite right either
+typedef UInteger  xs_positiveInteger;
 typedef DateTime  xs_time;
-typedef Integer   xs_uinteger;
+typedef UInteger  xs_uinteger;            // old, deprecated name
 typedef Duration  xs_yearMonthDuration;
 
 ///////////////////////////////////////////////////////////////////////////////

=== modified file 'src/zorbatypes/zorbatypes_decl.h'
--- src/zorbatypes/zorbatypes_decl.h	2011-06-14 17:26:33 +0000
+++ src/zorbatypes/zorbatypes_decl.h	2011-10-31 21:41:43 +0000
@@ -18,11 +18,23 @@
 #ifndef ZORBA_ZORBATYPES_DECL_H
 #define ZORBA_ZORBATYPES_DECL_H
 
+#include <zorba/config.h>
+
 namespace zorba 
 {
   /* numerics */
   class Decimal;
-  class Integer;
+
+#ifdef ZORBA_WITH_BIG_INTEGER
+  class IntegerImpl;
+  typedef IntegerImpl Integer;
+  typedef IntegerImpl UInteger;
+#else
+  template<typename T> class IntegerImpl;
+  typedef IntegerImpl<long long> Integer;
+  typedef IntegerImpl<unsigned long long> UInteger;
+#endif /* ZORBA_WITH_BIG_INTEGER */
+
   template<typename T> class FloatImpl;
   typedef FloatImpl<double> Double;
   typedef FloatImpl<float>  Float;

=== modified file 'test/rbkt/Queries/CMakeLists.txt'
--- test/rbkt/Queries/CMakeLists.txt	2011-10-13 12:46:50 +0000
+++ test/rbkt/Queries/CMakeLists.txt	2011-10-31 21:41:43 +0000
@@ -213,14 +213,6 @@
   EXPECTED_FAILURE(test/rbkt/w3c_testsuite/XQuery/PathExpr/Steps/Steps-leading-lone-slash-8a 3408285)
   EXPECTED_FAILURE(test/rbkt/w3c_testsuite/XQuery/Functions/QNameFunc/NamespaceURIForPrefixFunc/K2-NamespaceURIForPrefixFunc-2 872732)
 
-  IF(NOT ZORBA_WITH_BIG_INTEGER)
-    # These tests fail due to integer overflow.
-    EXPECTED_FAILURE(test/rbkt/w3c_testsuite/XQuery/Operators/CompExpr/ValComp/NumericComp/NumericGT/K2-NumericGT-1 3323548)
-    EXPECTED_FAILURE(test/rbkt/w3c_testsuite/XQuery/Operators/CompExpr/ValComp/NumericComp/NumericGT/K2-NumericGT-2 3323548)
-    EXPECTED_FAILURE(test/rbkt/w3c_testsuite/XQuery/Operators/CompExpr/ValComp/NumericComp/NumericLT/K2-NumericLT-1 3323548)
-    EXPECTED_FAILURE(test/rbkt/w3c_testsuite/XQuery/Operators/CompExpr/ValComp/NumericComp/NumericLT/K2-NumericLT-2 3323548)
-  ENDIF(NOT ZORBA_WITH_BIG_INTEGER)
-
   IF(ZORBA_TEST_XQUERYX)
 
     #w3c bug

=== modified file 'test/rbkt/Queries/w3c_known_failures.txt'
--- test/rbkt/Queries/w3c_known_failures.txt	2011-10-13 12:46:50 +0000
+++ test/rbkt/Queries/w3c_known_failures.txt	2011-10-31 21:41:43 +0000
@@ -116,7 +116,3 @@
 test/rbkt/w3c_testsuite/XQuery/StaticTyping/STFLWORExpr/ST-PITest-01
 test/rbkt/w3c_testsuite/XQuery/StaticTyping/STFunctions/ST-Data001
 test/rbkt/w3c_testsuite/XQuery/SchemaImport/SchemaImportProlog/modules-schema-context
-test/rbkt/w3c_testsuite/XQuery/Operators/CompExpr/ValComp/NumericComp/NumericLT/K2-NumericLT-2
-test/rbkt/w3c_testsuite/XQuery/Operators/CompExpr/ValComp/NumericComp/NumericLT/K2-NumericLT-1
-test/rbkt/w3c_testsuite/XQuery/Operators/CompExpr/ValComp/NumericComp/NumericGT/K2-NumericGT-2
-test/rbkt/w3c_testsuite/XQuery/Operators/CompExpr/ValComp/NumericComp/NumericGT/K2-NumericGT-1


Follow ups