Skip to content

Instantly share code, notes, and snippets.

@Swyter
Last active April 20, 2024 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swyter/e6414c6c2451e4bbe3f5fad04bec7b75 to your computer and use it in GitHub Desktop.
Save Swyter/e6414c6c2451e4bbe3f5fad04bec7b75 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
int fusb_measure_vbus_bsearch_comp(const void *a, const void *b)
{
long unsigned int index = (long unsigned int) b; float volt = (index * 0.420f);
int val = volt <= 5.040000f ? 0x80 : 0;
printf("-- %u %u %f = %x\n", a, b, volt, val);
return val == 0x80 ? 1 : -1;
}
int main()
{
printf("asdf\n");
void* key = bsearch(NULL, NULL, 64, 1, fusb_measure_vbus_bsearch_comp);
printf("key: %p\n", key);
}
@Swyter
Copy link
Author

Swyter commented Apr 15, 2024

Simple method in Godbolt.

ASM generation compiler returned: 0
Execution build compiler returned: 0
Program returned: 0
asdf
-- 3584316924 32 13.440000 = 0
-- 3584316924 16 6.720000 = 0
-- 3584316924 8 3.360000 = 80
-- 3584316924 12 5.040000 = 80
-- 3584316924 14 5.880000 = 0
-- 3584316924 13 5.460000 = 0
key: (nil)

Alternate method in Godbolt. Checking that we're (0x80) and the index above us is (0x00).

ASM generation compiler returned: 0
Execution build compiler returned: 0
Program returned: 0
asdf
[-] 0 32
- 13.440000 = 0
[-] 0 16
- 6.720000 = 0
[-] 0 8
- 3.360000 = 80
- 3.780000 = 80
[-] 0 12
- 5.040000 = 80
- 5.460000 = 0
key: (nil)

@Swyter
Copy link
Author

Swyter commented Apr 15, 2024

Alternate V2 in Godbolt.

Execution build compiler returned: 0
Program returned: 0
asdf
[-] 0 32 0
- 13.440000 = 0
[-] 0 16 0
- 6.720000 = 0
[-] 0 8 0
- 3.360000 = 80
- 3.780000 = 80
[-] 0 12 0
- 5.040000 = 80
- 5.460000 = 0
key: 12

@Swyter
Copy link
Author

Swyter commented Apr 20, 2024

imagen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment