Skip to content

Commit

Permalink
GCC: Can now compiled with -O2
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvught committed May 7, 2024
1 parent 3a25056 commit fb6ee8f
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 125 deletions.
1 change: 1 addition & 0 deletions lib-artnet/src/node/artnetnodehandledmx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#if !defined(__clang__)
# pragma GCC push_options
# pragma GCC optimize ("O2")
# pragma GCC optimize ("no-tree-loop-distribute-patterns")
#endif

#include <cstdint>
Expand Down
1 change: 1 addition & 0 deletions lib-artnet/src/node/dmxin/handledmxin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#if !defined(__clang__)
# pragma GCC push_options
# pragma GCC optimize ("O2")
# pragma GCC optimize ("no-tree-loop-distribute-patterns")
#endif

#include <cstdint>
Expand Down
1 change: 1 addition & 0 deletions lib-artnet/src/node/rdm/controller/artnetrdmcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#if !defined(__clang__)
# pragma GCC push_options
# pragma GCC optimize ("O2")
# pragma GCC optimize ("no-tree-loop-distribute-patterns")
#endif

#include <cstdint>
Expand Down
1 change: 1 addition & 0 deletions lib-artnet/src/node/rdm/controller/handlerdmin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#if !defined(__clang__)
# pragma GCC push_options
# pragma GCC optimize ("O2")
# pragma GCC optimize ("no-tree-loop-distribute-patterns")
#endif

#include <cstdint>
Expand Down
41 changes: 13 additions & 28 deletions lib-network/src/net/arp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@

#pragma GCC push_options
#pragma GCC optimize ("O2")
#pragma GCC optimize ("no-tree-loop-distribute-patterns")

#include <cstdint>
#include <cstring>
#include <cassert>

#include "net.h"
#include "net_private.h"
#include "net_memcpy.h"
#include "../../config/net_config.h"

#include "hardware.h"
Expand All @@ -56,11 +58,6 @@ extern uint8_t macAddress[ETH_ADDR_LEN];
} // namespace globals
} // namespace net

typedef union pcast32 {
uint32_t u32;
uint8_t u8[4];
} _pcast32;

