Skip to content

Commit 1c60095

Browse files
committed
patch intrinsics
1 parent 1a31fd9 commit 1c60095

5 files changed

+90
-99
lines changed

platformio_tasmota32.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ lib_ignore = ${esp32_defaults.lib_ignore}
8282
ccronexpr
8383

8484
[core32]
85-
platform = https://github.com/tasmota/platform-espressif32/releases/download/2024.07.11/platform-espressif32.zip
85+
platform = https://github.com/tasmota/platform-espressif32/releases/download/2024.09.10/platform-espressif32.zip
8686
platform_packages =
8787
build_unflags = ${esp32_defaults.build_unflags}
8888
build_flags = ${esp32_defaults.build_flags}

tasmota/Plugins/intrinsics.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// floating point intrinsics hook
2+
3+
// 36 bytes code about 60 bytes in total with vector
4+
// 16 instructions 30 in total on esp32 == 4,166 ns * 30 = 130 ns addition takes 8,7 us = 15 % slower
5+
MODULE_PART float __addsf3(float a, float b) {
6+
SETMINREGS
7+
return fadd(a,b);
8+
}
9+
10+
MODULE_PART float __subsf3(float a, float b) {
11+
SETMINREGS
12+
return fdiff(a,b);
13+
}
14+
15+
MODULE_PART float __mulsf3(float a, float b) {
16+
SETMINREGS
17+
return fmul(a,b);
18+
}
19+
20+
MODULE_PART float __divsf3(float a, float b) {
21+
SETMINREGS
22+
return fdiv(a,b);
23+
}
24+
25+
26+
27+
28+
/* 36 bytes __addsf3
29+
40205a30: e0c112 addi a1, a1, -32
30+
40205a33: 076102 s32i a0, a1, 28
31+
40205a36: 0661c2 s32i a12, a1, 24
32+
40205a39: 006132 s32i a3, a1, 0
33+
40205a3c: 02cd mov.n a12, a2
34+
40205a3e: fb6c05 call0 40201100 <gettbl>
35+
40205a41: 1228 l32i.n a2, a2, 4
36+
40205a43: 0138 l32i.n a3, a1, 0
37+
40205a45: 2b2242 l32i a4, a2, 172
38+
40205a48: 0c2d mov.n a2, a12
39+
40205a4a: 0004c0 callx0 a4
40+
40205a4d: 7108 l32i.n a0, a1, 28
41+
40205a4f: 61c8 l32i.n a12, a1, 24
42+
40205a51: 20c112 addi a1, a1, 32
43+
40205a54: f00d ret.n
44+
*/
45+
46+
MODULE_PART int __nesf2(float a, float b) {
47+
SETMINREGS
48+
return 1;
49+
}

tasmota/Plugins/patch_buildtins.py

+33-69
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
org_pos = 0
2626
copy_pos = 0
2727

28-
def string_to_array(s):
29-
return [c for c in s]
30-
3128
def array_find(index, arr1, arr2):
3229
m = len(arr1)
3330
n = len(arr2)
@@ -38,7 +35,7 @@ def array_find(index, arr1, arr2):
3835
#copy = arr1[i:i+n+3]
3936
#print(copy)
4037
if arr1[i+n]==0:
41-
print("found org")
38+
#print("found org")
4239
global org_pos
4340
org_pos = i
4441
return i
@@ -55,92 +52,59 @@ def array_find(index, arr1, arr2):
5552
break
5653
global cfunc
5754
cfunc = arr1[istart:istop]
58-
print("found: "+str(cfunc))
55+
#print("found: "+str(cfunc))
5956
global copy_pos
6057
copy_pos = istart
6158
return istart
6259
return -1
6360

64-
def isSubset(arr1, arr2):
65-
m = len(arr1)
66-
n = len(arr2)
67-
i = 0
68-
j = 0
69-
for i in range(n):
70-
for j in range(m):
71-
if(arr2[i] == arr1[j]):
72-
break
73-
74-
if (j == m):
75-
return -1
76-
return j
61+
# always 2 functions to patch
62+
def find_and_patch(source, sub):
63+
global org_pos
64+
org_pos = 0
65+
global copy_pos
66+
copy_pos = 0
67+
offset = array_find(0, source, sub)
68+
offset = array_find(offset + len(sub), source, sub)
69+
#print(org_pos,copy_pos)
70+
if org_pos==0 or copy_pos==0:
71+
print(sub.decode("utf-8")+" already patched or not needed")
72+
else:
73+
print("patching: "+sub.decode("utf-8"))
74+
patch = sub
75+
# patch with leading x
76+
patch[0] = 120
77+
patch.append(0)
78+
#print(patch)
79+
source[copy_pos:copy_pos+len(patch)] = patch
80+
source[org_pos:org_pos+len(patch)] = patch
81+
7782

7883
def patch_builtins(source, target, env):
7984
print("patching buildtins: "+file)
8085
path=str(target[0])
8186
directory = "/".join(list(path.split('/')[0:-1]))
8287
dpath = directory+"/src/Plugins/"
8388
fpath = dpath + file
84-
#print(fpath)
85-
89+
8690
with open(fpath, mode='rb') as f:
8791
source = f.read()
8892
f.close()
89-
9093
source=bytearray(source)
9194

