From fc5641cb69f77ff83a7265679c26f0c25fc4e2ef Mon Sep 17 00:00:00 2001 From: Christopher Young Date: Tue, 21 Jun 2016 14:54:59 -0400 Subject: [PATCH] Integrate @toofishes improvement: Unroll loop in convert_to_phi. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit “This yields a 10% CPU savings vs the original single value per loop iteration implementation on a Raspberry Pi 2 Model B.” https://github.com/mutability/dump978/commit/b0958f4305d2c7355d07fa9b938 8f6ad1b31b161 --- dump978/dump978.c | 20 ++++++++++++++++---- uatparse/uatparse.go | 13 +++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/dump978/dump978.c b/dump978/dump978.c index 1de85ffc..6f61ac72 100644 --- a/dump978/dump978.c +++ b/dump978/dump978.c @@ -143,11 +143,23 @@ void make_atan2_table(void) { } } -static void convert_to_phi(uint16_t *dest, uint16_t *src, int n) { - int i; +static void convert_to_phi(uint16_t *buffer, int n) +{ + int i; - for (i = 0; i < n; ++i) - dest[i] = iqphase[src[i]]; + // unroll the loop. n is always > 2048, usually 36864 + for (i = 0; i+8 <= n; i += 8) { + buffer[i] = iqphase[buffer[i]]; + buffer[i+1] = iqphase[buffer[i+1]]; + buffer[i+2] = iqphase[buffer[i+2]]; + buffer[i+3] = iqphase[buffer[i+3]]; + buffer[i+4] = iqphase[buffer[i+4]]; + buffer[i+5] = iqphase[buffer[i+5]]; + buffer[i+6] = iqphase[buffer[i+6]]; + buffer[i+7] = iqphase[buffer[i+7]]; + } + for (; i < n; ++i) + buffer[i] = iqphase[buffer[i]]; } static void calc_power(uint16_t *samples, int len) { // sets signal_strength to scaled amplitude. 0 = no signal, 1000 = saturated receiver on all samples in measurement. diff --git a/uatparse/uatparse.go b/uatparse/uatparse.go index c5c11e87..f6bb4fd3 100644 --- a/uatparse/uatparse.go +++ b/uatparse/uatparse.go @@ -599,9 +599,14 @@ func New(buf string) (*UATMsg, error) { buf = strings.Trim(buf, "\r\n") // Remove newlines. x := strings.Split(buf, ";") // We want to discard everything before the first ';'. + if len(x) < 2 { + return ret, errors.New(fmt.Sprintf("New UATMsg: Invalid format (%s).", buf)) + } + /* - RS_Err int - SignalStrength int + Parse _;rs=?;ss=? - if available. + RS_Err int + SignalStrength int */ ret.SignalStrength = -1 ret.RS_Err = -1 @@ -628,10 +633,10 @@ func New(buf string) (*UATMsg, error) { } if s[0] != '+' { // Only want + ("Uplink") messages currently. - (Downlink) or messages that start with other are discarded. - return ret, errors.New("New UATMsg: expecting uplink frames.") + return ret, errors.New("New UATMsg: expecting uplink frame.") } - s = s[1:] + s = s[1:] // Remove the preceding '+' or '-' character. // Convert the hex string into a byte array. frame := make([]byte, UPLINK_FRAME_DATA_BYTES)