Blame src/libFLAC/bitmath.c

Packit 8f7830
/* libFLAC - Free Lossless Audio Codec library
Packit 8f7830
 * Copyright (C) 2001-2009  Josh Coalson
Packit 8f7830
 * Copyright (C) 2011-2016  Xiph.Org Foundation
Packit 8f7830
 *
Packit 8f7830
 * Redistribution and use in source and binary forms, with or without
Packit 8f7830
 * modification, are permitted provided that the following conditions
Packit 8f7830
 * are met:
Packit 8f7830
 *
Packit 8f7830
 * - Redistributions of source code must retain the above copyright
Packit 8f7830
 * notice, this list of conditions and the following disclaimer.
Packit 8f7830
 *
Packit 8f7830
 * - Redistributions in binary form must reproduce the above copyright
Packit 8f7830
 * notice, this list of conditions and the following disclaimer in the
Packit 8f7830
 * documentation and/or other materials provided with the distribution.
Packit 8f7830
 *
Packit 8f7830
 * - Neither the name of the Xiph.org Foundation nor the names of its
Packit 8f7830
 * contributors may be used to endorse or promote products derived from
Packit 8f7830
 * this software without specific prior written permission.
Packit 8f7830
 *
Packit 8f7830
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 8f7830
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 8f7830
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 8f7830
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
Packit 8f7830
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Packit 8f7830
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit 8f7830
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit 8f7830
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit 8f7830
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 8f7830
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Packit 8f7830
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 8f7830
 */
Packit 8f7830
Packit 8f7830
#ifdef HAVE_CONFIG_H
Packit 8f7830
#  include <config.h>
Packit 8f7830
#endif
Packit 8f7830
Packit 8f7830
#include "private/bitmath.h"
Packit 8f7830
Packit 8f7830
/* An example of what FLAC__bitmath_silog2() computes:
Packit 8f7830
 *
Packit 8f7830
 * silog2(-10) = 5
Packit 8f7830
 * silog2(- 9) = 5
Packit 8f7830
 * silog2(- 8) = 4
Packit 8f7830
 * silog2(- 7) = 4
Packit 8f7830
 * silog2(- 6) = 4
Packit 8f7830
 * silog2(- 5) = 4
Packit 8f7830
 * silog2(- 4) = 3
Packit 8f7830
 * silog2(- 3) = 3
Packit 8f7830
 * silog2(- 2) = 2
Packit 8f7830
 * silog2(- 1) = 2
Packit 8f7830
 * silog2(  0) = 0
Packit 8f7830
 * silog2(  1) = 2
Packit 8f7830
 * silog2(  2) = 3
Packit 8f7830
 * silog2(  3) = 3
Packit 8f7830
 * silog2(  4) = 4
Packit 8f7830
 * silog2(  5) = 4
Packit 8f7830
 * silog2(  6) = 4
Packit 8f7830
 * silog2(  7) = 4
Packit 8f7830
 * silog2(  8) = 5
Packit 8f7830
 * silog2(  9) = 5
Packit 8f7830
 * silog2( 10) = 5
Packit 8f7830
 */
Packit 8f7830
unsigned FLAC__bitmath_silog2(FLAC__int64 v)
Packit 8f7830
{
Packit 8f7830
	if(v == 0)
Packit 8f7830
		return 0;
Packit 8f7830
Packit 8f7830
	if(v == -1)
Packit 8f7830
		return 2;
Packit 8f7830
Packit 8f7830
	v = (v < 0) ? (-(v+1)) : v;
Packit 8f7830
	return FLAC__bitmath_ilog2_wide(v)+2;
Packit 8f7830
}