92-
print(len(source))
93-
94-
sub = bytearray("__addsf3", 'utf-8')
95+
# list patches here
96+
patches = ["__addsf3", "__subsf3", "__mulsf3", "__divsf3", "__nesf2"]
9597

96-
# always 2 functions to patch
97-
global org_pos
98-
orgpos = 0
99-
global copy_pos
100-
copy_pos = 0
101-
offset = array_find(0, source, sub)
102-
offset = array_find(offset + len(sub), source, sub)
103-
104-
if org_pos==0 or copy_pos==0:
105-
print("already patched")
106-
107-
print(org_pos,copy_pos)
98+
for x in patches:
99+
sub = bytearray(x, 'utf-8')
100+
find_and_patch(source, sub)
108101

109-
global cfunc
110-
# replace org with copy and invalidate org
111-
if org_pos > copy_pos:
112-
# move part 1
113-
len1 = copy_pos
114-
len2 = org_pos - copy_pos - len(cfunc)
115-
len3 = len(source) - org_pos - len(sub)
116-
print(len1,len(cfunc),len2,len(sub),len3)
102+
#fpath = dpath + "test.o"
117103

118-
print(len1+len(cfunc)+len2+len(sub)+len3)
119-
120-
sum = len1+len(cfunc)+len2+len(sub)+len3
121-
print(len(source))
122-
123-
part1 = bytearray(len1)
124-
part2 = bytearray(len2)
125-
part3 = bytearray(len3)
126-
127-
part1[0:len1] = source[0:len1]
128-
part2[0:len2] = source[copy_pos + len(cfunc):org_pos]
129-
part3[0:len3] = source[org_pos + len(sub):len(source)]
130-
131-
source[copy_pos:len(sub)] = sub
132-
source[copy_pos+len(sub):len2] = part2
133-
source[copy_pos+len(sub)+len2:] = cfunc
134-
source[copy_pos+len(sub)+len2+len(cfunc):] = part3
135-
136-
137-
#fpath = dpath + "test.o"
138-
139-
with open(fpath, mode='wb') as f:
140-
f.write(source)
141-
f.close()
104+
with open(fpath, mode='wb') as f:
105+
f.write(source)
106+
f.close()
142107

143-
print (len(part1),len(part2),len(part3))
144108

145109
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", [patch_builtins])
146110

tasmota/Plugins/patch_linker_file.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@
8080

8181
out = part1 + insert + part2
8282

83-
#with open(libpath, "w") as wf:
84-
# wf.write(out)
85-
# wf.close()
83+
with open(libpath, "w") as wf:
84+
wf.write(out)
85+
wf.close()
8686

8787
print("patch complete")
8888

tasmota/Plugins/xsns_46_MLX90614_test.cpp

+4-26
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "module.h"
2323
#include "module_defines.h"
24+
#include "intrinsics.h"
2425

2526
#define MLX90614_REV 1 << 16 | 4
2627

@@ -72,29 +73,6 @@ const char JSON_IRTMP[] PROGMEM = ",\"MLX90614\":{\"OBJTMP\":%s,\"AMBTMP\":%s}";
7273
const char mlxdev[] PROGMEM = "MLX90614";
7374

7475

75-
MODULE_PART float __addsf3(float a, float b) {
76-
SETMINREGS
77-
return fadd(a,b);
78-
}
79-
80-
/* 36 bytes
81-
40205a30: e0c112 addi a1, a1, -32
82-
40205a33: 076102 s32i a0, a1, 28
83-
40205a36: 0661c2 s32i a12, a1, 24
84-
40205a39: 006132 s32i a3, a1, 0
85-
40205a3c: 02cd mov.n a12, a2
86-
40205a3e: fb6c05 call0 40201100 <gettbl>
87-
40205a41: 1228 l32i.n a2, a2, 4
88-
40205a43: 0138 l32i.n a3, a1, 0
89-
40205a45: 2b2242 l32i a4, a2, 172
90-
40205a48: 0c2d mov.n a2, a12
91-
40205a4a: 0004c0 callx0 a4
92-
40205a4d: 7108 l32i.n a0, a1, 28
93-
40205a4f: 61c8 l32i.n a12, a1, 24
94-
40205a51: 20c112 addi a1, a1, 32
95-
40205a54: f00d ret.n
96-
*/
97-
9876
int32_t Init_MLX90614() {
9977
ALLOCMEM
10078

@@ -106,8 +84,8 @@ int32_t Init_MLX90614() {
10684
float a = obj_temp;
10785
float b = 2;
10886
ready = a + b;
109-
//amb_temp = __addsf3(a,b);
110-
87+
88+
11189
SETWIRE(0);
11290

11391
// now init variables here
@@ -157,7 +135,7 @@ float MLX90614_GetValue(uint32_t reg) {
157135
void MLX90614_Show(uint32_t json) {
158136
SETREGS
159137

160-
if (ready == false) return;
138+
if (ready == false) return;
161139
char obj_tstr[16];
162140
ftostrfd(obj_temp, Settings->flag2.temperature_resolution, obj_tstr);
163141
char amb_tstr[16];

0 commit comments

Comments
 (0)