Merge branch '2020-05-08-assorted-fixes'
[oweals/u-boot.git] / lib / bch.c
index 147715afd06a7a39216d4215e1601e5a5d977917..8945d8d4cf2ac7ccdb5c46f4826eb92f1f4c29e7 100644 (file)
--- a/lib/bch.c
+++ b/lib/bch.c
@@ -1,8 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Generic binary BCH encoding/decoding library
  *
- * SPDX-License-Identifier:    GPL-2.0
- *
  * Copyright © 2011 Parrot S.A.
  *
  * Author: Ivan Djelic <ivan.djelic@parrot.com>
  * finite fields GF(2^q). In Rapport de recherche INRIA no 2829, 1996.
  */
 
+#ifndef USE_HOSTCC
 #include <common.h>
+#include <malloc.h>
 #include <ubi_uboot.h>
+#include <dm/devres.h>
 
 #include <linux/bitops.h>
+#else
+#include <errno.h>
+#if defined(__FreeBSD__)
+#include <sys/endian.h>
+#elif defined(__APPLE__)
+#include <machine/endian.h>
+#include <libkern/OSByteOrder.h>
+#else
+#include <endian.h>
+#endif
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#undef cpu_to_be32
+#if defined(__APPLE__)
+#define cpu_to_be32 OSSwapHostToBigInt32
+#else
+#define cpu_to_be32 htobe32
+#endif
+#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
+#define kmalloc(size, flags)   malloc(size)
+#define kzalloc(size, flags)   calloc(1, size)
+#define kfree free
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#endif
+
 #include <asm/byteorder.h>
 #include <linux/bch.h>
 
@@ -95,6 +124,39 @@ struct gf_poly_deg1 {
        unsigned int   c[2];
 };
 
+#ifdef USE_HOSTCC
+#if !defined(__DragonFly__) && !defined(__FreeBSD__) && !defined(__APPLE__)
+static int fls(int x)
+{
+       int r = 32;
+
+       if (!x)
+               return 0;
+       if (!(x & 0xffff0000u)) {
+               x <<= 16;
+               r -= 16;
+       }
+       if (!(x & 0xff000000u)) {
+               x <<= 8;
+               r -= 8;
+       }
+       if (!(x & 0xf0000000u)) {
+               x <<= 4;
+               r -= 4;
+       }
+       if (!(x & 0xc0000000u)) {
+               x <<= 2;
+               r -= 2;
+       }
+       if (!(x & 0x80000000u)) {
+               x <<= 1;
+               r -= 1;
+       }
+       return r;
+}
+#endif
+#endif
+
 /*
  * same as encode_bch(), but process input data one byte at a time
  */