kusano 7d535a
/* align.c -- test alignment (important for 16-bit systems)
kusano 7d535a
kusano 7d535a
   This file is part of the LZO real-time data compression library.
kusano 7d535a
kusano 7d535a
   Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   All Rights Reserved.
kusano 7d535a
kusano 7d535a
   The LZO library is free software; you can redistribute it and/or
kusano 7d535a
   modify it under the terms of the GNU General Public License as
kusano 7d535a
   published by the Free Software Foundation; either version 2 of
kusano 7d535a
   the License, or (at your option) any later version.
kusano 7d535a
kusano 7d535a
   The LZO library is distributed in the hope that it will be useful,
kusano 7d535a
   but WITHOUT ANY WARRANTY; without even the implied warranty of
kusano 7d535a
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
kusano 7d535a
   GNU General Public License for more details.
kusano 7d535a
kusano 7d535a
   You should have received a copy of the GNU General Public License
kusano 7d535a
   along with the LZO library; see the file COPYING.
kusano 7d535a
   If not, write to the Free Software Foundation, Inc.,
kusano 7d535a
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
kusano 7d535a
kusano 7d535a
   Markus F.X.J. Oberhumer
kusano 7d535a
   <markus@oberhumer.com></markus@oberhumer.com>
kusano 7d535a
   http://www.oberhumer.com/opensource/lzo/
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
#if 0
kusano 7d535a
#include "src/lzo_conf.h"
kusano 7d535a
#include "src/lzo_ptr.h"
kusano 7d535a
#endif
kusano 7d535a
#include "lzo/lzoconf.h"
kusano 7d535a
kusano 7d535a
/* utility layer */
kusano 7d535a
#define WANT_LZO_MALLOC 1
kusano 7d535a
#include "examples/portab.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
int opt_verbose = 0;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*************************************************************************
kusano 7d535a
//
kusano 7d535a
**************************************************************************/
kusano 7d535a
kusano 7d535a
long align_test(lzo_bytep block, lzo_uint len, lzo_uint step)
kusano 7d535a
{
kusano 7d535a
    lzo_bytep b1 = block;
kusano 7d535a
    lzo_bytep b2 = block;
kusano 7d535a
    lzo_bytep k1 = NULL;
kusano 7d535a
    lzo_bytep k2 = NULL;
kusano 7d535a
    lzo_bytep k;
kusano 7d535a
    lzo_bytep x;
kusano 7d535a
    lzo_uint offset = 0;
kusano 7d535a
    long i = 0;
kusano 7d535a
kusano 7d535a
    assert(step > 0);
kusano 7d535a
    assert(step <= 65536L);
kusano 7d535a
    assert((step & (step - 1)) == 0);
kusano 7d535a
kusano 7d535a
    for (offset = step; offset < len; offset += step)
kusano 7d535a
    {
kusano 7d535a
        k1 = LZO_PTR_ALIGN_UP(b1+1,step);
kusano 7d535a
        k2 = b2 + offset;
kusano 7d535a
        if (k1 != k2)
kusano 7d535a
        {
kusano 7d535a
            printf("error 1: i %ld step %ld offset %ld: "
kusano 7d535a
                    "%p (%ld) %p (%ld)\n",
kusano 7d535a
                    i, (long) step, (long) offset,
kusano 7d535a
                    k1, (long) (k1 - block),
kusano 7d535a
                    k2, (long) (k2 - block));
kusano 7d535a
            return 0;
kusano 7d535a
        }
kusano 7d535a
        if (k1 - step != b1)
kusano 7d535a
        {
kusano 7d535a
            printf("error 2: i %ld step %ld offset %ld: "
kusano 7d535a
                    "%p (%ld) %p (%ld)\n",
kusano 7d535a
                    i, (long) step, (long) offset,
kusano 7d535a
                    b1, (long) (b1 - block),
kusano 7d535a
                    k1, (long) (k1 - block));
kusano 7d535a
            return 0;
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        assert(k1 > b1);
kusano 7d535a
        assert(k2 > b2);
kusano 7d535a
        assert((lzo_uint)(k2 - b2) == offset);
kusano 7d535a
        assert(k1 - offset == b2);
kusano 7d535a
#if defined(PTR_ALIGNED_4)
kusano 7d535a
        if (step == 4)
kusano 7d535a
        {
kusano 7d535a
            assert(PTR_ALIGNED_4(k1));
kusano 7d535a
            assert(PTR_ALIGNED_4(k2));
kusano 7d535a
            assert(PTR_ALIGNED2_4(k1,k2));
kusano 7d535a
        }
kusano 7d535a
#endif
kusano 7d535a
#if defined(PTR_ALIGNED_8)
kusano 7d535a
        if (step == 8)
kusano 7d535a
        {
kusano 7d535a
            assert(PTR_ALIGNED_8(k1));
kusano 7d535a
            assert(PTR_ALIGNED_8(k2));
kusano 7d535a
            assert(PTR_ALIGNED2_8(k1,k2));
kusano 7d535a
        }
kusano 7d535a
#endif
kusano 7d535a
#if defined(PTR_LINEAR)
kusano 7d535a
        assert((PTR_LINEAR(k1) & (step-1)) == 0);
kusano 7d535a
        assert((PTR_LINEAR(k2) & (step-1)) == 0);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
        for (k = b1 + 1; k <= k1; k++)
kusano 7d535a
        {
kusano 7d535a
            x = LZO_PTR_ALIGN_UP(k,step);
kusano 7d535a
            if (x != k1)
kusano 7d535a
            {
kusano 7d535a
                printf("error 3: base: %p %p %p  i %ld step %ld offset %ld: "
kusano 7d535a
                        "%p (%ld) %p (%ld) %p (%ld)\n",
kusano 7d535a
                        block, b1, b2,
kusano 7d535a
                        i, (long) step, (long) offset,
kusano 7d535a
                        k1, (long) (k1 - block),
kusano 7d535a
                        k, (long) (k - block),
kusano 7d535a
                        x, (long) (x - block));
kusano 7d535a
                return 0;
kusano 7d535a
            }
kusano 7d535a
        }
kusano 7d535a
kusano 7d535a
        b1 = k1;
kusano 7d535a
        i++;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    return i;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*************************************************************************
kusano 7d535a
//
kusano 7d535a
**************************************************************************/
kusano 7d535a
kusano 7d535a
#define BLOCK_LEN   (128*1024L)
kusano 7d535a
kusano 7d535a
int main(int argc, char *argv[])
kusano 7d535a
{
kusano 7d535a
    lzo_bytep buf;
kusano 7d535a
    lzo_uint step;
kusano 7d535a
kusano 7d535a
    if (argc >= 2 && strcmp(argv[1],"-v") == 0)
kusano 7d535a
        opt_verbose = 1;
kusano 7d535a
kusano 7d535a
    if (lzo_init() != LZO_E_OK)
kusano 7d535a
    {
kusano 7d535a
        printf("lzo_init() failed !!!\n");
kusano 7d535a
        return 3;
kusano 7d535a
    }
kusano 7d535a
    buf = (lzo_bytep) lzo_malloc(2*BLOCK_LEN + 256);
kusano 7d535a
    if (buf == NULL)
kusano 7d535a
    {
kusano 7d535a
        printf("out of memory\n");
kusano 7d535a
        return 2;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) buf);
kusano 7d535a
kusano 7d535a
    for (step = 1; step <= 65536L; step *= 2)
kusano 7d535a
    {
kusano 7d535a
        lzo_bytep block = buf;
kusano 7d535a
        long n;
kusano 7d535a
        unsigned gap;
kusano 7d535a
kusano 7d535a
        gap = __lzo_align_gap(block,step);
kusano 7d535a
        block = LZO_PTR_ALIGN_UP(block,step);
kusano 7d535a
        if (opt_verbose >= 1)
kusano 7d535a
            printf("STEP %5ld: GAP: %5lu  %p %p %5ld\n",
kusano 7d535a
                    (long) step, (long) gap, buf, block,
kusano 7d535a
                    (long) (block - buf));
kusano 7d535a
        n = align_test(block,BLOCK_LEN,step);
kusano 7d535a
        if (n == 0)
kusano 7d535a
            return 1;
kusano 7d535a
        if ((n + 1) * step != BLOCK_LEN)
kusano 7d535a
        {
kusano 7d535a
            printf("error 4: %ld %ld\n",(long)step,n);
kusano 7d535a
            return 1;
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    lzo_free(buf);
kusano 7d535a
    printf("Alignment test passed.\n");
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
vi:ts=4:et
kusano 7d535a
*/
kusano 7d535a