void __attribute__((cold)) arp_init() {
arp_cache_init();

Expand All @@ -80,9 +77,7 @@ void __attribute__((cold)) arp_init() {
s_arp_request.arp.opcode = __builtin_bswap16(ARP_OPCODE_RQST);

memcpy(s_arp_request.arp.sender_mac, net::globals::macAddress, ETH_ADDR_LEN);
_pcast32 ip_addr;
ip_addr.u32 = net::globals::ipInfo.ip.addr;
memcpy(s_arp_request.arp.sender_ip, ip_addr.u8, IPv4_ADDR_LEN);
net::memcpy_ip(s_arp_request.arp.sender_ip, net::globals::ipInfo.ip.addr);
memset(s_arp_request.arp.target_mac, 0x00, ETH_ADDR_LEN);

// ARP Reply Template
Expand Down Expand Up @@ -133,10 +128,7 @@ void arp_send_request(uint32_t nIp) {

s_requestType = net::arp::RequestType::REQUEST;

_pcast32 ip_addr;
ip_addr.u32 = nIp;

memcpy(s_arp_request.arp.target_ip, ip_addr.u8, IPv4_ADDR_LEN);
net::memcpy_ip(s_arp_request.arp.target_ip, nIp);

emac_eth_send(reinterpret_cast<void *>(&s_arp_request), sizeof(struct t_arp));

Expand All @@ -159,9 +151,7 @@ void arp_send_probe() {

arp_send_request(net::globals::ipInfo.ip.addr);

_pcast32 ip_addr;
ip_addr.u32 = net::globals::ipInfo.ip.addr;
memcpy(s_arp_request.arp.sender_ip, ip_addr.u8, IPv4_ADDR_LEN);
net::memcpy_ip(s_arp_request.arp.sender_ip, net::globals::ipInfo.ip.addr);

DEBUG_EXIT
}
Expand All @@ -185,17 +175,14 @@ void arp_send_announcement() {
void arp_handle_request(struct t_arp *p_arp) {
DEBUG_ENTRY

_pcast32 target;

memcpy(target.u8, p_arp->arp.target_ip, IPv4_ADDR_LEN);

_pcast32 sender;
const auto nIpTarget = net::memcpy_ip(p_arp->arp.target_ip);

memcpy(sender.u8, p_arp->arp.sender_ip, IPv4_ADDR_LEN);

DEBUG_PRINTF("Sender " IPSTR " Target " IPSTR, IP2STR(sender.u32), IP2STR(target.u32));
#ifndef NDEBUG
const auto nIpSender = net::memcpy_ip(p_arp->arp.sender_ip);
DEBUG_PRINTF("Sender " IPSTR " Target " IPSTR, IP2STR(nIpSender), IP2STR(nIpTarget));
#endif

if (!((target.u32 == net::globals::ipInfo.ip.addr) || (target.u32 == net::globals::ipInfo.secondary_ip.addr) || (target.u32 == net::globals::ipInfo.broadcast_ip.addr))) {
if (!((nIpTarget == net::globals::ipInfo.ip.addr) || (nIpTarget == net::globals::ipInfo.secondary_ip.addr) || (nIpTarget == net::globals::ipInfo.broadcast_ip.addr))) {
DEBUG_PUTS("No for me.");
DEBUG_EXIT
return;
Expand All @@ -206,7 +193,7 @@ void arp_handle_request(struct t_arp *p_arp) {
// ARP Header
memcpy(s_arp_reply.arp.target_mac, p_arp->arp.sender_mac, ETH_ADDR_LEN);
memcpy(s_arp_reply.arp.target_ip, p_arp->arp.sender_ip, IPv4_ADDR_LEN);
memcpy(s_arp_reply.arp.sender_ip, target.u8, IPv4_ADDR_LEN);
net::memcpy_ip(s_arp_reply.arp.sender_ip, nIpTarget);

emac_eth_send(reinterpret_cast<void *>(&s_arp_reply), sizeof(struct t_arp));

Expand All @@ -218,9 +205,7 @@ void arp_handle_reply(struct t_arp *p_arp) {

switch (s_requestType) {
case net::arp::RequestType::REQUEST: {
_pcast32 sender;
memcpy(sender.u8, p_arp->arp.sender_ip, IPv4_ADDR_LEN);
arp_cache_update(p_arp->arp.sender_mac, sender.u32);
arp_cache_update(p_arp->arp.sender_mac, net::memcpy_ip(p_arp->arp.sender_ip));
}
break;
case net::arp::RequestType::PROBE:
Expand Down
23 changes: 10 additions & 13 deletions lib-network/src/net/arp_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file arp_cache.cpp
*
*/
/* Copyright (C) 2018-2023 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2018-2024 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,6 +23,10 @@
* THE SOFTWARE.
*/

#pragma GCC push_options
#pragma GCC optimize ("O2")
#pragma GCC optimize ("no-tree-loop-distribute-patterns")

#include <cstdint>
#include <cstring>
#include <cassert>
Expand All @@ -47,17 +51,12 @@ struct ArpRecord {
uint8_t mac_address[ETH_ADDR_LEN];
};

typedef union pcast32 {
uint32_t u32;
uint8_t u8[4];
} _pcast32;

static ArpRecord s_ArpRecords[MAX_RECORDS] SECTION_NETWORK ALIGNED;
static uint16_t s_Entries SECTION_NETWORK ALIGNED;

#ifndef NDEBUG
# define TICKER_COUNT 100 ///< 10 seconds
static volatile uint32_t s_ticker ;
static uint32_t s_ticker ;
#endif

void __attribute__((cold)) arp_cache_init() {
Expand Down Expand Up @@ -100,16 +99,14 @@ uint32_t arp_cache_lookup(uint32_t nIp, uint8_t *pMacAddress) {
DEBUG_ENTRY
DEBUG_PRINTF(IPSTR " " MACSTR, IP2STR(nIp), MAC2STR(pMacAddress));

uint32_t i;

for (i = 0; i < MAX_RECORDS; i++) {
if (s_ArpRecords[i].nIp == nIp) {
memcpy(pMacAddress, s_ArpRecords[i].mac_address, ETH_ADDR_LEN);
for (auto& record : s_ArpRecords) {
if (record.nIp == nIp) {
memcpy(pMacAddress, record.mac_address, ETH_ADDR_LEN);
DEBUG_EXIT
return nIp;
}

if (s_ArpRecords[i].nIp == 0) {
if (record.nIp == 0) {
break;
}
}
Expand Down
18 changes: 6 additions & 12 deletions lib-network/src/net/icmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@

#pragma GCC push_options
#pragma GCC optimize ("O2")
#pragma GCC optimize ("no-tree-loop-distribute-patterns")

#include <cstdint>
#include <cstring>

#include "net.h"
#include "net_memcpy.h"
#include "net_private.h"

#include "../../config/net_config.h"
Expand All @@ -41,11 +43,6 @@ extern uint8_t macAddress[ETH_ADDR_LEN];
} // namespace globals
} // namespace net

typedef union pcast32 {
uint32_t u32;
uint8_t u8[4];
} _pcast32;

__attribute__((hot)) void icmp_handle(struct t_icmp *p_icmp) {
if (p_icmp->icmp.type == ICMP_TYPE_ECHO) {
if (p_icmp->icmp.code == ICMP_CODE_ECHO) {
Expand All @@ -55,17 +52,14 @@ __attribute__((hot)) void icmp_handle(struct t_icmp *p_icmp) {
// IPv4
p_icmp->ip4.id = static_cast<uint16_t>(~p_icmp->ip4.id);

_pcast32 dst;
memcpy(dst.u8, p_icmp->ip4.dst, IPv4_ADDR_LEN);
const auto nIpDestination = net::memcpy_ip(p_icmp->ip4.dst);

memcpy(p_icmp->ip4.dst, p_icmp->ip4.src, IPv4_ADDR_LEN);

if (dst.u32 == net::globals::ipInfo.secondary_ip.addr) {
memcpy(p_icmp->ip4.src, dst.u8, IPv4_ADDR_LEN);
if (nIpDestination == net::globals::ipInfo.secondary_ip.addr) {
net::memcpy_ip(p_icmp->ip4.src, net::globals::ipInfo.secondary_ip.addr);
} else {
_pcast32 src;
src.u32 = net::globals::ipInfo.ip.addr;
memcpy(p_icmp->ip4.src, src.u8, IPv4_ADDR_LEN);
net::memcpy_ip(p_icmp->ip4.src, net::globals::ipInfo.ip.addr);
}

p_icmp->ip4.chksum = 0;
Expand Down
21 changes: 9 additions & 12 deletions lib-network/src/net/igmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file igmp.cpp
*
*/
/* Copyright (C) 2018-2023 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2018-2024 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,11 +23,16 @@
* THE SOFTWARE.
*/

#pragma GCC push_options
#pragma GCC optimize ("O2")
#pragma GCC optimize ("no-tree-loop-distribute-patterns")

#include <cstdint>
#include <cstdio>
#include <cstring>

#include "net.h"
#include "net_memcpy.h"
#include "net_private.h"

#include "../../config/net_config.h"
Expand Down Expand Up @@ -68,12 +73,8 @@ extern uint8_t macAddress[ETH_ADDR_LEN];
static void _send_report(uint32_t nGroupAddress);

void igmp_set_ip() {
_pcast32 src;

src.u32 = net::globals::ipInfo.ip.addr;

memcpy(s_report.ip4.src, src.u8, IPv4_ADDR_LEN);
memcpy(s_leave.ip4.src, src.u8, IPv4_ADDR_LEN);
net::memcpy_ip(s_report.ip4.src, net::globals::ipInfo.ip.addr);
net::memcpy_ip(s_leave.ip4.src, net::globals::ipInfo.ip.addr);
}

void __attribute__((cold)) igmp_init() {
Expand Down Expand Up @@ -186,10 +187,6 @@ static void _send_report(const uint32_t nGroupAddress) {

static void _send_leave(const uint32_t nGroupAddress) {
DEBUG_ENTRY
_pcast32 multicast_ip;

multicast_ip.u32 = nGroupAddress;

DEBUG_PRINTF(IPSTR " " MACSTR, IP2STR(nGroupAddress), MAC2STR(s_multicast_mac));

// IPv4
Expand All @@ -199,7 +196,7 @@ static void _send_leave(const uint32_t nGroupAddress) {
s_leave.ip4.chksum = net_chksum(reinterpret_cast<void *>(&s_leave.ip4), 24); //TODO
#endif
// IGMP
memcpy(s_leave.igmp.report.igmp.group_address, multicast_ip.u8, IPv4_ADDR_LEN);
net::memcpy_ip(s_leave.igmp.report.igmp.group_address, nGroupAddress);
s_leave.igmp.report.igmp.checksum = 0;
#if !defined (CHECKSUM_BY_HARDWARE)
s_leave.igmp.report.igmp.checksum = net_chksum(reinterpret_cast<void *>(&s_leave.ip4), IPv4_IGMP_REPORT_HEADERS_SIZE);
Expand Down
6 changes: 5 additions & 1 deletion lib-network/src/net/ip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file ip.cpp
*
*/
/* Copyright (C) 2018-2023 by Arjan van Vught mailto:[email protected]
/* Copyright (C) 2018-2024 by Arjan van Vught mailto:[email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,6 +23,10 @@
* THE SOFTWARE.
*/

#pragma GCC push_options
#pragma GCC optimize ("O2")
#pragma GCC optimize ("no-tree-loop-distribute-patterns")

#include <cstdint>

#include "net.h"
Expand Down
14 changes: 7 additions & 7 deletions lib-network/src/net/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#pragma GCC push_options
#pragma GCC optimize ("O2")
#pragma GCC optimize ("no-tree-loop-distribute-patterns")

#include <cstdint>
#include <cstring>
Expand All @@ -51,9 +52,9 @@ void ptp_run();
} // namespace net

static uint8_t *s_p;
static bool s_isDhcp = false;
static bool s_isDhcp;

static void refresh_and_init(struct IpInfo *pIpInfo, bool doInit) {
static void refresh_and_init(struct IpInfo *pIpInfo, const bool doInit) {
net::globals::ipInfo.broadcast_ip.addr = net::globals::ipInfo.ip.addr | ~net::globals::ipInfo.netmask.addr;

net::globals::nBroadcastMask = ~(net::globals::ipInfo.netmask.addr);
Expand Down Expand Up @@ -140,7 +141,7 @@ void net_set_ip(struct IpInfo *pIpInfo) {
net::globals::ipInfo.ip.addr = pIpInfo->ip.addr;

if (net::globals::ipInfo.ip.addr == 0) {
set_secondary_ip();
set_secondary_ip();
}

refresh_and_init(pIpInfo, true);
Expand All @@ -166,7 +167,7 @@ void net_set_gw(struct IpInfo *pIpInfo) {
}

bool net_set_dhcp(struct IpInfo *pIpInfo, const char *const pHostname, bool *isZeroconfUsed) {
bool isDhcp = false;
auto isDhcp = false;
*isZeroconfUsed = false;

if (dhcp_client(pHostname) < 0) {
Expand All @@ -185,6 +186,7 @@ bool net_set_dhcp(struct IpInfo *pIpInfo, const char *const pHostname, bool *isZ
arp_send_announcement();
} else {
console_error("IP Conflict!\n");
return false;
}

return isDhcp;
Expand All @@ -196,9 +198,7 @@ void net_dhcp_release() {
}

bool net_set_zeroconf(struct IpInfo *pIpInfo) {
const auto b = rfc3927();

if (b) {
if (rfc3927()) {
refresh_and_init(pIpInfo, true);

s_isDhcp = false;
Expand Down
Loading

0 comments on commit fb6ee8f

Please sign in to comment.