reduce flash footprint by smarter code generation #4163

This commit is contained in:
rusefillc 2022-12-03 00:51:41 -05:00
parent 8ad27f100f
commit 5e62595d69
6 changed files with 59 additions and 3 deletions

View File

@ -236,8 +236,6 @@ float atoff(const char *param) {
return integerPart + decimal / divider;
}
#define TO_LOWER(x) (((x)>='A' && (x)<='Z') ? (x) - 'A' + 'a' : (x))
bool strEqualCaseInsensitive(const char *str1, const char *str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
@ -257,6 +255,19 @@ int mytolower(const char c) {
return TO_LOWER(c);
}
int djb2lowerCase(const char *str) {
unsigned long hash = 5381;
int c;
while (c = *str++) {
c = TO_LOWER(c);
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash;
}
bool strEqual(const char *str1, const char *str2) {
// todo: there must be a standard function?!
int len1 = strlen(str1);

View File

@ -12,6 +12,9 @@
#include <rusefi/arrays.h>
#endif
#define TO_LOWER(x) (((x)>='A' && (x)<='Z') ? (x) - 'A' + 'a' : (x))
int djb2lowerCase(const char *str);
#define _MAX_FILLER 11
// http://en.wikipedia.org/wiki/Endianness

View File

@ -0,0 +1,17 @@
package com.rusefi.output;
public class HashUtil {
public static int hash(String string) {
return djb2lowerCase(string);
}
public static int djb2lowerCase(String string) {
int hash = 5381;
for (int i = 0; i < string.length(); i++) {
char c = Character.toLowerCase(string.charAt(i));
hash = ((hash << 5) + hash) + (byte) c/* hash * 33 + c */;
}
return hash;
}
}

View File

@ -0,0 +1,16 @@
package com.rusefi.test;
import com.rusefi.output.HashUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HashTest {
@Test
public void testdjb2() {
assertEquals(HashUtil.djb2lowerCase("Hello1"), 30950378);
assertEquals(HashUtil.djb2lowerCase("Hello2"), 30950379);
assertEquals(HashUtil.djb2lowerCase("HELLO2"), 30950379);
}
}

View File

@ -0,0 +1,8 @@
#include "pch.h"
//#include "efilib.h"
TEST(util, hash) {
ASSERT_EQ(djb2lowerCase("Hello1"), 30950378);
ASSERT_EQ(djb2lowerCase("Hello2"), 30950379);
ASSERT_EQ(djb2lowerCase("HELLO2"), 30950379);
}

View File

@ -3,6 +3,7 @@
CPPSRC += $(PROJECT_DIR)/../unit_tests/tests/util/test_buffered_writer.cpp \
$(PROJECT_DIR)/../unit_tests/tests/util/test_error_accumulator.cpp \
$(PROJECT_DIR)/../unit_tests/tests/util/test_exp_average.cpp \
$(PROJECT_DIR)/../unit_tests/tests/util/test_hash.cpp \
INCDIR += $(PROJECT_DIR)/controllers/system