libpbcd: Packed Binary-Coded Decimal for data portability
SourceForge.net Logo



Current release:

libpbcd 0.2.0

  January 27, 2008



Overview

Libpbcd is a Packed Binary-Coded Decimal library for cross-platform data portability. It supports 16, 32 and 64 bit integers as well as 32 and 64 bit floating point numbers. The code is written entirely in ANSI C99. The library does not support arbitrary precision arithmetic and will not solve any floating point precision problems. These problems are better solved by  gmplib,  the fastest bignum library on the planet. The source code is distributed under the terms of the GNU Lesser General Public License version 2.1.

Where is the source?

Here.

How do I contact the author?

Send mail to: stormjib at freemail dot gr

How libpbcd works?

Encoding every digit in 4 bits. The leftmost bit of the first byte is used to keep the sign. The floating point numbers are encoded as fixed point decimals. The precision for the floats (32 bits) is six digits and for the doubles (64 bits) is ten digits.   

Header files

pbcd.h

Defined types

pbcd_i16
pbcd_u16
pbcd_i32
pbcd_u32
pbcd_i64
pbcd_u64
pbcd_f32
pbcd_f64
Packed decimal capable to hold a 16 bit integer
Packed decimal capable to hold a 16 bit unsigned integer
Packed decimal capable to hold a 32 bit integer
Packed decimal capable to hold a 32 bit unsigned integer
Packed decimal capable to hold a 64 bit integer
Packed decimal capable to hold a 64 bit unsigned integer
Packed decimal capable to hold a 32 bit float with 6 digits precision
Packed decimal capable to hold a 32 bit float (double) with 10 digits precision

Defined macros

Library version macros
PBCD_VERSION_MAJOR   
PBCD_VERSION_MINOR   
PBCD_VERSION_PATCH   

Bytes used per type
PBCDI16_SIZE       
PBCDI32_SIZE       
PBCDI64_SIZE       
PBCDF32_SIZE       
PBCDF64_SIZE       

Bytes occupied in memory per type
PBCDI16_MEM          
PBCDI32_MEM           
PBCDI64_MEM           
PBCDF32_MEM           
PBCDF64_MEM           

Precision digits
PBCDF32_PREC       
PBCDF64_PREC

Functions

Convert standard type to packed decimal
void  int16_pbcd (int16_t number,  pbcd_i16  pbcd)
void  uint16_pbcd (uint16_t number,  pbcd_u16  pbcd)
void  int32_pbcd (int32_t number,  pbcd_i32  pbcd)
void  uint32_pbcd (uint32_t number,  pbcd_u32  pbcd)
void  int64_pbcd (int64_t number,  pbcd_i64  pbcd)
void  uint64_pbcd (uint64_t number,  pbcd_u64  pbcd)
void  float_pbcd (float number,  pbcd_f32  pbcd)
void  double_pbcd (double number,  pbcd_f64  pbcd)

Convert packed decimal to standard type
int16_t  pbcd_int16 (pbcd_i16 pbcd)
uint16_t  pbcd_uint16 (pbcd_u16 pbcd)
int32_t  pbcd_int32 (pbcd_i32 pbcd)
uint32_t  pbcd_uint32 (pbcd_u32 pbcd)
int64_t  pbcd_int64 (pbcd_i64 pbcd)
uint64_t  pbcd_uint64 (pbcd_u64 pbcd)
float  pbcd_float (pbcd_f32 pbcd)
double  pbcd_double (pbcd_f64 pbcd)

Compare packed decimals
int pbcdcmp (const void *p1, const void *p2,  size_t  n)

How to compile and install libpbcd

There is no configure script yet. Makefile understands the following environment variables:
CFLAGS  to pass options to the compiler
CPPFLAGS to pass option to the preprocessor
LDFLAGS to pass linking options to the compiler
PREFIX to define the install prefix of the static library, the shared library and the header file

example:
$ make clean
$ CFLAGS="-fPIC -O3" LDFLAGS="-shared" make
$ PREFIX="/usr" make install

Makefile always use  cc to compile the source, ar to create the static library (libpbcd.a) and cc to create the shared library (libpbcd.so).
After compilation the Makefile runs a test program to check the correct functionality of the library.
Only GNU Make 3.79 or later can understand the Makefile of libpbcd.

WARNING: Broken compilers will cause failure of the test program.


CHANGE LOG

20080127 - v.0.2.0
* pbcdcmp function added


20071210 - v.0.1.0
* The initial release of the source code