/* language-model-utils.c generated by valac 0.56.19, the Vala compiler
 * generated from language-model-utils.vala, do not modify */

/*
 * Copyright (C) 2012-2014 Daiki Ueno <ueno@gnu.org>
 * Copyright (C) 2012-2014 Red Hat, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <float.h>
#include <math.h>
#include <string.h>
#include "libkkc/libkkc.h"

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

VALA_EXTERN gdouble kkc_language_model_utils_decode_cost (guint16 cost,
                                              gdouble min_cost);
VALA_EXTERN glong kkc_language_model_utils_bsearch_ngram (void* memory,
                                              glong start_offset,
                                              glong end_offset,
                                              glong record_size,
                                              guint8* needle,
                                              gint needle_length1);

gdouble
kkc_language_model_utils_decode_cost (guint16 cost,
                                      gdouble min_cost)
{
	gdouble result;
	result = (cost * min_cost) / 65535;
	return result;
}

glong
kkc_language_model_utils_bsearch_ngram (void* memory,
                                        glong start_offset,
                                        glong end_offset,
                                        glong record_size,
                                        guint8* needle,
                                        gint needle_length1)
{
	glong offset = 0L;
	glong result;
	offset = start_offset + ((end_offset - start_offset) / 2);
	while (TRUE) {
		guint8* p = NULL;
		gint r = 0;
		guint8* _tmp0_;
		if (!(start_offset <= end_offset)) {
			break;
		}
		p = ((guint8*) memory) + (offset * record_size);
		_tmp0_ = p;
		r = memcmp (_tmp0_, needle, (gsize) needle_length1);
		if (r == 0) {
			result = offset;
			return result;
		}
		if (r > 0) {
			end_offset = offset - 1;
		} else {
			start_offset = offset + 1;
		}
		offset = start_offset + ((end_offset - start_offset) / 2);
	}
	result = (glong) -1;
	return result;
}

