From 992d4b4b8d3f3a630a2cf00d593ee4ce4dfa7ce8 Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Thu, 9 Jan 2025 22:12:06 +0100 Subject: New upstream version 2.7.0~rc8+repack --- lib/superfox/qpc/dbgprintf.c | 134 +++---- lib/superfox/qpc/dbgprintf.h | 128 +++---- lib/superfox/qpc/nhash2.c | 766 ++++++++++++++++++------------------- lib/superfox/qpc/nhash2.h | 44 +-- lib/superfox/qpc/np_qpc.c | 658 +++++++++++++++---------------- lib/superfox/qpc/np_qpc.h | 124 +++--- lib/superfox/qpc/np_rnd.c | 270 ++++++------- lib/superfox/qpc/np_rnd.h | 118 +++--- lib/superfox/qpc/qpc_fwht.c | 466 +++++++++++----------- lib/superfox/qpc/qpc_fwht.h | 114 +++--- lib/superfox/qpc/qpc_main.cpp | 396 +++++++++---------- lib/superfox/qpc/qpc_mod.f90 | 76 ++-- lib/superfox/qpc/qpc_n127k50q128.c | 120 +++--- lib/superfox/qpc/qpc_subs.c | 230 +++++------ 14 files changed, 1822 insertions(+), 1822 deletions(-) (limited to 'lib/superfox/qpc') diff --git a/lib/superfox/qpc/dbgprintf.c b/lib/superfox/qpc/dbgprintf.c index f17d4e47..4a86e555 100755 --- a/lib/superfox/qpc/dbgprintf.c +++ b/lib/superfox/qpc/dbgprintf.c @@ -1,67 +1,67 @@ -// ------------------------------------------------------------------------------ -// dbgprintf.c -// Functions for printing debug information -// -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ -// -// This source 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 file 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 source distribution. -// If not, see . -// ------------------------------------------------------------------------------ - -#include "dbgprintf.h" - -// print functions for debug purposes -void dbgprintf_vector_uchar(const char* name, const unsigned char* v, int vsize) -{ - int k; - - printf("%s=", name); - for (k = 0; k < vsize; k++) { - if ((k & 0x0F) == 0) - printf("\n"); - printf("%02X ", v[k]); - } - printf("\n"); -} - -void dbgprintf_vector_float(const char* name, const float* v, int vsize) -{ - int k; - - printf("%s=", name); - for (k = 0; k < vsize; k++) { - if ((k & 0x07) == 0) - printf("\n"); - printf("%10.3f ", v[k]); - } - printf("\n"); -} - -void dbgprintf_rows_float(const char* name, const float* v, int vsize, int nrows) -{ - int k; - int j; - - printf("%s=\n", name); - for (j = 0; j < nrows; j++) { - printf("r%d:", j); - for (k = 0; k < vsize; k++) { - if ((k & 0x07) == 0) - printf("\n"); - printf("%7.3f ", v[k]); - } - printf("\n"); - v += vsize; - } -} +// ------------------------------------------------------------------------------ +// dbgprintf.c +// Functions for printing debug information +// +// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy +// ------------------------------------------------------------------------------ +// +// This source 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 file 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 source distribution. +// If not, see . +// ------------------------------------------------------------------------------ + +#include "dbgprintf.h" + +// print functions for debug purposes +void dbgprintf_vector_uchar(const char* name, const unsigned char* v, int vsize) +{ + int k; + + printf("%s=", name); + for (k = 0; k < vsize; k++) { + if ((k & 0x0F) == 0) + printf("\n"); + printf("%02X ", v[k]); + } + printf("\n"); +} + +void dbgprintf_vector_float(const char* name, const float* v, int vsize) +{ + int k; + + printf("%s=", name); + for (k = 0; k < vsize; k++) { + if ((k & 0x07) == 0) + printf("\n"); + printf("%10.3f ", v[k]); + } + printf("\n"); +} + +void dbgprintf_rows_float(const char* name, const float* v, int vsize, int nrows) +{ + int k; + int j; + + printf("%s=\n", name); + for (j = 0; j < nrows; j++) { + printf("r%d:", j); + for (k = 0; k < vsize; k++) { + if ((k & 0x07) == 0) + printf("\n"); + printf("%7.3f ", v[k]); + } + printf("\n"); + v += vsize; + } +} diff --git a/lib/superfox/qpc/dbgprintf.h b/lib/superfox/qpc/dbgprintf.h index 8133042b..dc906f09 100755 --- a/lib/superfox/qpc/dbgprintf.h +++ b/lib/superfox/qpc/dbgprintf.h @@ -1,64 +1,64 @@ -// ------------------------------------------------------------------------------ -// dbgprintf.h -// Functions for printing debug information -// -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ -// -// This source 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 file 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 source distribution. -// If not, see . -// ------------------------------------------------------------------------------ - -#ifndef _dbgprintf_h_ -#define _dbgprintf_h_ - -// Define DBGPRINTF_ANYWAY -// in order to print debug information also -// when _DEBUG is not defined - -#define DBGPRINTF_ANYWAY - -#ifdef _DEBUG -#define DBG_PRINTF -#else -#ifdef DBGPRINTF_ANYWAY -#define DBG_PRINTF -#endif -#endif - -#include - -// print functions for debug purposes -#ifdef DBG_PRINTF - -#ifdef __cplusplus -extern "C" { -#endif - - void dbgprintf_vector_uchar(const char* name, const unsigned char* v, int vsize); - void dbgprintf_vector_float(const char* name, const float* v, int vsize); - void dbgprintf_rows_float(const char* name, const float* v, int vsize, int nrows); - -#ifdef __cplusplus -} -#endif - -#else - -#define dbgprintf_vector_uchar(a,b) -#define dbgprintf_vector_float(a,b) -#define dbgprintf_rows_float(a, b, c, d) - -#endif - -#endif // _dbgprintf_h_ +// ------------------------------------------------------------------------------ +// dbgprintf.h +// Functions for printing debug information +// +// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy +// ------------------------------------------------------------------------------ +// +// This source 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 file 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 source distribution. +// If not, see . +// ------------------------------------------------------------------------------ + +#ifndef _dbgprintf_h_ +#define _dbgprintf_h_ + +// Define DBGPRINTF_ANYWAY +// in order to print debug information also +// when _DEBUG is not defined + +#define DBGPRINTF_ANYWAY + +#ifdef _DEBUG +#define DBG_PRINTF +#else +#ifdef DBGPRINTF_ANYWAY +#define DBG_PRINTF +#endif +#endif + +#include + +// print functions for debug purposes +#ifdef DBG_PRINTF + +#ifdef __cplusplus +extern "C" { +#endif + + void dbgprintf_vector_uchar(const char* name, const unsigned char* v, int vsize); + void dbgprintf_vector_float(const char* name, const float* v, int vsize); + void dbgprintf_rows_float(const char* name, const float* v, int vsize, int nrows); + +#ifdef __cplusplus +} +#endif + +#else + +#define dbgprintf_vector_uchar(a,b) +#define dbgprintf_vector_float(a,b) +#define dbgprintf_rows_float(a, b, c, d) + +#endif + +#endif // _dbgprintf_h_ diff --git a/lib/superfox/qpc/nhash2.c b/lib/superfox/qpc/nhash2.c index 40bae20e..78817c7d 100755 --- a/lib/superfox/qpc/nhash2.c +++ b/lib/superfox/qpc/nhash2.c @@ -1,383 +1,383 @@ -/* - - File name: nhash2.c - - *------------------------------------------------------------------------------ - * - * This file is part of the WSPR application, Weak Signal Propogation Reporter - * - * File Name: nhash.c - * Description: Functions to produce 32-bit hashes for hash table lookup - * - * Copyright (C) 2008-2014 Joseph Taylor, K1JT - * License: GNU GPL v3+ - * - * 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, write to the Free Software Foundation, Inc., 51 Franklin - * Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * Files: lookup3.c - * Copyright: Copyright (C) 2006 Bob Jenkins - * License: public-domain - * You may use this code any way you wish, private, educational, or commercial. - * It's free. - * - *------------------------------------------------------------------------------- -*/ - -/* -These are functions for producing 32-bit hashes for hash table lookup. -hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() -are externally useful functions. Routines to test the hash are included -if SELF_TEST is defined. You can use this free for any purpose. It's in -the public domain. It has no warranty. - -You probably want to use hashlittle(). hashlittle() and hashbig() -hash byte arrays. hashlittle() is is faster than hashbig() on -little-endian machines. Intel and AMD are little-endian machines. -On second thought, you probably want hashlittle2(), which is identical to -hashlittle() except it returns two 32-bit hashes for the price of one. -You could implement hashbig2() if you wanted but I haven't bothered here. - -If you want to find a hash of, say, exactly 7 integers, do - a = i1; b = i2; c = i3; - mix(a,b,c); - a += i4; b += i5; c += i6; - mix(a,b,c); - a += i7; - final(a,b,c); -then use c as the hash value. If you have a variable length array of -4-byte integers to hash, use hashword(). If you have a byte array (like -a character string), use hashlittle(). If you have several byte arrays, or -a mix of things, see the comments above hashlittle(). - -Why is this so big? I read 12 bytes at a time into 3 4-byte integers, -then mix those integers. This is fast (you can do a lot more thorough -mixing with 12*3 instructions on 3 integers than you can with 3 instructions -on 1 byte), but shoehorning those bytes into integers efficiently is messy. -*/ - -#define SELF_TEST 1 - -#include /* defines printf for tests */ -#include /* defines time_t for timings in the test */ -#include "nhash2.h" -//#include /* attempt to define endianness */ -//#ifdef linux -//# include /* attempt to define endianness */ -//#endif - -#define HASH_LITTLE_ENDIAN 1 - -#define hashsize(n) ((uint32_t)1<<(n)) -#define hashmask(n) (hashsize(n)-1) -#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) - -/* -------------------------------------------------------------------------------- -mix -- mix 3 32-bit values reversibly. - -This is reversible, so any information in (a,b,c) before mix() is -still in (a,b,c) after mix(). - -If four pairs of (a,b,c) inputs are run through mix(), or through -mix() in reverse, there are at least 32 bits of the output that -are sometimes the same for one pair and different for another pair. -This was tested for: -* pairs that differed by one bit, by two bits, in any combination - of top bits of (a,b,c), or in any combination of bottom bits of - (a,b,c). -* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed - the output delta to a Gray code (a^(a>>1)) so a string of 1's (as - is commonly produced by subtraction) look like a single 1-bit - difference. -* the base values were pseudorandom, all zero but one bit set, or - all zero plus a counter that starts at zero. - -Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that -satisfy this are - 4 6 8 16 19 4 - 9 15 3 18 27 15 - 14 9 3 7 17 3 -Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing -for "differ" defined as + with a one-bit base and a two-bit delta. I -used http://burtleburtle.net/bob/hash/avalanche.html to choose -the operations, constants, and arrangements of the variables. - -This does not achieve avalanche. There are input bits of (a,b,c) -that fail to affect some output bits of (a,b,c), especially of a. The -most thoroughly mixed value is c, but it doesn't really even achieve -avalanche in c. - -This allows some parallelism. Read-after-writes are good at doubling -the number of bits affected, so the goal of mixing pulls in the opposite -direction as the goal of parallelism. I did what I could. Rotates -seem to cost as much as shifts on every machine I could lay my hands -on, and rotates are much kinder to the top and bottom bits, so I used -rotates. -------------------------------------------------------------------------------- -*/ -#define mix(a,b,c) \ -{ \ - a -= c; a ^= rot(c, 4); c += b; \ - b -= a; b ^= rot(a, 6); a += c; \ - c -= b; c ^= rot(b, 8); b += a; \ - a -= c; a ^= rot(c,16); c += b; \ - b -= a; b ^= rot(a,19); a += c; \ - c -= b; c ^= rot(b, 4); b += a; \ -} - -/* -------------------------------------------------------------------------------- -final -- final mixing of 3 32-bit values (a,b,c) into c - -Pairs of (a,b,c) values differing in only a few bits will usually -produce values of c that look totally different. This was tested for -* pairs that differed by one bit, by two bits, in any combination - of top bits of (a,b,c), or in any combination of bottom bits of - (a,b,c). -* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed - the output delta to a Gray code (a^(a>>1)) so a string of 1's (as - is commonly produced by subtraction) look like a single 1-bit - difference. -* the base values were pseudorandom, all zero but one bit set, or - all zero plus a counter that starts at zero. - -These constants passed: - 14 11 25 16 4 14 24 - 12 14 25 16 4 14 24 -and these came close: - 4 8 15 26 3 22 24 - 10 8 15 26 3 22 24 - 11 8 15 26 3 22 24 -------------------------------------------------------------------------------- -*/ -#define final(a,b,c) \ -{ \ - c ^= b; c -= rot(b,14); \ - a ^= c; a -= rot(c,11); \ - b ^= a; b -= rot(a,25); \ - c ^= b; c -= rot(b,16); \ - a ^= c; a -= rot(c,4); \ - b ^= a; b -= rot(a,14); \ - c ^= b; c -= rot(b,24); \ -} - -/* -------------------------------------------------------------------------------- -hashlittle() -- hash a variable-length key into a 32-bit value - k : the key (the unaligned variable-length array of bytes) - length : the length of the key, counting by bytes - initval : can be any 4-byte value -Returns a 32-bit value. Every bit of the key affects every bit of -the return value. Two keys differing by one or two bits will have -totally different hash values. - -The best hash table sizes are powers of 2. There is no need to do -mod a prime (mod is sooo slow!). If you need less than 32 bits, -use a bitmask. For example, if you need only 10 bits, do - h = (h & hashmask(10)); -In which case, the hash table should have hashsize(10) elements. - -If you are hashing n strings (uint8_t **)k, do it like this: - for (i=0, h=0; i 12) - { - a += k[0]; - b += k[1]; - c += k[2]; - mix(a,b,c); - length -= 12; - k += 3; - } - - // printf("AAA %x %d %x %d\n",c,c,c&32767,c&32767); - - /*----------------------------- handle the last (probably partial) block */ - /* - * "k[2]&0xffffff" actually reads beyond the end of the string, but - * then masks off the part it's not allowed to read. Because the - * string is aligned, the masked-off tail is in the same word as the - * rest of the string. Every machine with memory protection I've seen - * does it on word boundaries, so is OK with this. But VALGRIND will - * still catch it and complain. The masking trick does make the hash - * noticably faster for short strings (like English words). - */ -#ifndef VALGRIND - - switch(length) - { - case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; - case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; - case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; - case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; - case 8 : b+=k[1]; a+=k[0]; break; - case 7 : b+=k[1]&0xffffff; a+=k[0]; break; - case 6 : b+=k[1]&0xffff; a+=k[0]; break; - case 5 : b+=k[1]&0xff; a+=k[0]; break; - case 4 : a+=k[0]; break; - case 3 : a+=k[0]&0xffffff; break; - case 2 : a+=k[0]&0xffff; break; - case 1 : a+=k[0]&0xff; break; - case 0 : return c; /* zero length strings require no mixing */ - } - -#else /* make valgrind happy */ - - k8 = (const uint8_t *)k; - switch(length) - { - case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; - case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ - case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ - case 9 : c+=k8[8]; /* fall through */ - case 8 : b+=k[1]; a+=k[0]; break; - case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ - case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ - case 5 : b+=k8[4]; /* fall through */ - case 4 : a+=k[0]; break; - case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ - case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ - case 1 : a+=k8[0]; break; - case 0 : return c; - } - -#endif /* !valgrind */ - - } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { - const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ - const uint8_t *k8; - - /*--------------- all but last block: aligned reads and different mixing */ - while (length > 12) - { - a += k[0] + (((uint32_t)k[1])<<16); - b += k[2] + (((uint32_t)k[3])<<16); - c += k[4] + (((uint32_t)k[5])<<16); - mix(a,b,c); - length -= 12; - k += 6; - } - - /*----------------------------- handle the last (probably partial) block */ - k8 = (const uint8_t *)k; - switch(length) - { - case 12: c+=k[4]+(((uint32_t)k[5])<<16); - b+=k[2]+(((uint32_t)k[3])<<16); - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ - case 10: c+=k[4]; - b+=k[2]+(((uint32_t)k[3])<<16); - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 9 : c+=k8[8]; /* fall through */ - case 8 : b+=k[2]+(((uint32_t)k[3])<<16); - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ - case 6 : b+=k[2]; - a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 5 : b+=k8[4]; /* fall through */ - case 4 : a+=k[0]+(((uint32_t)k[1])<<16); - break; - case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ - case 2 : a+=k[0]; - break; - case 1 : a+=k8[0]; - break; - case 0 : return c; /* zero length requires no mixing */ - } - - } else { /* need to read the key one byte at a time */ - const uint8_t *k = (const uint8_t *)key; - - /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ - while (length > 12) - { - a += k[0]; - a += ((uint32_t)k[1])<<8; - a += ((uint32_t)k[2])<<16; - a += ((uint32_t)k[3])<<24; - b += k[4]; - b += ((uint32_t)k[5])<<8; - b += ((uint32_t)k[6])<<16; - b += ((uint32_t)k[7])<<24; - c += k[8]; - c += ((uint32_t)k[9])<<8; - c += ((uint32_t)k[10])<<16; - c += ((uint32_t)k[11])<<24; - mix(a,b,c); - length -= 12; - k += 12; - } - - /*-------------------------------- last block: affect all 32 bits of (c) */ - switch(length) /* all the case statements fall through */ - { - case 12: c+=((uint32_t)k[11])<<24; - /* fall through */ - case 11: c+=((uint32_t)k[10])<<16; - /* fall through */ - case 10: c+=((uint32_t)k[9])<<8; - /* fall through */ - case 9 : c+=k[8]; - /* fall through */ - case 8 : b+=((uint32_t)k[7])<<24; - /* fall through */ - case 7 : b+=((uint32_t)k[6])<<16; - /* fall through */ - case 6 : b+=((uint32_t)k[5])<<8; - /* fall through */ - case 5 : b+=k[4]; - /* fall through */ - case 4 : a+=((uint32_t)k[3])<<24; - /* fall through */ - case 3 : a+=((uint32_t)k[2])<<16; - /* fall through */ - case 2 : a+=((uint32_t)k[1])<<8; - /* fall through */ - case 1 : a+=k[0]; - break; - case 0 : return c; - } - } - - final(a,b,c); - return c; -} +/* + + File name: nhash2.c + + *------------------------------------------------------------------------------ + * + * This file is part of the WSPR application, Weak Signal Propogation Reporter + * + * File Name: nhash.c + * Description: Functions to produce 32-bit hashes for hash table lookup + * + * Copyright (C) 2008-2014 Joseph Taylor, K1JT + * License: GNU GPL v3+ + * + * 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, write to the Free Software Foundation, Inc., 51 Franklin + * Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Files: lookup3.c + * Copyright: Copyright (C) 2006 Bob Jenkins + * License: public-domain + * You may use this code any way you wish, private, educational, or commercial. + * It's free. + * + *------------------------------------------------------------------------------- +*/ + +/* +These are functions for producing 32-bit hashes for hash table lookup. +hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() +are externally useful functions. Routines to test the hash are included +if SELF_TEST is defined. You can use this free for any purpose. It's in +the public domain. It has no warranty. + +You probably want to use hashlittle(). hashlittle() and hashbig() +hash byte arrays. hashlittle() is is faster than hashbig() on +little-endian machines. Intel and AMD are little-endian machines. +On second thought, you probably want hashlittle2(), which is identical to +hashlittle() except it returns two 32-bit hashes for the price of one. +You could implement hashbig2() if you wanted but I haven't bothered here. + +If you want to find a hash of, say, exactly 7 integers, do + a = i1; b = i2; c = i3; + mix(a,b,c); + a += i4; b += i5; c += i6; + mix(a,b,c); + a += i7; + final(a,b,c); +then use c as the hash value. If you have a variable length array of +4-byte integers to hash, use hashword(). If you have a byte array (like +a character string), use hashlittle(). If you have several byte arrays, or +a mix of things, see the comments above hashlittle(). + +Why is this so big? I read 12 bytes at a time into 3 4-byte integers, +then mix those integers. This is fast (you can do a lot more thorough +mixing with 12*3 instructions on 3 integers than you can with 3 instructions +on 1 byte), but shoehorning those bytes into integers efficiently is messy. +*/ + +#define SELF_TEST 1 + +#include /* defines printf for tests */ +#include /* defines time_t for timings in the test */ +#include "nhash2.h" +//#include /* attempt to define endianness */ +//#ifdef linux +//# include /* attempt to define endianness */ +//#endif + +#define HASH_LITTLE_ENDIAN 1 + +#define hashsize(n) ((uint32_t)1<<(n)) +#define hashmask(n) (hashsize(n)-1) +#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) + +/* +------------------------------------------------------------------------------- +mix -- mix 3 32-bit values reversibly. + +This is reversible, so any information in (a,b,c) before mix() is +still in (a,b,c) after mix(). + +If four pairs of (a,b,c) inputs are run through mix(), or through +mix() in reverse, there are at least 32 bits of the output that +are sometimes the same for one pair and different for another pair. +This was tested for: +* pairs that differed by one bit, by two bits, in any combination + of top bits of (a,b,c), or in any combination of bottom bits of + (a,b,c). +* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed + the output delta to a Gray code (a^(a>>1)) so a string of 1's (as + is commonly produced by subtraction) look like a single 1-bit + difference. +* the base values were pseudorandom, all zero but one bit set, or + all zero plus a counter that starts at zero. + +Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that +satisfy this are + 4 6 8 16 19 4 + 9 15 3 18 27 15 + 14 9 3 7 17 3 +Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing +for "differ" defined as + with a one-bit base and a two-bit delta. I +used http://burtleburtle.net/bob/hash/avalanche.html to choose +the operations, constants, and arrangements of the variables. + +This does not achieve avalanche. There are input bits of (a,b,c) +that fail to affect some output bits of (a,b,c), especially of a. The +most thoroughly mixed value is c, but it doesn't really even achieve +avalanche in c. + +This allows some parallelism. Read-after-writes are good at doubling +the number of bits affected, so the goal of mixing pulls in the opposite +direction as the goal of parallelism. I did what I could. Rotates +seem to cost as much as shifts on every machine I could lay my hands +on, and rotates are much kinder to the top and bottom bits, so I used +rotates. +------------------------------------------------------------------------------- +*/ +#define mix(a,b,c) \ +{ \ + a -= c; a ^= rot(c, 4); c += b; \ + b -= a; b ^= rot(a, 6); a += c; \ + c -= b; c ^= rot(b, 8); b += a; \ + a -= c; a ^= rot(c,16); c += b; \ + b -= a; b ^= rot(a,19); a += c; \ + c -= b; c ^= rot(b, 4); b += a; \ +} + +/* +------------------------------------------------------------------------------- +final -- final mixing of 3 32-bit values (a,b,c) into c + +Pairs of (a,b,c) values differing in only a few bits will usually +produce values of c that look totally different. This was tested for +* pairs that differed by one bit, by two bits, in any combination + of top bits of (a,b,c), or in any combination of bottom bits of + (a,b,c). +* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed + the output delta to a Gray code (a^(a>>1)) so a string of 1's (as + is commonly produced by subtraction) look like a single 1-bit + difference. +* the base values were pseudorandom, all zero but one bit set, or + all zero plus a counter that starts at zero. + +These constants passed: + 14 11 25 16 4 14 24 + 12 14 25 16 4 14 24 +and these came close: + 4 8 15 26 3 22 24 + 10 8 15 26 3 22 24 + 11 8 15 26 3 22 24 +------------------------------------------------------------------------------- +*/ +#define final(a,b,c) \ +{ \ + c ^= b; c -= rot(b,14); \ + a ^= c; a -= rot(c,11); \ + b ^= a; b -= rot(a,25); \ + c ^= b; c -= rot(b,16); \ + a ^= c; a -= rot(c,4); \ + b ^= a; b -= rot(a,14); \ + c ^= b; c -= rot(b,24); \ +} + +/* +------------------------------------------------------------------------------- +hashlittle() -- hash a variable-length key into a 32-bit value + k : the key (the unaligned variable-length array of bytes) + length : the length of the key, counting by bytes + initval : can be any 4-byte value +Returns a 32-bit value. Every bit of the key affects every bit of +the return value. Two keys differing by one or two bits will have +totally different hash values. + +The best hash table sizes are powers of 2. There is no need to do +mod a prime (mod is sooo slow!). If you need less than 32 bits, +use a bitmask. For example, if you need only 10 bits, do + h = (h & hashmask(10)); +In which case, the hash table should have hashsize(10) elements. + +If you are hashing n strings (uint8_t **)k, do it like this: + for (i=0, h=0; i 12) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + length -= 12; + k += 3; + } + + // printf("AAA %x %d %x %d\n",c,c,c&32767,c&32767); + + /*----------------------------- handle the last (probably partial) block */ + /* + * "k[2]&0xffffff" actually reads beyond the end of the string, but + * then masks off the part it's not allowed to read. Because the + * string is aligned, the masked-off tail is in the same word as the + * rest of the string. Every machine with memory protection I've seen + * does it on word boundaries, so is OK with this. But VALGRIND will + * still catch it and complain. The masking trick does make the hash + * noticably faster for short strings (like English words). + */ +#ifndef VALGRIND + + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; + case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; + case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=k[1]&0xffffff; a+=k[0]; break; + case 6 : b+=k[1]&0xffff; a+=k[0]; break; + case 5 : b+=k[1]&0xff; a+=k[0]; break; + case 4 : a+=k[0]; break; + case 3 : a+=k[0]&0xffffff; break; + case 2 : a+=k[0]&0xffff; break; + case 1 : a+=k[0]&0xff; break; + case 0 : return c; /* zero length strings require no mixing */ + } + +#else /* make valgrind happy */ + + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]; break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ + case 1 : a+=k8[0]; break; + case 0 : return c; + } + +#endif /* !valgrind */ + + } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { + const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ + const uint8_t *k8; + + /*--------------- all but last block: aligned reads and different mixing */ + while (length > 12) + { + a += k[0] + (((uint32_t)k[1])<<16); + b += k[2] + (((uint32_t)k[3])<<16); + c += k[4] + (((uint32_t)k[5])<<16); + mix(a,b,c); + length -= 12; + k += 6; + } + + /*----------------------------- handle the last (probably partial) block */ + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[4]+(((uint32_t)k[5])<<16); + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=k[4]; + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=k[2]; + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=k[0]; + break; + case 1 : a+=k8[0]; + break; + case 0 : return c; /* zero length requires no mixing */ + } + + } else { /* need to read the key one byte at a time */ + const uint8_t *k = (const uint8_t *)key; + + /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ + while (length > 12) + { + a += k[0]; + a += ((uint32_t)k[1])<<8; + a += ((uint32_t)k[2])<<16; + a += ((uint32_t)k[3])<<24; + b += k[4]; + b += ((uint32_t)k[5])<<8; + b += ((uint32_t)k[6])<<16; + b += ((uint32_t)k[7])<<24; + c += k[8]; + c += ((uint32_t)k[9])<<8; + c += ((uint32_t)k[10])<<16; + c += ((uint32_t)k[11])<<24; + mix(a,b,c); + length -= 12; + k += 12; + } + + /*-------------------------------- last block: affect all 32 bits of (c) */ + switch(length) /* all the case statements fall through */ + { + case 12: c+=((uint32_t)k[11])<<24; + /* fall through */ + case 11: c+=((uint32_t)k[10])<<16; + /* fall through */ + case 10: c+=((uint32_t)k[9])<<8; + /* fall through */ + case 9 : c+=k[8]; + /* fall through */ + case 8 : b+=((uint32_t)k[7])<<24; + /* fall through */ + case 7 : b+=((uint32_t)k[6])<<16; + /* fall through */ + case 6 : b+=((uint32_t)k[5])<<8; + /* fall through */ + case 5 : b+=k[4]; + /* fall through */ + case 4 : a+=((uint32_t)k[3])<<24; + /* fall through */ + case 3 : a+=((uint32_t)k[2])<<16; + /* fall through */ + case 2 : a+=((uint32_t)k[1])<<8; + /* fall through */ + case 1 : a+=k[0]; + break; + case 0 : return c; + } + } + + final(a,b,c); + return c; +} diff --git a/lib/superfox/qpc/nhash2.h b/lib/superfox/qpc/nhash2.h index 830e18ac..1864a93f 100755 --- a/lib/superfox/qpc/nhash2.h +++ b/lib/superfox/qpc/nhash2.h @@ -1,22 +1,22 @@ -#ifndef NHASH_H_ -#define NHASH_H_ - -#ifdef Win32 -#include "win_stdint.h" /* defines uint32_t etc */ -#else -#include -#include /* defines uint32_t etc */ -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -uint32_t nhash( const void * key, uint64_t length, uint32_t initval); - //int nhash(const char *key, int length, int initval); - -#ifdef __cplusplus -} -#endif - -#endif +#ifndef NHASH_H_ +#define NHASH_H_ + +#ifdef Win32 +#include "win_stdint.h" /* defines uint32_t etc */ +#else +#include +#include /* defines uint32_t etc */ +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +uint32_t nhash( const void * key, uint64_t length, uint32_t initval); + //int nhash(const char *key, int length, int initval); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/superfox/qpc/np_qpc.c b/lib/superfox/qpc/np_qpc.c index 9398c43b..d44e544b 100755 --- a/lib/superfox/qpc/np_qpc.c +++ b/lib/superfox/qpc/np_qpc.c @@ -1,329 +1,329 @@ -// ------------------------------------------------------------------------------ -// np_qpc.h -// Q-Ary Polar Codes encoding/decoding functions -// -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ -// -// This source 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 file 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 source distribution. -// If not, see . - -#include -#include -#include - -#include "dbgprintf.h" -#include "qpc_fwht.h" -#include "np_qpc.h" - -// static constant / functions - -static const float knorm = 1.0f / QPC_Q; - -static float* pdf_conv(float *dst, float* pdf1, float* pdf2) -{ - // convolution between two pdf - - float fwht_pdf1[QPC_Q]; - float fwht_pdf2[QPC_Q]; - int k; - - qpc_fwht(fwht_pdf1, pdf1); - qpc_fwht(fwht_pdf2, pdf2); - - for (k = 0; k < QPC_Q; k++) - fwht_pdf1[k] *= fwht_pdf2[k]; - - qpc_fwht(dst, fwht_pdf1); - - for (k = 0; k < QPC_Q; k++) - dst[k] *= knorm; - - return dst; -} -static void pdfarray_conv(float* dstarray, float* pdf1array, float* pdf2array, int numrows) -{ - int k; - - // convolutions between rows of pdfs - - for (k = 0; k < numrows; k++) { - pdf_conv(dstarray, pdf1array, pdf2array); - dstarray += QPC_Q; - pdf1array += QPC_Q; - pdf2array += QPC_Q; - } - -} - -static float _pdfuniform[QPC_Q]; -static const float* _pdf_uniform1(); -static const float* _pdf_uniform0(); -typedef const float*(*ptr_pdfuniform)(void); - -static ptr_pdfuniform _ptr_pdf_uniform = _pdf_uniform0; - -static const float* _pdf_uniform1() -{ - return _pdfuniform; -}; -static const float* _pdf_uniform0() -{ - // compute uniform pdf once for all - int k; - for (k = 0; k < QPC_Q; k++) - _pdfuniform[k] = knorm; - - // next call to _qpc_pdfuniform - // will be handled directly by _pqc_pdfuniform1 - _ptr_pdf_uniform = _pdf_uniform1; - - return _pdfuniform; -}; -static const float* pdf_uniform() -{ - return _ptr_pdf_uniform(); -} - -static float * pdf_mul(float *dst, float* pdf1, float* pdf2) -{ - int k; - float v; - float norm = 0; - for (k = 0; k < QPC_Q; k++) { - v = pdf1[k] * pdf2[k]; - dst[k] = v; - norm += v; - } - // if norm of the result is not positive - // return in dst a uniform distribution - if (norm <= 0) - memcpy(dst, pdf_uniform(), QPC_Q * sizeof(float)); - else { - norm = 1.0f / norm; - for (k = 0; k < QPC_Q; k++) - dst[k] = dst[k] * norm; - } - - - return dst; -} -static void pdfarray_mul(float* dstarray, float* pdf1array, float* pdf2array, int numrows) -{ - int k; - - // products between rows of pdfs - - for (k = 0; k < numrows; k++) { - pdf_mul(dstarray, pdf1array, pdf2array); - dstarray += QPC_Q; - pdf1array += QPC_Q; - pdf2array += QPC_Q; - } -} - -static float* pdf_convhard(float* dst, const float* pdf, unsigned char hd) -{ - // convolution between a pdf and a hard-decision feedback - - int k; - for (k=0;k pdfmax) { - pdfmax = pdf[k]; - imax = k; - } - - return imax; -} - -// local stack functions --------------------------------------- -static float _qpc_stack[QPC_N * QPC_Q * 2]; -static float* _qpc_stack_base = _qpc_stack; - -static float* _qpc_stack_push(int numfloats) -{ - float* addr = _qpc_stack_base; - _qpc_stack_base += numfloats; - return addr; -} -static void _qpc_stack_pop(int numfloats) -{ - _qpc_stack_base -= numfloats; -} - -// qpc encoder function (internal use) ---------------------------------------------------------- -unsigned char* _qpc_encode(unsigned char* y, unsigned char* x) -{ - // Non recursive polar encoder - // Same architecture of a fast fourier transform - // in which the fft butteflies are replaced by the polar transform - // butterflies - - int k, j, m; - int groups; - int bfypergroup; - int stepbfy; - int stepgroup; - int basegroup; - int basebfy; - - memcpy(y, x, QPC_N); - - for (k = 0; k < QPC_LOG2N; k++) { - groups = 1 << (QPC_LOG2N - 1 - k); - stepbfy = bfypergroup = 1 << k; - stepgroup = stepbfy << 1; - basegroup = 0; - for (j = 0; j < groups; j++) { - basebfy = basegroup; - for (m = 0; m < bfypergroup; m++) { - // polar transform - y[basebfy + stepbfy] = y[basebfy + stepbfy] ^ y[basebfy]; - basebfy = basebfy + 1; - } - basegroup = basegroup + stepgroup; - } - } - - return y; -} - -// qpc polar decoder (internal use )-------------------------------------------------- -void _qpc_decode(unsigned char* xdec, unsigned char* ydec, const float* py, const unsigned char* f, const unsigned char* fsize, const int numrows) -{ - - if (numrows == 1) { - if (fsize[0] == 0) { - // dbgprintf_vector_float("py", py, QPC_Q); - - // frozen symbol - xdec[0] = pdf_max(py); - ydec[0] = f[0]; - } - else { - // fsize = 1 => information symbol - xdec[0] = pdf_max(py); - ydec[0] = xdec[0]; - } - - } - else { - - int k; - int nextrows = numrows >> 1; - int size = nextrows << QPC_LOG2Q; - - // upper block variables - unsigned char* xdech = xdec + nextrows; - unsigned char* ydech = ydec + nextrows; - const unsigned char* fh = f + nextrows; - const unsigned char* fsizeh = fsize + nextrows; - - // Step 1. - // stack and init variables used in the recursion - - float* pyl = _qpc_stack_push(size); - memcpy(pyl, py, size * sizeof(float)); - - float* pyh = _qpc_stack_push(size); - memcpy(pyh, py + size, size * sizeof(float)); - - // Step 2. Recursion on upper block - // Forward pdf convolutions for the upper block - // (place in the lower part of py the convolution of lower and higher blocks) - -// float* pyh = py + size; -// pdfarray_conv(py, pyl, pyh, nextrows); // convolution overwriting the lower block of py which is not needed - - pdfarray_conv(pyh, pyl, pyh, nextrows); - _qpc_decode(xdech, ydech, pyh, fh, fsizeh, nextrows); - - // Step 3. compute pdfs in the lower block - pdfarray_convhard(pyh, py+size, ydech,nextrows); // dst ptr must be different form src ptr - pdfarray_mul(pyl, pyl, pyh, nextrows); - // we don't need pyh anymore - _qpc_stack_pop(size); - - // Step 4. Recursion on the lower block - _qpc_decode(xdec, ydec, pyl, f, fsize, nextrows); - // we don't need pyl anymore - _qpc_stack_pop(size); - - // Step 5. Update backward results - // xdec is already ok, we need just to update ydech - for (k = 0; k < nextrows; k++) - ydech[k] = ydech[k] ^ ydec[k]; - - } - - -} - -// Public encoding/decoding functions ------------------------------------------------ -void qpc_encode(unsigned char* y, const unsigned char* x) -{ - - // map information symbols - int kk, pos; - for (kk = 0; kk < QPC_K; kk++) { - pos = qpccode.xpos[kk]; - qpccode.f[pos] = x[kk]; - } - // do polar encoding - _qpc_encode(y, qpccode.f); -} -void qpc_decode(unsigned char* xdec, unsigned char* ydec, float* py) -{ - int k; - unsigned char x[QPC_N]; - - // set the first py row with know frozen (punctured) symbol - if (qpccode.np < qpccode.n) { - // assume that we punctured only the first output symbol - memset(py, 0, QPC_Q * sizeof(float)); - py[qpccode.f[0]] = 1.0f; - } - - // decode - _qpc_decode(x, ydec, py, qpccode.f, qpccode.fsize, QPC_N); - - // demap information symbols - for (k = 0; k < QPC_K; k++) - xdec[k] = x[qpccode.xpos[k]]; - -} +// ------------------------------------------------------------------------------ +// np_qpc.h +// Q-Ary Polar Codes encoding/decoding functions +// +// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy +// ------------------------------------------------------------------------------ +// +// This source 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 file 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 source distribution. +// If not, see . + +#include +#include +#include + +#include "dbgprintf.h" +#include "qpc_fwht.h" +#include "np_qpc.h" + +// static constant / functions + +static const float knorm = 1.0f / QPC_Q; + +static float* pdf_conv(float *dst, float* pdf1, float* pdf2) +{ + // convolution between two pdf + + float fwht_pdf1[QPC_Q]; + float fwht_pdf2[QPC_Q]; + int k; + + qpc_fwht(fwht_pdf1, pdf1); + qpc_fwht(fwht_pdf2, pdf2); + + for (k = 0; k < QPC_Q; k++) + fwht_pdf1[k] *= fwht_pdf2[k]; + + qpc_fwht(dst, fwht_pdf1); + + for (k = 0; k < QPC_Q; k++) + dst[k] *= knorm; + + return dst; +} +static void pdfarray_conv(float* dstarray, float* pdf1array, float* pdf2array, int numrows) +{ + int k; + + // convolutions between rows of pdfs + + for (k = 0; k < numrows; k++) { + pdf_conv(dstarray, pdf1array, pdf2array); + dstarray += QPC_Q; + pdf1array += QPC_Q; + pdf2array += QPC_Q; + } + +} + +static float _pdfuniform[QPC_Q]; +static const float* _pdf_uniform1(); +static const float* _pdf_uniform0(); +typedef const float*(*ptr_pdfuniform)(void); + +static ptr_pdfuniform _ptr_pdf_uniform = _pdf_uniform0; + +static const float* _pdf_uniform1() +{ + return _pdfuniform; +}; +static const float* _pdf_uniform0() +{ + // compute uniform pdf once for all + int k; + for (k = 0; k < QPC_Q; k++) + _pdfuniform[k] = knorm; + + // next call to _qpc_pdfuniform + // will be handled directly by _pqc_pdfuniform1 + _ptr_pdf_uniform = _pdf_uniform1; + + return _pdfuniform; +}; +static const float* pdf_uniform() +{ + return _ptr_pdf_uniform(); +} + +static float * pdf_mul(float *dst, float* pdf1, float* pdf2) +{ + int k; + float v; + float norm = 0; + for (k = 0; k < QPC_Q; k++) { + v = pdf1[k] * pdf2[k]; + dst[k] = v; + norm += v; + } + // if norm of the result is not positive + // return in dst a uniform distribution + if (norm <= 0) + memcpy(dst, pdf_uniform(), QPC_Q * sizeof(float)); + else { + norm = 1.0f / norm; + for (k = 0; k < QPC_Q; k++) + dst[k] = dst[k] * norm; + } + + + return dst; +} +static void pdfarray_mul(float* dstarray, float* pdf1array, float* pdf2array, int numrows) +{ + int k; + + // products between rows of pdfs + + for (k = 0; k < numrows; k++) { + pdf_mul(dstarray, pdf1array, pdf2array); + dstarray += QPC_Q; + pdf1array += QPC_Q; + pdf2array += QPC_Q; + } +} + +static float* pdf_convhard(float* dst, const float* pdf, unsigned char hd) +{ + // convolution between a pdf and a hard-decision feedback + + int k; + for (k=0;k pdfmax) { + pdfmax = pdf[k]; + imax = k; + } + + return imax; +} + +// local stack functions --------------------------------------- +static float _qpc_stack[QPC_N * QPC_Q * 2]; +static float* _qpc_stack_base = _qpc_stack; + +static float* _qpc_stack_push(int numfloats) +{ + float* addr = _qpc_stack_base; + _qpc_stack_base += numfloats; + return addr; +} +static void _qpc_stack_pop(int numfloats) +{ + _qpc_stack_base -= numfloats; +} + +// qpc encoder function (internal use) ---------------------------------------------------------- +unsigned char* _qpc_encode(unsigned char* y, unsigned char* x) +{ + // Non recursive polar encoder + // Same architecture of a fast fourier transform + // in which the fft butteflies are replaced by the polar transform + // butterflies + + int k, j, m; + int groups; + int bfypergroup; + int stepbfy; + int stepgroup; + int basegroup; + int basebfy; + + memcpy(y, x, QPC_N); + + for (k = 0; k < QPC_LOG2N; k++) { + groups = 1 << (QPC_LOG2N - 1 - k); + stepbfy = bfypergroup = 1 << k; + stepgroup = stepbfy << 1; + basegroup = 0; + for (j = 0; j < groups; j++) { + basebfy = basegroup; + for (m = 0; m < bfypergroup; m++) { + // polar transform + y[basebfy + stepbfy] = y[basebfy + stepbfy] ^ y[basebfy]; + basebfy = basebfy + 1; + } + basegroup = basegroup + stepgroup; + } + } + + return y; +} + +// qpc polar decoder (internal use )-------------------------------------------------- +void _qpc_decode(unsigned char* xdec, unsigned char* ydec, const float* py, const unsigned char* f, const unsigned char* fsize, const int numrows) +{ + + if (numrows == 1) { + if (fsize[0] == 0) { + // dbgprintf_vector_float("py", py, QPC_Q); + + // frozen symbol + xdec[0] = pdf_max(py); + ydec[0] = f[0]; + } + else { + // fsize = 1 => information symbol + xdec[0] = pdf_max(py); + ydec[0] = xdec[0]; + } + + } + else { + + int k; + int nextrows = numrows >> 1; + int size = nextrows << QPC_LOG2Q; + + // upper block variables + unsigned char* xdech = xdec + nextrows; + unsigned char* ydech = ydec + nextrows; + const unsigned char* fh = f + nextrows; + const unsigned char* fsizeh = fsize + nextrows; + + // Step 1. + // stack and init variables used in the recursion + + float* pyl = _qpc_stack_push(size); + memcpy(pyl, py, size * sizeof(float)); + + float* pyh = _qpc_stack_push(size); + memcpy(pyh, py + size, size * sizeof(float)); + + // Step 2. Recursion on upper block + // Forward pdf convolutions for the upper block + // (place in the lower part of py the convolution of lower and higher blocks) + +// float* pyh = py + size; +// pdfarray_conv(py, pyl, pyh, nextrows); // convolution overwriting the lower block of py which is not needed + + pdfarray_conv(pyh, pyl, pyh, nextrows); + _qpc_decode(xdech, ydech, pyh, fh, fsizeh, nextrows); + + // Step 3. compute pdfs in the lower block + pdfarray_convhard(pyh, py+size, ydech,nextrows); // dst ptr must be different form src ptr + pdfarray_mul(pyl, pyl, pyh, nextrows); + // we don't need pyh anymore + _qpc_stack_pop(size); + + // Step 4. Recursion on the lower block + _qpc_decode(xdec, ydec, pyl, f, fsize, nextrows); + // we don't need pyl anymore + _qpc_stack_pop(size); + + // Step 5. Update backward results + // xdec is already ok, we need just to update ydech + for (k = 0; k < nextrows; k++) + ydech[k] = ydech[k] ^ ydec[k]; + + } + + +} + +// Public encoding/decoding functions ------------------------------------------------ +void qpc_encode(unsigned char* y, const unsigned char* x) +{ + + // map information symbols + int kk, pos; + for (kk = 0; kk < QPC_K; kk++) { + pos = qpccode.xpos[kk]; + qpccode.f[pos] = x[kk]; + } + // do polar encoding + _qpc_encode(y, qpccode.f); +} +void qpc_decode(unsigned char* xdec, unsigned char* ydec, float* py) +{ + int k; + unsigned char x[QPC_N]; + + // set the first py row with know frozen (punctured) symbol + if (qpccode.np < qpccode.n) { + // assume that we punctured only the first output symbol + memset(py, 0, QPC_Q * sizeof(float)); + py[qpccode.f[0]] = 1.0f; + } + + // decode + _qpc_decode(x, ydec, py, qpccode.f, qpccode.fsize, QPC_N); + + // demap information symbols + for (k = 0; k < QPC_K; k++) + xdec[k] = x[qpccode.xpos[k]]; + +} diff --git a/lib/superfox/qpc/np_qpc.h b/lib/superfox/qpc/np_qpc.h index a93bf1bc..0fa440e7 100755 --- a/lib/superfox/qpc/np_qpc.h +++ b/lib/superfox/qpc/np_qpc.h @@ -1,62 +1,62 @@ -// ------------------------------------------------------------------------------ -// np_qpc.h -// Q-Ary Polar Codes encoding/decoding functions -// -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ -// -// This source 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 file 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 source distribution. -// If not, see . - - - -#ifndef _np_qpc_h_ -#define _np_qpc_h_ - -#define QPC_LOG2N 7 // log2(codeword length) (not punctured) -#define QPC_N (1< frozen) -} qpccode_ds; - -#ifdef __cplusplus -extern "C" -{ -#endif - - void qpc_encode(unsigned char* y, const unsigned char* x); - void qpc_decode(unsigned char* xdec, unsigned char* ydec, float* py); - - unsigned char* _qpc_encode(unsigned char* y, unsigned char* x); - void _qpc_decode(unsigned char* xdec, unsigned char* ydec, - const float* py, const unsigned char* f, const unsigned char* fsize, - const int numrows); - - extern qpccode_ds qpccode; - -#ifdef __cplusplus -} -#endif - -#endif // _np_qpc_h_ +// ------------------------------------------------------------------------------ +// np_qpc.h +// Q-Ary Polar Codes encoding/decoding functions +// +// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy +// ------------------------------------------------------------------------------ +// +// This source 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 file 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 source distribution. +// If not, see . + + + +#ifndef _np_qpc_h_ +#define _np_qpc_h_ + +#define QPC_LOG2N 7 // log2(codeword length) (not punctured) +#define QPC_N (1< frozen) +} qpccode_ds; + +#ifdef __cplusplus +extern "C" +{ +#endif + + void qpc_encode(unsigned char* y, const unsigned char* x); + void qpc_decode(unsigned char* xdec, unsigned char* ydec, float* py); + + unsigned char* _qpc_encode(unsigned char* y, unsigned char* x); + void _qpc_decode(unsigned char* xdec, unsigned char* ydec, + const float* py, const unsigned char* f, const unsigned char* fsize, + const int numrows); + + extern qpccode_ds qpccode; + +#ifdef __cplusplus +} +#endif + +#endif // _np_qpc_h_ diff --git a/lib/superfox/qpc/np_rnd.c b/lib/superfox/qpc/np_rnd.c index 1d792c6b..df2afacc 100755 --- a/lib/superfox/qpc/np_rnd.c +++ b/lib/superfox/qpc/np_rnd.c @@ -1,135 +1,135 @@ -// ------------------------------------------------------------------------------ -// np_rnd.c -// Functions to generate random numbers with uniform/gaussian probability distributions -// -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ -// -// This source 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 file 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 source distribution. -// If not, see . - -#include "np_rnd.h" - -#if _WIN32 // note the underscore: without it, it's not msdn official! - // Windows (x64 and x86) - #include // required only for GetTickCount(...) - #define K_RAND_MAX UINT_MAX -#elif _SVID_SOURCE || _XOPEN_SOURCE || __unix__ || (defined (__APPLE__) && defined(__MACH__)) /* POSIX or Unix or Apple */ - #include - #define rand_s(x) (*x)=(unsigned int)lrand48() // returns unsigned integers in the range 0..0x7FFFFFFF - #define K_RAND_MAX 0x7FFFFFFF // that's the max number - // generated by lrand48 -#else - #error "No good quality PRNG found" -#endif - - -void np_normrnd_real(float *dst, int nitems, float mean, float stdev) -{ - // for odd or even nitems - - unsigned int r; - float phi=0, u=0; - int set = 0; - float scalephi = (M_2PI / (1.0f + K_RAND_MAX)); - float scaleu = (1.0f / (1.0f + K_RAND_MAX)); - - - while (nitems--) - if (set==1) { - // generate a second normally distributed number - *dst++ = sinf(phi)*u*stdev+mean; - set = 0; - } - else { - // generate a uniform distributed phase phi in the range [0,2*PI) - rand_s((unsigned int*)&r); phi = scalephi * r; - - // generate a uniform distributed random number u in the range (0,1] - rand_s((unsigned int*)&r); u = scaleu * (1.0f + r); - - // generate normally distributed number - u = (float)sqrt(-2.0f * log(u)); - *dst++ = cosf(phi) * u * stdev + mean; - - set=1; - } -} - -void np_normrnd_cpx(float* dst, int nitems, float mean, float stdev) -{ - // as qpc_normrnd_real, but generates always nitems complex numbers (2*nitems reals) - - unsigned int r; - float phi = 0, u = 0; - // JHT int set = 0; - float scalephi = (M_2PI / (1.0f + K_RAND_MAX)); - float scaleu = (1.0f / (1.0f + K_RAND_MAX)); - - while (nitems--) { - // generate a uniform distributed phase phi in the range [0,2*PI) - rand_s((unsigned int*)&r); phi = scalephi * r; - - // generate a uniform distributed random number u in the range (0,1] - rand_s((unsigned int*)&r); u = scaleu * (1.0f + r); - - // generate normally distributed real and imaginary parts - u = (float)sqrt(-2.0f * log(u)); - *dst++ = cosf(phi) * u * stdev + mean; - *dst++ = sinf(phi) * u * stdev + mean; - } -} - -void np_unidrnd(unsigned int* dst, int nitems, unsigned int nsetsize) -{ - // generate uniform unsigned int random numbers in the range [0..nsetsize) - - unsigned int r; - float u; - float scaleu = (1.0f / (1.0f + K_RAND_MAX)); - - while (nitems--) { - rand_s((unsigned int*)&r); u = scaleu * nsetsize * r; - *dst++ = (unsigned int)floorf(u); - } - -} -void np_unidrnd_uc(unsigned char* dst, int nitems, unsigned char nsetsize) -{ - // generate uniform unsigned char random numbers in the range [0..nsetsize) - - unsigned int r; - float u; - float scaleu = (1.0f / (1.0f + K_RAND_MAX)); - - while (nitems--) { - rand_s((unsigned int*)&r); u = scaleu * nsetsize * r; - *dst++ = (unsigned char)floorf(u); - } - -} - -void np_unifrnd(float* dst, int nitems, float fmax) -{ - // generate uniform float random numbers in the range [0..fmax) - - unsigned int r; - float u; - float scaleu = (1.0f / (1.0f + K_RAND_MAX)); - - while (nitems--) { - rand_s((unsigned int*)&r); u = scaleu * fmax * r; - *dst++ = u; - } - -} +// ------------------------------------------------------------------------------ +// np_rnd.c +// Functions to generate random numbers with uniform/gaussian probability distributions +// +// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy +// ------------------------------------------------------------------------------ +// +// This source 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 file 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 source distribution. +// If not, see . + +#include "np_rnd.h" + +#if _WIN32 // note the underscore: without it, it's not msdn official! + // Windows (x64 and x86) + #include // required only for GetTickCount(...) + #define K_RAND_MAX UINT_MAX +#elif _SVID_SOURCE || _XOPEN_SOURCE || __unix__ || (defined (__APPLE__) && defined(__MACH__)) /* POSIX or Unix or Apple */ + #include + #define rand_s(x) (*x)=(unsigned int)lrand48() // returns unsigned integers in the range 0..0x7FFFFFFF + #define K_RAND_MAX 0x7FFFFFFF // that's the max number + // generated by lrand48 +#else + #error "No good quality PRNG found" +#endif + + +void np_normrnd_real(float *dst, int nitems, float mean, float stdev) +{ + // for odd or even nitems + + unsigned int r; + float phi=0, u=0; + int set = 0; + float scalephi = (M_2PI / (1.0f + K_RAND_MAX)); + float scaleu = (1.0f / (1.0f + K_RAND_MAX)); + + + while (nitems--) + if (set==1) { + // generate a second normally distributed number + *dst++ = sinf(phi)*u*stdev+mean; + set = 0; + } + else { + // generate a uniform distributed phase phi in the range [0,2*PI) + rand_s((unsigned int*)&r); phi = scalephi * r; + + // generate a uniform distributed random number u in the range (0,1] + rand_s((unsigned int*)&r); u = scaleu * (1.0f + r); + + // generate normally distributed number + u = (float)sqrt(-2.0f * log(u)); + *dst++ = cosf(phi) * u * stdev + mean; + + set=1; + } +} + +void np_normrnd_cpx(float* dst, int nitems, float mean, float stdev) +{ + // as qpc_normrnd_real, but generates always nitems complex numbers (2*nitems reals) + + unsigned int r; + float phi = 0, u = 0; + // JHT int set = 0; + float scalephi = (M_2PI / (1.0f + K_RAND_MAX)); + float scaleu = (1.0f / (1.0f + K_RAND_MAX)); + + while (nitems--) { + // generate a uniform distributed phase phi in the range [0,2*PI) + rand_s((unsigned int*)&r); phi = scalephi * r; + + // generate a uniform distributed random number u in the range (0,1] + rand_s((unsigned int*)&r); u = scaleu * (1.0f + r); + + // generate normally distributed real and imaginary parts + u = (float)sqrt(-2.0f * log(u)); + *dst++ = cosf(phi) * u * stdev + mean; + *dst++ = sinf(phi) * u * stdev + mean; + } +} + +void np_unidrnd(unsigned int* dst, int nitems, unsigned int nsetsize) +{ + // generate uniform unsigned int random numbers in the range [0..nsetsize) + + unsigned int r; + float u; + float scaleu = (1.0f / (1.0f + K_RAND_MAX)); + + while (nitems--) { + rand_s((unsigned int*)&r); u = scaleu * nsetsize * r; + *dst++ = (unsigned int)floorf(u); + } + +} +void np_unidrnd_uc(unsigned char* dst, int nitems, unsigned char nsetsize) +{ + // generate uniform unsigned char random numbers in the range [0..nsetsize) + + unsigned int r; + float u; + float scaleu = (1.0f / (1.0f + K_RAND_MAX)); + + while (nitems--) { + rand_s((unsigned int*)&r); u = scaleu * nsetsize * r; + *dst++ = (unsigned char)floorf(u); + } + +} + +void np_unifrnd(float* dst, int nitems, float fmax) +{ + // generate uniform float random numbers in the range [0..fmax) + + unsigned int r; + float u; + float scaleu = (1.0f / (1.0f + K_RAND_MAX)); + + while (nitems--) { + rand_s((unsigned int*)&r); u = scaleu * fmax * r; + *dst++ = u; + } + +} diff --git a/lib/superfox/qpc/np_rnd.h b/lib/superfox/qpc/np_rnd.h index 6bc27b20..47a71a3c 100755 --- a/lib/superfox/qpc/np_rnd.h +++ b/lib/superfox/qpc/np_rnd.h @@ -1,59 +1,59 @@ -// ------------------------------------------------------------------------------ -// np_rnd.h -// Functions to generate random numbers with uniform/gaussian probability distributions -// -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ -// -// This source 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 file 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 source distribution. -// If not, see . - -#ifndef _np_rnd_h_ -#define _np_rnd_h_ - -#define _CRT_RAND_S -#include - -#define _USE_MATH_DEFINES -#include -#define M_2PI (2.0f*(float)M_PI) - -#ifdef __cplusplus -extern "C" { -#endif - -// generate a random array of real numbers with a gaussian distribution of given mean and stdev -void np_normrnd_real(float *dst, int nitems, float mean, float stdev); - -// generate a random array of nitems complex numbers with a gaussian distribution of given mean and stdev -void np_normrnd_cpx(float* dst, int nitems, float mean, float stdev); - -// generate a random array of nitems unsigned int numbers with uniform distribution -// in the range [0 .. nsetsize) -void np_unidrnd(unsigned int* dst, int nitems, unsigned int nsetsize); - -// generate a random array of nitems unsigned chars numbers with uniform distribution -// in the range [0 .. nsetsize) -void np_unidrnd_uc(unsigned char* dst, int nitems, unsigned char nsetsize); - -// generate a random array of nitems float numbers with uniform distribution -// in the range [0 .. fmax) -void np_unifrnd(float* dst, int nitems, float fmax); - - -#ifdef __cplusplus -} -#endif - -#endif // _np_rnd_h_ - +// ------------------------------------------------------------------------------ +// np_rnd.h +// Functions to generate random numbers with uniform/gaussian probability distributions +// +// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy +// ------------------------------------------------------------------------------ +// +// This source 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 file 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 source distribution. +// If not, see . + +#ifndef _np_rnd_h_ +#define _np_rnd_h_ + +#define _CRT_RAND_S +#include + +#define _USE_MATH_DEFINES +#include +#define M_2PI (2.0f*(float)M_PI) + +#ifdef __cplusplus +extern "C" { +#endif + +// generate a random array of real numbers with a gaussian distribution of given mean and stdev +void np_normrnd_real(float *dst, int nitems, float mean, float stdev); + +// generate a random array of nitems complex numbers with a gaussian distribution of given mean and stdev +void np_normrnd_cpx(float* dst, int nitems, float mean, float stdev); + +// generate a random array of nitems unsigned int numbers with uniform distribution +// in the range [0 .. nsetsize) +void np_unidrnd(unsigned int* dst, int nitems, unsigned int nsetsize); + +// generate a random array of nitems unsigned chars numbers with uniform distribution +// in the range [0 .. nsetsize) +void np_unidrnd_uc(unsigned char* dst, int nitems, unsigned char nsetsize); + +// generate a random array of nitems float numbers with uniform distribution +// in the range [0 .. fmax) +void np_unifrnd(float* dst, int nitems, float fmax); + + +#ifdef __cplusplus +} +#endif + +#endif // _np_rnd_h_ + diff --git a/lib/superfox/qpc/qpc_fwht.c b/lib/superfox/qpc/qpc_fwht.c index 92cb7c2b..77411211 100755 --- a/lib/superfox/qpc/qpc_fwht.c +++ b/lib/superfox/qpc/qpc_fwht.c @@ -1,233 +1,233 @@ -// ------------------------------------------------------------------------------ -// qpc_fwht.c -// Fast Walsh-Hadamard Transforms for q-ary polar codes -// -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ -// -// This source 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 file 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 source distribution. -// If not, see . - -#include "qpc_fwht.h" - -static void _qpc_sumdiff8_16(float* y, float* t) -{ - y[0] = t[0] + t[16]; - y[16] = t[0] - t[16]; - - y[1] = t[1] + t[17]; - y[17] = t[1] - t[17]; - - y[2] = t[2] + t[18]; - y[18] = t[2] - t[18]; - - y[3] = t[3] + t[19]; - y[19] = t[3] - t[19]; - - y[4] = t[4] + t[20]; - y[20] = t[4] - t[20]; - - y[5] = t[5] + t[21]; - y[21] = t[5] - t[21]; - - y[6] = t[6] + t[22]; - y[22] = t[6] - t[22]; - - y[7] = t[7] + t[23]; - y[23] = t[7] - t[23]; - -} -static void _qpc_sumdiff8_32(float* y, float* t) -{ - y[0] = t[0] + t[32]; - y[32] = t[0] - t[32]; - - y[1] = t[1] + t[33]; - y[33] = t[1] - t[33]; - - y[2] = t[2] + t[34]; - y[34] = t[2] - t[34]; - - y[3] = t[3] + t[35]; - y[35] = t[3] - t[35]; - - y[4] = t[4] + t[36]; - y[36] = t[4] - t[36]; - - y[5] = t[5] + t[37]; - y[37] = t[5] - t[37]; - - y[6] = t[6] + t[38]; - y[38] = t[6] - t[38]; - - y[7] = t[7] + t[39]; - y[39] = t[7] - t[39]; - -} -static void _qpc_sumdiff8_64(float* y, float* t) -{ - y[0] = t[0] + t[64]; - y[64] = t[0] - t[64]; - - y[1] = t[1] + t[65]; - y[65] = t[1] - t[65]; - - y[2] = t[2] + t[66]; - y[66] = t[2] - t[66]; - - y[3] = t[3] + t[67]; - y[67] = t[3] - t[67]; - - y[4] = t[4] + t[68]; - y[68] = t[4] - t[68]; - - y[5] = t[5] + t[69]; - y[69] = t[5] - t[69]; - - y[6] = t[6] + t[70]; - y[70] = t[6] - t[70]; - - y[7] = t[7] + t[71]; - y[71] = t[7] - t[71]; - -} - -float* qpc_fwht8(float* y, float* x) -{ - float t[8]; - - // first stage - y[0] = x[0] + x[1]; - y[1] = x[0] - x[1]; - - y[2] = x[2] + x[3]; - y[3] = x[2] - x[3]; - - y[4] = x[4] + x[5]; - y[5] = x[4] - x[5]; - - y[6] = x[6] + x[7]; - y[7] = x[6] - x[7]; - - // second stage - t[0] = y[0] + y[2]; - t[2] = y[0] - y[2]; - - t[1] = y[1] + y[3]; - t[3] = y[1] - y[3]; - - t[4] = y[4] + y[6]; - t[6] = y[4] - y[6]; - - t[5] = y[5] + y[7]; - t[7] = y[5] - y[7]; - - // third stage - y[0] = t[0] + t[4]; - y[4] = t[0] - t[4]; - - y[1] = t[1] + t[5]; - y[5] = t[1] - t[5]; - - y[2] = t[2] + t[6]; - y[6] = t[2] - t[6]; - - y[3] = t[3] + t[7]; - y[7] = t[3] - t[7]; - - return y; -} -float* qpc_fwht16(float* y, float* x) -{ - float t[16]; - - qpc_fwht8(t, x); - qpc_fwht8(t + 8, x + 8); - - y[0] = t[0] + t[8]; - y[8] = t[0] - t[8]; - - y[1] = t[1] + t[9]; - y[9] = t[1] - t[9]; - - y[2] = t[2] + t[10]; - y[10] = t[2] - t[10]; - - y[3] = t[3] + t[11]; - y[11] = t[3] - t[11]; - - y[4] = t[4] + t[12]; - y[12] = t[4] - t[12]; - - y[5] = t[5] + t[13]; - y[13] = t[5] - t[13]; - - y[6] = t[6] + t[14]; - y[14] = t[6] - t[14]; - - y[7] = t[7] + t[15]; - y[15] = t[7] - t[15]; - - return y; - -} -float* qpc_fwht32(float* y, float* x) -{ - float t[32]; - - qpc_fwht16(t, x); - qpc_fwht16(t + 16, x + 16); - - _qpc_sumdiff8_16(y, t); - _qpc_sumdiff8_16(y + 8, t + 8); - - return y; -} -float* qpc_fwht64(float* y, float* x) -{ - float t[64]; - - qpc_fwht32(t, x); - qpc_fwht32(t + 32, x + 32); - - _qpc_sumdiff8_32(y, t); - _qpc_sumdiff8_32(y + 8, t + 8); - _qpc_sumdiff8_32(y + 16, t + 16); - _qpc_sumdiff8_32(y + 24, t + 24); - - return y; -} -float* qpc_fwht128(float* y, float* x) -{ - float t[128]; - - qpc_fwht64(t, x); - qpc_fwht64(t + 64, x + 64); - - _qpc_sumdiff8_64(y, t); - _qpc_sumdiff8_64(y + 8, t + 8); - _qpc_sumdiff8_64(y + 16, t + 16); - _qpc_sumdiff8_64(y + 24, t + 24); - _qpc_sumdiff8_64(y + 32, t + 32); - _qpc_sumdiff8_64(y + 40, t + 40); - _qpc_sumdiff8_64(y + 48, t + 48); - _qpc_sumdiff8_64(y + 56, t + 56); - - return y; -} - -// functions over pdfs used by the decoder ----------------------------------------------------------- - - - - +// ------------------------------------------------------------------------------ +// qpc_fwht.c +// Fast Walsh-Hadamard Transforms for q-ary polar codes +// +// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy +// ------------------------------------------------------------------------------ +// +// This source 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 file 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 source distribution. +// If not, see . + +#include "qpc_fwht.h" + +static void _qpc_sumdiff8_16(float* y, float* t) +{ + y[0] = t[0] + t[16]; + y[16] = t[0] - t[16]; + + y[1] = t[1] + t[17]; + y[17] = t[1] - t[17]; + + y[2] = t[2] + t[18]; + y[18] = t[2] - t[18]; + + y[3] = t[3] + t[19]; + y[19] = t[3] - t[19]; + + y[4] = t[4] + t[20]; + y[20] = t[4] - t[20]; + + y[5] = t[5] + t[21]; + y[21] = t[5] - t[21]; + + y[6] = t[6] + t[22]; + y[22] = t[6] - t[22]; + + y[7] = t[7] + t[23]; + y[23] = t[7] - t[23]; + +} +static void _qpc_sumdiff8_32(float* y, float* t) +{ + y[0] = t[0] + t[32]; + y[32] = t[0] - t[32]; + + y[1] = t[1] + t[33]; + y[33] = t[1] - t[33]; + + y[2] = t[2] + t[34]; + y[34] = t[2] - t[34]; + + y[3] = t[3] + t[35]; + y[35] = t[3] - t[35]; + + y[4] = t[4] + t[36]; + y[36] = t[4] - t[36]; + + y[5] = t[5] + t[37]; + y[37] = t[5] - t[37]; + + y[6] = t[6] + t[38]; + y[38] = t[6] - t[38]; + + y[7] = t[7] + t[39]; + y[39] = t[7] - t[39]; + +} +static void _qpc_sumdiff8_64(float* y, float* t) +{ + y[0] = t[0] + t[64]; + y[64] = t[0] - t[64]; + + y[1] = t[1] + t[65]; + y[65] = t[1] - t[65]; + + y[2] = t[2] + t[66]; + y[66] = t[2] - t[66]; + + y[3] = t[3] + t[67]; + y[67] = t[3] - t[67]; + + y[4] = t[4] + t[68]; + y[68] = t[4] - t[68]; + + y[5] = t[5] + t[69]; + y[69] = t[5] - t[69]; + + y[6] = t[6] + t[70]; + y[70] = t[6] - t[70]; + + y[7] = t[7] + t[71]; + y[71] = t[7] - t[71]; + +} + +float* qpc_fwht8(float* y, float* x) +{ + float t[8]; + + // first stage + y[0] = x[0] + x[1]; + y[1] = x[0] - x[1]; + + y[2] = x[2] + x[3]; + y[3] = x[2] - x[3]; + + y[4] = x[4] + x[5]; + y[5] = x[4] - x[5]; + + y[6] = x[6] + x[7]; + y[7] = x[6] - x[7]; + + // second stage + t[0] = y[0] + y[2]; + t[2] = y[0] - y[2]; + + t[1] = y[1] + y[3]; + t[3] = y[1] - y[3]; + + t[4] = y[4] + y[6]; + t[6] = y[4] - y[6]; + + t[5] = y[5] + y[7]; + t[7] = y[5] - y[7]; + + // third stage + y[0] = t[0] + t[4]; + y[4] = t[0] - t[4]; + + y[1] = t[1] + t[5]; + y[5] = t[1] - t[5]; + + y[2] = t[2] + t[6]; + y[6] = t[2] - t[6]; + + y[3] = t[3] + t[7]; + y[7] = t[3] - t[7]; + + return y; +} +float* qpc_fwht16(float* y, float* x) +{ + float t[16]; + + qpc_fwht8(t, x); + qpc_fwht8(t + 8, x + 8); + + y[0] = t[0] + t[8]; + y[8] = t[0] - t[8]; + + y[1] = t[1] + t[9]; + y[9] = t[1] - t[9]; + + y[2] = t[2] + t[10]; + y[10] = t[2] - t[10]; + + y[3] = t[3] + t[11]; + y[11] = t[3] - t[11]; + + y[4] = t[4] + t[12]; + y[12] = t[4] - t[12]; + + y[5] = t[5] + t[13]; + y[13] = t[5] - t[13]; + + y[6] = t[6] + t[14]; + y[14] = t[6] - t[14]; + + y[7] = t[7] + t[15]; + y[15] = t[7] - t[15]; + + return y; + +} +float* qpc_fwht32(float* y, float* x) +{ + float t[32]; + + qpc_fwht16(t, x); + qpc_fwht16(t + 16, x + 16); + + _qpc_sumdiff8_16(y, t); + _qpc_sumdiff8_16(y + 8, t + 8); + + return y; +} +float* qpc_fwht64(float* y, float* x) +{ + float t[64]; + + qpc_fwht32(t, x); + qpc_fwht32(t + 32, x + 32); + + _qpc_sumdiff8_32(y, t); + _qpc_sumdiff8_32(y + 8, t + 8); + _qpc_sumdiff8_32(y + 16, t + 16); + _qpc_sumdiff8_32(y + 24, t + 24); + + return y; +} +float* qpc_fwht128(float* y, float* x) +{ + float t[128]; + + qpc_fwht64(t, x); + qpc_fwht64(t + 64, x + 64); + + _qpc_sumdiff8_64(y, t); + _qpc_sumdiff8_64(y + 8, t + 8); + _qpc_sumdiff8_64(y + 16, t + 16); + _qpc_sumdiff8_64(y + 24, t + 24); + _qpc_sumdiff8_64(y + 32, t + 32); + _qpc_sumdiff8_64(y + 40, t + 40); + _qpc_sumdiff8_64(y + 48, t + 48); + _qpc_sumdiff8_64(y + 56, t + 56); + + return y; +} + +// functions over pdfs used by the decoder ----------------------------------------------------------- + + + + diff --git a/lib/superfox/qpc/qpc_fwht.h b/lib/superfox/qpc/qpc_fwht.h index 733af7d4..30477a33 100755 --- a/lib/superfox/qpc/qpc_fwht.h +++ b/lib/superfox/qpc/qpc_fwht.h @@ -1,57 +1,57 @@ -// ------------------------------------------------------------------------------ -// qpc_fwht.h -// Fast Walsh-Hadamard Transforms for q-ary polar codes -// -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ -// -// This source 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 file 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 source distribution. -// If not, see . - -#ifndef _qpc_fwht_h_ -#define _qpc_fwht_h_ - -#include "np_qpc.h" - -#if QPC_LOG2Q==7 -#define qpc_fwht(a,b) qpc_fwht128(a,b) -#endif -#if QPC_LOG2Q==6 -#define qpc_fwht(a,b) qpc_fwht64(a,b) -#endif -#if QPC_LOG2Q==5 -#define qpc_fwht(a,b) qpc_fwht32(a,b) -#endif -#if QPC_LOG2Q==4 -#define qpc_fwht(a,b) qpc_fwht16(a,b) -#endif -// Note that it makes no sense to use fast convolutions -// for transforms that are less than 16 symbols in size. -// For such cases direct convolutions are faster. -#if QPC_LOG2Q==3 -#define qpc_fwht(a,b) qpc_fwht8(a,b) -#endif - -#ifdef __cplusplus -extern "C" { -#endif - float* qpc_fwht8(float* y, float* x); - float* qpc_fwht16(float* y, float* x); - float* qpc_fwht32(float* y, float* x); - float* qpc_fwht64(float* y, float* x); - float* qpc_fwht128(float* y, float* x); -#ifdef __cplusplus -} -#endif - -#endif // _qpc_fwht_h_ +// ------------------------------------------------------------------------------ +// qpc_fwht.h +// Fast Walsh-Hadamard Transforms for q-ary polar codes +// +// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy +// ------------------------------------------------------------------------------ +// +// This source 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 file 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 source distribution. +// If not, see . + +#ifndef _qpc_fwht_h_ +#define _qpc_fwht_h_ + +#include "np_qpc.h" + +#if QPC_LOG2Q==7 +#define qpc_fwht(a,b) qpc_fwht128(a,b) +#endif +#if QPC_LOG2Q==6 +#define qpc_fwht(a,b) qpc_fwht64(a,b) +#endif +#if QPC_LOG2Q==5 +#define qpc_fwht(a,b) qpc_fwht32(a,b) +#endif +#if QPC_LOG2Q==4 +#define qpc_fwht(a,b) qpc_fwht16(a,b) +#endif +// Note that it makes no sense to use fast convolutions +// for transforms that are less than 16 symbols in size. +// For such cases direct convolutions are faster. +#if QPC_LOG2Q==3 +#define qpc_fwht(a,b) qpc_fwht8(a,b) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + float* qpc_fwht8(float* y, float* x); + float* qpc_fwht16(float* y, float* x); + float* qpc_fwht32(float* y, float* x); + float* qpc_fwht64(float* y, float* x); + float* qpc_fwht128(float* y, float* x); +#ifdef __cplusplus +} +#endif + +#endif // _qpc_fwht_h_ diff --git a/lib/superfox/qpc/qpc_main.cpp b/lib/superfox/qpc/qpc_main.cpp index 4da5ed99..8e6c5955 100755 --- a/lib/superfox/qpc/qpc_main.cpp +++ b/lib/superfox/qpc/qpc_main.cpp @@ -1,198 +1,198 @@ - -// ------------------------------------------------------------------------------ -// qpc_main.cpp -// -// Test the WER performance of the QPC (127,50) Q=128 code -// -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ - -#include -#include -#include - -#include "dbgprintf.h" -#include "qpc_fwht.h" -#include "np_qpc.h" - -// for random numbers generation and NCFSK Rayleigh channel simulation -#include "np_rnd.h" -void qpc_channel(float* yout, unsigned char* y, float EsNo) -{ - // compute channel outputs amplitudes - - int k; - - // generate noise samples ----------------------------------------- - float sigman = 1.0f / sqrtf(2.0f); - np_normrnd_cpx(yout, QPC_N * QPC_Q, 0, sigman); - - // dbgprintf_rows_float("yout", yout, QPC_Q*2, QPC_N); - - // generate rayleigh distributed signal amplitudes ----------------- - float symamps[QPC_N * 2]; - np_normrnd_cpx(symamps, QPC_N, 0, sigman); - - // dbgprintf_vector_float("symamps", symamps, QPC_N*2); - - // normalize signal amps to unity ---------------------------------- - float pwr = 0.0f; - float* psymamps = symamps; - // compute sig power - for (k = 0; k < QPC_N; k++) { - pwr += psymamps[0] * psymamps[0] + psymamps[1] * psymamps[1]; - psymamps += 2; - } - pwr = pwr / QPC_N; - - // normalize to avg EsNo - float norm = sqrtf(EsNo/pwr); - psymamps = symamps; - for (k = 0; k < QPC_N; k++) { - psymamps[0] *= norm; - psymamps[1] *= norm; - psymamps += 2; - } - // dbgprintf_vector_float("symamps norm", symamps, QPC_N * 2); - - // add signal amplitudes to noise ----------------------------------- - float *pyout = yout; - psymamps= symamps; - for (k = 0; k < QPC_N; k++) { - pyout[y[k]<<1] += psymamps[0]; - pyout[(y[k]<<1)+1] += psymamps[1]; - pyout += (QPC_Q*2); - psymamps += 2; - } - - // dbgprintf_rows_float("s+n out", yout, QPC_Q * 2, QPC_N); - - return; -} -void qpc_likelihoods(float* py, float* yout, float EsNo, float No) -{ - // compute symbols likelihoods - // (rayleigh channel) - - int k, j; - - float norm = EsNo / (EsNo + 1) / No; - - // compute likelihoods from energies ---------------------------- - float* pybase; - float* ppyout = yout; - float normpwr; - float normpwrmax; - float pynorm; - - for (k = 0; k < QPC_N; k++) { - - // compute loglikelihoods and largest one from energies - pybase = py + k * QPC_Q; - normpwrmax = 0.0f; - for (j = 0; j < QPC_Q; j++) { - normpwr = norm * (ppyout[0] * ppyout[0] + ppyout[1] * ppyout[1]); - pybase[j] = normpwr; - if (normpwr > normpwrmax) - normpwrmax = normpwr; - ppyout += 2; - } - // subtract largest exponent - pynorm = 0.0f; - for (j = 0; j < QPC_Q; j++) { - pybase[j] = expf(pybase[j] - normpwrmax); - pynorm += pybase[j]; - } - // normalize to probabilities - pynorm = 1.0f / pynorm; - for (j = 0; j < QPC_Q; j++) - pybase[j] = pybase[j] * pynorm; - - } - - return; -} - -int main() -{ - int k; - - int NT = 20000; // number of transmissions to simultate - float EbNodB = 3.7f; // Eb/No to simulate - - - int kc = qpccode.k; - int np = qpccode.np; - float Rc = 1.0f * kc / np; - float Tm = 10.0f; // message duration - float Rb = 1.0f / Tm * (QPC_K - 2) * QPC_LOG2Q; // Bit rate assuming two symbols for CRC - - float EsNodB = EbNodB + 10.0f * log10f(Rc * QPC_LOG2Q); - - static unsigned char xin[QPC_K]; // input information symbols - static unsigned char xdec[QPC_K]; // decoded information symbols - static unsigned char y[QPC_N]; // encoded codeword - static unsigned char ydec[QPC_N]; // decoded codeword - - static float yout[QPC_N * QPC_Q * 2]; // channel complex amplitutes output - static float py[QPC_N * QPC_Q]; // channel output probabilities - - float EsNo = powf(10.0f, EsNodB / 10.0f); - float No = 1.0f; - - //JHT static float we; - static float wer; - - int kk; - - printf("QPC Word Error Rate Test\n"); - printf("Polar Code (%d,%d) Q=%d for the Rayleigh channel\n", qpccode.np, qpccode.k, qpccode.q); - printf("Eb/No = %.1f dB\n", EbNodB); - printf("Es/No = %.1f dB\n", EsNodB); - printf("Tmsg = %.1f s\n", Tm); - printf("Bit Rate = %.1f bit/s\n", Rb); - printf("SNR(2500) = %.1f dB\n\n", EbNodB + 10.0f * log10f(Rb/ 2500)); - - - for (k = 0; k < NT; k++) { - - np_unidrnd_uc(xin, QPC_K, QPC_Q); // generate random information symbols - - qpc_encode(y, xin); // encode - - // compute channel outputs and probabilities - - // Note that if the code is punctured (i.e. N=127) - // the first codeword symbol must not be transmitted over the channel - // Here, in order to avoid useless copies, - // the vector of likelihoods py continues to be a QPC_N*QPC_Q vector of floats. - // The first received symbol likelihoods should start always at offset QPC_Q. - // The first QPC_Q positions of py will be ignored (and set) by the decoder. - qpc_channel(yout, y, EsNo); - - // The decoder can't estimate the EsNo easily - // so we assume that in any case it is 5 dB - float EsNoDec = 3.16; - qpc_likelihoods(py, yout, EsNoDec, No); - - qpc_decode(xdec, ydec, py); // decode - - // count words in errors - for (kk = 0; kk < QPC_K; kk++) - if (xin[kk] ^ xdec[kk]) { - wer += 1; - break; - } - - // show the decoder performance every 200 codewords transmissions - if ((k % 200) == 0 && k>0) { - printf("k=%6d/%6d wer = %8.2E\n", k, NT, wer / k); - fflush(stdout); - } - - } - // show the final result - printf("k=%6d/%6d wer = %8.2E\n", k, NT, wer / k); - printf("\nAll done.\n"); - return 0; -} + +// ------------------------------------------------------------------------------ +// qpc_main.cpp +// +// Test the WER performance of the QPC (127,50) Q=128 code +// +// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy +// ------------------------------------------------------------------------------ + +#include +#include +#include + +#include "dbgprintf.h" +#include "qpc_fwht.h" +#include "np_qpc.h" + +// for random numbers generation and NCFSK Rayleigh channel simulation +#include "np_rnd.h" +void qpc_channel(float* yout, unsigned char* y, float EsNo) +{ + // compute channel outputs amplitudes + + int k; + + // generate noise samples ----------------------------------------- + float sigman = 1.0f / sqrtf(2.0f); + np_normrnd_cpx(yout, QPC_N * QPC_Q, 0, sigman); + + // dbgprintf_rows_float("yout", yout, QPC_Q*2, QPC_N); + + // generate rayleigh distributed signal amplitudes ----------------- + float symamps[QPC_N * 2]; + np_normrnd_cpx(symamps, QPC_N, 0, sigman); + + // dbgprintf_vector_float("symamps", symamps, QPC_N*2); + + // normalize signal amps to unity ---------------------------------- + float pwr = 0.0f; + float* psymamps = symamps; + // compute sig power + for (k = 0; k < QPC_N; k++) { + pwr += psymamps[0] * psymamps[0] + psymamps[1] * psymamps[1]; + psymamps += 2; + } + pwr = pwr / QPC_N; + + // normalize to avg EsNo + float norm = sqrtf(EsNo/pwr); + psymamps = symamps; + for (k = 0; k < QPC_N; k++) { + psymamps[0] *= norm; + psymamps[1] *= norm; + psymamps += 2; + } + // dbgprintf_vector_float("symamps norm", symamps, QPC_N * 2); + + // add signal amplitudes to noise ----------------------------------- + float *pyout = yout; + psymamps= symamps; + for (k = 0; k < QPC_N; k++) { + pyout[y[k]<<1] += psymamps[0]; + pyout[(y[k]<<1)+1] += psymamps[1]; + pyout += (QPC_Q*2); + psymamps += 2; + } + + // dbgprintf_rows_float("s+n out", yout, QPC_Q * 2, QPC_N); + + return; +} +void qpc_likelihoods(float* py, float* yout, float EsNo, float No) +{ + // compute symbols likelihoods + // (rayleigh channel) + + int k, j; + + float norm = EsNo / (EsNo + 1) / No; + + // compute likelihoods from energies ---------------------------- + float* pybase; + float* ppyout = yout; + float normpwr; + float normpwrmax; + float pynorm; + + for (k = 0; k < QPC_N; k++) { + + // compute loglikelihoods and largest one from energies + pybase = py + k * QPC_Q; + normpwrmax = 0.0f; + for (j = 0; j < QPC_Q; j++) { + normpwr = norm * (ppyout[0] * ppyout[0] + ppyout[1] * ppyout[1]); + pybase[j] = normpwr; + if (normpwr > normpwrmax) + normpwrmax = normpwr; + ppyout += 2; + } + // subtract largest exponent + pynorm = 0.0f; + for (j = 0; j < QPC_Q; j++) { + pybase[j] = expf(pybase[j] - normpwrmax); + pynorm += pybase[j]; + } + // normalize to probabilities + pynorm = 1.0f / pynorm; + for (j = 0; j < QPC_Q; j++) + pybase[j] = pybase[j] * pynorm; + + } + + return; +} + +int main() +{ + int k; + + int NT = 20000; // number of transmissions to simultate + float EbNodB = 3.7f; // Eb/No to simulate + + + int kc = qpccode.k; + int np = qpccode.np; + float Rc = 1.0f * kc / np; + float Tm = 10.0f; // message duration + float Rb = 1.0f / Tm * (QPC_K - 2) * QPC_LOG2Q; // Bit rate assuming two symbols for CRC + + float EsNodB = EbNodB + 10.0f * log10f(Rc * QPC_LOG2Q); + + static unsigned char xin[QPC_K]; // input information symbols + static unsigned char xdec[QPC_K]; // decoded information symbols + static unsigned char y[QPC_N]; // encoded codeword + static unsigned char ydec[QPC_N]; // decoded codeword + + static float yout[QPC_N * QPC_Q * 2]; // channel complex amplitutes output + static float py[QPC_N * QPC_Q]; // channel output probabilities + + float EsNo = powf(10.0f, EsNodB / 10.0f); + float No = 1.0f; + + //JHT static float we; + static float wer; + + int kk; + + printf("QPC Word Error Rate Test\n"); + printf("Polar Code (%d,%d) Q=%d for the Rayleigh channel\n", qpccode.np, qpccode.k, qpccode.q); + printf("Eb/No = %.1f dB\n", EbNodB); + printf("Es/No = %.1f dB\n", EsNodB); + printf("Tmsg = %.1f s\n", Tm); + printf("Bit Rate = %.1f bit/s\n", Rb); + printf("SNR(2500) = %.1f dB\n\n", EbNodB + 10.0f * log10f(Rb/ 2500)); + + + for (k = 0; k < NT; k++) { + + np_unidrnd_uc(xin, QPC_K, QPC_Q); // generate random information symbols + + qpc_encode(y, xin); // encode + + // compute channel outputs and probabilities + + // Note that if the code is punctured (i.e. N=127) + // the first codeword symbol must not be transmitted over the channel + // Here, in order to avoid useless copies, + // the vector of likelihoods py continues to be a QPC_N*QPC_Q vector of floats. + // The first received symbol likelihoods should start always at offset QPC_Q. + // The first QPC_Q positions of py will be ignored (and set) by the decoder. + qpc_channel(yout, y, EsNo); + + // The decoder can't estimate the EsNo easily + // so we assume that in any case it is 5 dB + float EsNoDec = 3.16; + qpc_likelihoods(py, yout, EsNoDec, No); + + qpc_decode(xdec, ydec, py); // decode + + // count words in errors + for (kk = 0; kk < QPC_K; kk++) + if (xin[kk] ^ xdec[kk]) { + wer += 1; + break; + } + + // show the decoder performance every 200 codewords transmissions + if ((k % 200) == 0 && k>0) { + printf("k=%6d/%6d wer = %8.2E\n", k, NT, wer / k); + fflush(stdout); + } + + } + // show the final result + printf("k=%6d/%6d wer = %8.2E\n", k, NT, wer / k); + printf("\nAll done.\n"); + return 0; +} diff --git a/lib/superfox/qpc/qpc_mod.f90 b/lib/superfox/qpc/qpc_mod.f90 index 069c77b1..931e5b3d 100755 --- a/lib/superfox/qpc/qpc_mod.f90 +++ b/lib/superfox/qpc/qpc_mod.f90 @@ -1,38 +1,38 @@ -module qpc_mod - interface - integer(c_int32_t) function nhash2 (xin, length, initval) bind(C, & - name="nhash2") - use iso_c_binding, only: c_signed_char, c_int64_t, c_int32_t - integer(c_int64_t), intent(in), value :: length - integer(c_signed_char), intent(in) :: xin(length) - integer(c_int32_t), intent(in), value :: initval - end function nhash2 - end interface - - interface - subroutine qpc_encode(y, xin) bind(C,name="qpc_encode") - use iso_c_binding, only: c_ptr, c_signed_char - integer(c_signed_char), intent(out) :: y(127) - integer(c_signed_char), intent(in) :: xin(50) - end subroutine qpc_encode - end interface - - interface - subroutine qpc_channel(yout, y, EsNo) bind(C,name="qpc_channel") - use iso_c_binding, only: c_float_complex, c_float, c_signed_char - complex(c_float_complex), intent(out) :: yout(128,128) - integer(c_signed_char), intent(out) :: y(127) - real(c_float), intent(in), value :: EsNo - end subroutine qpc_channel - end interface - - interface - subroutine qpc_decode(xdec, ydec, py) bind(C,name="qpc_decode") - use iso_c_binding, only: c_float, c_signed_char - real(c_float), intent(in) :: py(128,128) - integer(c_signed_char), intent(out) :: ydec(127) - integer(c_signed_char), intent(out) :: xdec(50) - end subroutine qpc_decode - end interface - -end module qpc_mod +module qpc_mod + interface + integer(c_int32_t) function nhash2 (xin, length, initval) bind(C, & + name="nhash2") + use iso_c_binding, only: c_signed_char, c_int64_t, c_int32_t + integer(c_int64_t), intent(in), value :: length + integer(c_signed_char), intent(in) :: xin(length) + integer(c_int32_t), intent(in), value :: initval + end function nhash2 + end interface + + interface + subroutine qpc_encode(y, xin) bind(C,name="qpc_encode") + use iso_c_binding, only: c_ptr, c_signed_char + integer(c_signed_char), intent(out) :: y(127) + integer(c_signed_char), intent(in) :: xin(50) + end subroutine qpc_encode + end interface + + interface + subroutine qpc_channel(yout, y, EsNo) bind(C,name="qpc_channel") + use iso_c_binding, only: c_float_complex, c_float, c_signed_char + complex(c_float_complex), intent(out) :: yout(128,128) + integer(c_signed_char), intent(out) :: y(127) + real(c_float), intent(in), value :: EsNo + end subroutine qpc_channel + end interface + + interface + subroutine qpc_decode(xdec, ydec, py) bind(C,name="qpc_decode") + use iso_c_binding, only: c_float, c_signed_char + real(c_float), intent(in) :: py(128,128) + integer(c_signed_char), intent(out) :: ydec(127) + integer(c_signed_char), intent(out) :: xdec(50) + end subroutine qpc_decode + end interface + +end module qpc_mod diff --git a/lib/superfox/qpc/qpc_n127k50q128.c b/lib/superfox/qpc/qpc_n127k50q128.c index c57bcd2b..3897cc83 100755 --- a/lib/superfox/qpc/qpc_n127k50q128.c +++ b/lib/superfox/qpc/qpc_n127k50q128.c @@ -1,60 +1,60 @@ -// ------------------------------------------------------------------------------ -// qpc_n127k50q128.c -// -// Defines the parameters of the q-ary polar code (127,50) Q=128 -// for WSJT-X -// ------------------------------------------------------------------------------ -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ -// -// WARNING: -// This source is NOT free software and it is licensed only for use with WSJT-X. -// No derived work is authorized to use this code without the written -// permission of his author (Nico Palermo / IV3NWV) who owns all the rights -// of this IP (intellectual property). -// -// This file is distributed ONLY for the purpose of documenting and making of -// public domain the encoding scheme used in WSJT-X so that the transmitted -// messages can be decoded by anybody. -// Anyway this does not imply that one could use the following tables in a -// derived work without an explicit and written authorization from his author. -// Any unauthorized use, as for any intellectual property, is simply -// illegal. -// ------------------------------------------------------------------------------- -#include "np_qpc.h" -qpccode_ds qpccode = { - 128, //n - 127, //np - 50, //k - 128, //q -{ - 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 16, 32, 17, 18, 64, 20, - 33, 34, 24, 7, 11, 36, 13, 19, 14, 65, 40, 21, 66, 22, 35, 68, - 25, 48, 37, 26, 72, 15, 38, 28, 41, 67, 23, 80, 42, 69, 49, 96, - 44, 27, 70, 50, 73, 39, 29, 52, 74, 30, 56, 81, 76, 43, 82, 84, - 97, 45, 71, 88, 98, 46, 100, 51, 104, 53, 75, 112, 54, 57, 99, 119, - 92, 77, 58, 117, 59, 83, 106, 31, 85, 108, 115, 116, 122, 125, 124, 91, - 61, 90, 89, 111, 78, 93, 94, 126, 86, 107, 110, 118, 121, 62, 120, 87, -105, 55, 114, 60, 127, 63, 103, 101, 123, 95, 102, 47, 109, 79, 113, 0 -}, -{ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}, -{ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, - 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, - 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -} -}; +// ------------------------------------------------------------------------------ +// qpc_n127k50q128.c +// +// Defines the parameters of the q-ary polar code (127,50) Q=128 +// for WSJT-X +// ------------------------------------------------------------------------------ +// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy +// ------------------------------------------------------------------------------ +// +// WARNING: +// This source is NOT free software and it is licensed only for use with WSJT-X. +// No derived work is authorized to use this code without the written +// permission of his author (Nico Palermo / IV3NWV) who owns all the rights +// of this IP (intellectual property). +// +// This file is distributed ONLY for the purpose of documenting and making of +// public domain the encoding scheme used in WSJT-X so that the transmitted +// messages can be decoded by anybody. +// Anyway this does not imply that one could use the following tables in a +// derived work without an explicit and written authorization from his author. +// Any unauthorized use, as for any intellectual property, is simply +// illegal. +// ------------------------------------------------------------------------------- +#include "np_qpc.h" +qpccode_ds qpccode = { + 128, //n + 127, //np + 50, //k + 128, //q +{ + 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 16, 32, 17, 18, 64, 20, + 33, 34, 24, 7, 11, 36, 13, 19, 14, 65, 40, 21, 66, 22, 35, 68, + 25, 48, 37, 26, 72, 15, 38, 28, 41, 67, 23, 80, 42, 69, 49, 96, + 44, 27, 70, 50, 73, 39, 29, 52, 74, 30, 56, 81, 76, 43, 82, 84, + 97, 45, 71, 88, 98, 46, 100, 51, 104, 53, 75, 112, 54, 57, 99, 119, + 92, 77, 58, 117, 59, 83, 106, 31, 85, 108, 115, 116, 122, 125, 124, 91, + 61, 90, 89, 111, 78, 93, 94, 126, 86, 107, 110, 118, 121, 62, 120, 87, +105, 55, 114, 60, 127, 63, 103, 101, 123, 95, 102, 47, 109, 79, 113, 0 +}, +{ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}, +{ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, + 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +} +}; diff --git a/lib/superfox/qpc/qpc_subs.c b/lib/superfox/qpc/qpc_subs.c index cc6625e8..ec5f8e44 100755 --- a/lib/superfox/qpc/qpc_subs.c +++ b/lib/superfox/qpc/qpc_subs.c @@ -1,115 +1,115 @@ -// ------------------------------------------------------------------------------ -// qpc_test.cpp -// -// Test the WER performance of the QPC (127,50) Q=128 code -// -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ - -#include -#include -#include -#include - -#include "dbgprintf.h" -#include "qpc_fwht.h" -#include "np_qpc.h" - -#include "nhash2.h" - -// for random numbers generation and NCFSK Rayleigh channel simulation -#include "np_rnd.h" -void qpc_channel(float* yout, unsigned char* y, float EsNo) -{ - // compute channel outputs amplitudes - - int k; - - // generate noise samples ----------------------------------------- - float sigman = 1.0f / sqrtf(2.0f); - np_normrnd_cpx(yout, QPC_N * QPC_Q, 0, sigman); - - // dbgprintf_rows_float("yout", yout, QPC_Q*2, QPC_N); - - // generate rayleigh distributed signal amplitudes ----------------- - float symamps[QPC_N * 2]; - np_normrnd_cpx(symamps, QPC_N, 0, sigman); - - // dbgprintf_vector_float("symamps", symamps, QPC_N*2); - - // normalize signal amps to unity ---------------------------------- - float pwr = 0.0f; - float* psymamps = symamps; - // compute sig power - for (k = 0; k < QPC_N; k++) { - pwr += psymamps[0] * psymamps[0] + psymamps[1] * psymamps[1]; - psymamps += 2; - } - pwr = pwr / QPC_N; - - // normalize to avg EsNo - float norm = sqrtf(EsNo/pwr); - psymamps = symamps; - for (k = 0; k < QPC_N; k++) { - psymamps[0] *= norm; - psymamps[1] *= norm; - psymamps += 2; - } - // dbgprintf_vector_float("symamps norm", symamps, QPC_N * 2); - - // add signal amplitudes to noise ----------------------------------- - float *pyout = yout; - psymamps= symamps; - for (k = 0; k < QPC_N; k++) { - pyout[y[k]<<1] += psymamps[0]; - pyout[(y[k]<<1)+1] += psymamps[1]; - pyout += (QPC_Q*2); - psymamps += 2; - } - - // dbgprintf_rows_float("s+n out", yout, QPC_Q * 2, QPC_N); - - return; -} -void qpc_likelihoods(float* py, float* yout, float EsNo, float No) -{ - // compute symbols likelihoods - // (rayleigh channel) - - int k, j; - - float norm = EsNo / (EsNo + 1) / No; - - // compute likelihoods from energies ---------------------------- - float* pybase; - float* ppyout = yout; - float normpwr; - float normpwrmax; - float pynorm; - - for (k = 0; k < QPC_N; k++) { - - // compute loglikelihoods and largest one from energies - pybase = py + k * QPC_Q; - normpwrmax = 0.0f; - for (j = 0; j < QPC_Q; j++) { - normpwr = norm * (ppyout[0] * ppyout[0] + ppyout[1] * ppyout[1]); - pybase[j] = normpwr; - if (normpwr > normpwrmax) - normpwrmax = normpwr; - ppyout += 2; - } - // subtract largest exponent - pynorm = 0.0f; - for (j = 0; j < QPC_Q; j++) { - pybase[j] = expf(pybase[j] - normpwrmax); - pynorm += pybase[j]; - } - // normalize to probabilities - pynorm = 1.0f / pynorm; - for (j = 0; j < QPC_Q; j++) - pybase[j] = pybase[j] * pynorm; - } - - return; -} +// ------------------------------------------------------------------------------ +// qpc_test.cpp +// +// Test the WER performance of the QPC (127,50) Q=128 code +// +// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy +// ------------------------------------------------------------------------------ + +#include +#include +#include +#include + +#include "dbgprintf.h" +#include "qpc_fwht.h" +#include "np_qpc.h" + +#include "nhash2.h" + +// for random numbers generation and NCFSK Rayleigh channel simulation +#include "np_rnd.h" +void qpc_channel(float* yout, unsigned char* y, float EsNo) +{ + // compute channel outputs amplitudes + + int k; + + // generate noise samples ----------------------------------------- + float sigman = 1.0f / sqrtf(2.0f); + np_normrnd_cpx(yout, QPC_N * QPC_Q, 0, sigman); + + // dbgprintf_rows_float("yout", yout, QPC_Q*2, QPC_N); + + // generate rayleigh distributed signal amplitudes ----------------- + float symamps[QPC_N * 2]; + np_normrnd_cpx(symamps, QPC_N, 0, sigman); + + // dbgprintf_vector_float("symamps", symamps, QPC_N*2); + + // normalize signal amps to unity ---------------------------------- + float pwr = 0.0f; + float* psymamps = symamps; + // compute sig power + for (k = 0; k < QPC_N; k++) { + pwr += psymamps[0] * psymamps[0] + psymamps[1] * psymamps[1]; + psymamps += 2; + } + pwr = pwr / QPC_N; + + // normalize to avg EsNo + float norm = sqrtf(EsNo/pwr); + psymamps = symamps; + for (k = 0; k < QPC_N; k++) { + psymamps[0] *= norm; + psymamps[1] *= norm; + psymamps += 2; + } + // dbgprintf_vector_float("symamps norm", symamps, QPC_N * 2); + + // add signal amplitudes to noise ----------------------------------- + float *pyout = yout; + psymamps= symamps; + for (k = 0; k < QPC_N; k++) { + pyout[y[k]<<1] += psymamps[0]; + pyout[(y[k]<<1)+1] += psymamps[1]; + pyout += (QPC_Q*2); + psymamps += 2; + } + + // dbgprintf_rows_float("s+n out", yout, QPC_Q * 2, QPC_N); + + return; +} +void qpc_likelihoods(float* py, float* yout, float EsNo, float No) +{ + // compute symbols likelihoods + // (rayleigh channel) + + int k, j; + + float norm = EsNo / (EsNo + 1) / No; + + // compute likelihoods from energies ---------------------------- + float* pybase; + float* ppyout = yout; + float normpwr; + float normpwrmax; + float pynorm; + + for (k = 0; k < QPC_N; k++) { + + // compute loglikelihoods and largest one from energies + pybase = py + k * QPC_Q; + normpwrmax = 0.0f; + for (j = 0; j < QPC_Q; j++) { + normpwr = norm * (ppyout[0] * ppyout[0] + ppyout[1] * ppyout[1]); + pybase[j] = normpwr; + if (normpwr > normpwrmax) + normpwrmax = normpwr; + ppyout += 2; + } + // subtract largest exponent + pynorm = 0.0f; + for (j = 0; j < QPC_Q; j++) { + pybase[j] = expf(pybase[j] - normpwrmax); + pynorm += pybase[j]; + } + // normalize to probabilities + pynorm = 1.0f / pynorm; + for (j = 0; j < QPC_Q; j++) + pybase[j] = pybase[j] * pynorm; + } + + return; +} -- cgit v1.2.3