Why not use the routines from the Linux kernel, which are battle tested?
https://elixir.bootlin.com/linux/latest/source/arch/x86/incl...
Or even NetBSD: http://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/sys/net/...
EDIT:
freebsd: https://github.com/freebsd/freebsd/blob/master/sys/netinet/i...
openbsd: https://github.com/openbsd/src/blob/master/sys/netinet/in4_c...