| 1 |
2 |
dgisselq |
////////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
//
|
| 3 |
|
|
// Filename: twoc.cpp
|
| 4 |
|
|
//
|
| 5 |
|
|
// Project: A Doubletime Pipelined FFT
|
| 6 |
|
|
//
|
| 7 |
|
|
// Purpose: Some various two's complement related C++ helper routines.
|
| 8 |
|
|
// Specifically, these help extract signed numbers from
|
| 9 |
|
|
// packed bitfields, while guaranteeing that the upper bits
|
| 10 |
|
|
// are properly sign extended (or not) as desired.
|
| 11 |
|
|
//
|
| 12 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
| 13 |
|
|
// Gisselquist Technology, LLC
|
| 14 |
|
|
//
|
| 15 |
|
|
///////////////////////////////////////////////////////////////////////////
|
| 16 |
|
|
//
|
| 17 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
| 18 |
|
|
//
|
| 19 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
| 20 |
|
|
// modify it under the terms of the GNU General Public License as published
|
| 21 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
| 22 |
|
|
// your option) any later version.
|
| 23 |
|
|
//
|
| 24 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
| 25 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
| 26 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 27 |
|
|
// for more details.
|
| 28 |
|
|
//
|
| 29 |
|
|
// You should have received a copy of the GNU General Public License along
|
| 30 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
| 31 |
|
|
// target there if the PDF file isn't present.) If not, see
|
| 32 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
| 33 |
|
|
//
|
| 34 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
| 35 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
| 36 |
|
|
//
|
| 37 |
|
|
//
|
| 38 |
|
|
///////////////////////////////////////////////////////////////////////////
|
| 39 |
|
|
#include "twoc.h"
|
| 40 |
|
|
|
| 41 |
|
|
long sbits(const long val, const int bits) {
|
| 42 |
|
|
long r;
|
| 43 |
|
|
|
| 44 |
|
|
r = val & ((1l<<bits)-1);
|
| 45 |
|
|
if (r & (1l << (bits-1)))
|
| 46 |
|
|
r |= (-1l << bits);
|
| 47 |
|
|
return r;
|
| 48 |
|
|
}
|
| 49 |
|
|
|
| 50 |
|
|
unsigned long ubits(const long val, const int bits) {
|
| 51 |
|
|
unsigned long r = val & ((1l<<bits)-1);
|
| 52 |
|
|
return r;
|
| 53 |
|
|
}
|
| 54 |
|
|
|
| 55 |
|
|
|